参数化的安全任务包括 - '参数未定义'

时间:2016-04-29 09:53:18

标签: ansible ansible-playbook

我在使用参数化的ansible include时遇到了问题。

我创建了以下文件,名为 tasks / haproxy.xml

 - name: "change node state to {{state}} in haproxy"
    tags:
    - "haproxy-{{state}}"
    become: yes
    become_user: root
    haproxy:
      state: "{{ state }}"
      wait: yes
      host: "{{ inventory_hostname }}"
      backend: app
      socket: /var/container_data/haproxy/run/haproxy.sock
    delegate_to: "{{ item }}"
    with_items: "{{ groups.haproxy }}"

我在playbook.yml中包含了这个文件,传递了state参数的值

  - include: tasks/haproxy.yml state=enabled

我收到以下错误

TASK [include] *****************************************************************
included: /home/bb/tasks/haproxy.yml for 172.16.224.68, 172.16.224.69
ERROR! 'state' is undefined

state是我的参数,在执行include时传递(如http://docs.ansible.com/ansible/playbooks_roles.html#task-include-files-and-encouraging-reuse中所述) 怎么了?

我正在使用Ansible 2.0.2.0。

编辑: 使用替代语法传递参数

 - include: tasks/haproxy.yml
    vars:
     state: enabled

给出完全相同的错误消息。

1 个答案:

答案 0 :(得分:1)

使用替代语法(vars)时删除单个前导空格(!!)解决了。

所以正确的参数化包括

 - include: tasks/haproxy.yml
   vars:
    state: enabled

vars关键字必须与include关键字处于同一级别。 否则它不起作用,消息为ERROR! 'state' is undefined

缩短的语法(- include: tasks/haproxy.yml state=enabled)仍无效。