gpt4 book ai didi

Ansible 在其处理程序中未检测到角色默认变量

转载 作者:行者123 更新时间:2023-12-01 03:26:21 25 4
gpt4 key购买 nike

ansible 是否将角色默认变量传递给同一角色内的处理程序?

这是有问题的剧本的最小摘录:

角色层次

- playbook.yml
- roles/
- gunicorn/
- defaults/
- main.yml
- handlers/
- main.yml
- code-checkout/
- tasks/
- main.yml

这是文件内容

gunicorn/defaults/main.yml
---
gu_log: "/tmp/gunicorn.log"

gunicorn/handlers/main.yml
---
- name: Clear Gunicorn Log
shell: rm {{ gu_log }}

完成/任务/main.yml
---
- name: Test Handlers
shell: ls
notify:
- Restart Gunicorn

playbook.yml
---
- name: Deploy
hosts: webservers
tasks:
- include: roles/finalize/tasks/main.yml
handlers:
- include: roles/gunicorn/handlers/main.yml

AFAIK 一切看起来都不错。但是,我在剧本执行期间收到此错误

FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'gu_log' is undefined\n\nThe error appears to have been in '/roles/gunicorn/handlers/main.yml': line 3, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Restart Gunicorn\n ^ here\n"}



在 Ubuntu 12.04 LTS 上使用 Ansible 2.2

这是 techraf 脚本的修改版本,它创建了所有目录并演示了我的问题
#!/bin/bash

mkdir -p ./rtindru-test/roles/gunicorn
mkdir -p ./rtindru-test/roles/gunicorn/defaults
mkdir -p ./rtindru-test/roles/gunicorn/handlers
mkdir -p ./rtindru-test/roles/finalize/tasks

cat >./rtindru-test/roles/finalize/tasks/main.yml <<HANDLERS_END
---
- name: Test Handlers
shell: rm {{ gu_log }}
HANDLERS_END

cat >./rtindru-test/roles/gunicorn/handlers/main.yml <<HANDLERS_END
---
- name: Clear Gunicorn Log
shell: rm {{ gu_log }}
HANDLERS_END

cat >./rtindru-test/roles/gunicorn/defaults/main.yml <<DEFAULTS_END
---
gu_log: "/tmp/gunicorn.log"
DEFAULTS_END

cat >./rtindru-test/playbook.yml <<PLAYBOOK_END
---
- name: Deploy
hosts: localhost
tasks:
- include: roles/finalize/tasks/main.yml
handlers:
- include: roles/gunicorn/handlers/main.yml
PLAYBOOK_END

touch /tmp/gunicorn.log
ls -l /tmp/gunicorn.log
ansible-playbook ./rtindru-test/playbook.yml
ls -l /tmp/gunicorn.log

输出

PLAY [Deploy]


TASK [setup] ******************************************************************* ok: [localhost]

TASK [Test Handlers] *********************************************************** fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'gu_log' is undefined\n\nThe error appears to have been in '/rtindru-test/roles/finalize/tasks/main.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Test Handlers\n ^ here\n"} to retry, use: --limit @/rtindru-test/playbook.retry

PLAY RECAP ********************************************************************* localhost : ok=1 changed=0 unreachable=0
failed=1

最佳答案

您既没有定义也没有使用任何角色。具有以下任务:

- include: roles/finalize/tasks/main.yml

您只将任务文件包含在您的剧本中。它与角色无关。

要分配角色,您应该为戏剧指定角色列表(一个或多个):
role:
- my_role1
- my_role2

请查看 roles 上的文档并随意使用由以下脚本创建的剧本和结构。

Does ansible pass Role Default variables to the Handlers within the same Role?



是的,它确实。

对于证明,请运行以下 bash 脚本,该脚本创建并运行一个最小示例。它需要 gunicorn/defaults/main.yml 的内容和 gunicorn/handlers/main.yml从完整的问题并添加缺失的组件:任务和剧本。它创建一个要删除的文件并运行剧本。
#!/bin/bash

mkdir -p ./so41285033/roles/gunicorn
mkdir -p ./so41285033/roles/gunicorn/defaults
mkdir -p ./so41285033/roles/gunicorn/handlers
mkdir -p ./so41285033/roles/gunicorn/tasks

cat >./so41285033/roles/gunicorn/tasks/main.yml <<TASKS_END
---
- debug:
changed_when: true
notify: Clear Gunicorn Log
TASKS_END

cat >./so41285033/roles/gunicorn/handlers/main.yml <<HANDLERS_END
---
- name: Clear Gunicorn Log
shell: rm {{ gu_log }}
when: "'apiservers' not in group_names"
HANDLERS_END

cat >./so41285033/roles/gunicorn/defaults/main.yml <<DEFAULTS_END
---
gu_log: "/tmp/gunicorn.log"
DEFAULTS_END

cat >./so41285033/playbook.yml <<PLAYBOOK_END
---
- hosts: localhost
gather_facts: no
connection: local
roles:
- gunicorn
PLAYBOOK_END

touch /tmp/gunicorn.log
ls -l /tmp/gunicorn.log
ansible-playbook ./so41285033/playbook.yml
ls -l /tmp/gunicorn.log

结果:
-rw-r--r--  1 techraf  wheel  0 Dec 23 07:57 /tmp/gunicorn.log
[WARNING]: Host file not found: /etc/ansible/hosts

[WARNING]: provided hosts list is empty, only localhost is available


PLAY [localhost] ***************************************************************

TASK [gunicorn : debug] ********************************************************
ok: [localhost] => {
"msg": "Hello world!"
}

RUNNING HANDLER [gunicorn : Clear Gunicorn Log] ********************************
changed: [localhost]
[WARNING]: Consider using file module with state=absent rather than running rm


PLAY RECAP *********************************************************************
localhost : ok=2 changed=2 unreachable=0 failed=0

ls: /tmp/gunicorn.log: No such file or directory

解释:
  • 在运行剧本之前,文件 /tmp/gunicorn.log已创建并验证其存在:
    -rw-r--r--  1 techraf  wheel  0 Dec 23 07:57 /tmp/gunicorn.log
  • 运行剧本后文件/tmp/gunicorn.log不存在:
    ls: /tmp/gunicorn.log: No such file or directory
  • Ansible 正确传递了变量 gu_log值到 Clear Gunicorn Log删除文件的处理程序。

  • 最后备注:

    问题中描述的问题无法重现,因为问题不包含 完整 也不是 可验证 MCVE 含义中的示例.

    关于Ansible 在其处理程序中未检测到角色默认变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41285033/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com