Ansible未在其处理程序

时间:2016-12-22 14:07:51

标签: ansible ansible-2.x ansible-role ansible-handlers

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

这是该剧本剧本的最小摘录:

角色层次结构

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

这是文件内容

gunicorn /默认/ main.yml

---
gu_log: "/tmp/gunicorn.log"

gunicorn /处理程序/ 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一切看起来都不错。但是,我在剧本执行期间收到此错误

  

失败! => {"失败":是的," msg":"字段' args'有无效   value,似乎包含未定义的变量。该   错误是:' gu_log'未定义\ n \ n错误似乎已经出现   ' /roles/gunicorn/handlers/main.yml' ;:   第3行,第3列,但可能在文件的其他位置,具体取决于   确切的语法问题。\ n \ n违规行似乎是:\ n \ n \ n-   名称:重新启动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

输出

  

播放[部署]

           

任务[设置]   ************************************************** *****************好的:[localhost]

     

任务[测试处理程序]   ************************************************** *********致命:[localhost]:失败了! =&GT; {&#34;失败&#34;:是的,&#34; msg&#34;:&#34;字段&#39; args&#39;具有   一个无效的值,似乎包含一个变量   未定义。错误是:&#39; gu_log&#39;未定义\ n \ n出现错误   已进入&#39; /rtindru-test/roles/finalize/tasks/main.yml' ;:第2行,   第3列,但可能在文件的其他位置,具体取决于具体情况   语法问题。\ n \ n违规行似乎是:\ n \ n --- \ n-名称:   测试处理程序\ n ^这里\ n&#34;}重试,使用: - limit   @ / rtindru测试/ playbook.retry

     

PLAY RECAP   ************************************************** ******************* localhost:ok = 1 changed = 0 unreachable = 0
  失败= 1

1 个答案:

答案 0 :(得分:2)

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

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

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

要指定角色,您应指定游戏的角色列表(一个或多个):

role:
  - my_role1
  - my_role2

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

  

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

是的。

对于证明运行以下bash脚本,它创建并运行一个最小的示例。它将问题的gunicorn/defaults/main.ymlgunicorn/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

解读:

  • 在运行Playbook之前,已创建文件/tmp/gunicorn.log并验证其存在:

    -rw-r--r--  1 techraf  wheel  0 Dec 23 07:57 /tmp/gunicorn.log
    
  • 运行playbook后,文件/tmp/gunicorn.log不存在:

    ls: /tmp/gunicorn.log: No such file or directory
    
  • Ansible正确地将变量gu_log值传递给删除了该文件的Clear Gunicorn Log处理程序。

最后评论:

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