Bartosz Bilicki
Bartosz Bilicki

Reputation: 13235

Parametrized ansible task include - 'paramater is undefined'

I have problem using parametrized ansible include.

I have created following file, named 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 }}"

I am including this file in my playbook.yml, passing value of state parameter

  - include: tasks/haproxy.yml state=enabled

I am getting following error

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

state is my parameter, passed when doing include (as described in http://docs.ansible.com/ansible/playbooks_roles.html#task-include-files-and-encouraging-reuse) Whats wrong?

I am using Ansible 2.0.2.0.

edit: using alternative syntax for passing paramteres

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

gives exactly same error message.

Upvotes: 1

Views: 459

Answers (1)

Bartosz Bilicki
Bartosz Bilicki

Reputation: 13235

Resolved by removing single leading space (!!) when using alternative syntax (vars).

So correct parametrized include is

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

vars keyword must be at the same level as include keyword. Otherwise it does not work, with message ERROR! 'state' is undefined.

Shortened syntax (- include: tasks/haproxy.yml state=enabled) still does not work.

Upvotes: 1

Related Questions