参数化 ansible 任务包括 - 'paramater is undefined'



我在使用参数化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中,传递状态参数的值

  - 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-包括文件并鼓励重复使用)怎么了?

我使用的是Ansible 2.0.2.0。

编辑:使用替代语法传递paramteres

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

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

使用替代语法(vars)时,通过删除单个前导空格(!!)进行解析。

因此,正确的参数化包含是

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

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

缩短的语法(- include: tasks/haproxy.yml state=enabled)仍然不起作用。

相关内容

最新更新