0
votes

I'm facing a mysterious ansible behaviour.

I've set groups, hosts and groups vars in my inventory file, but the ansible-playbook command is raising Undefined Variable for all my group vars, while printing them with the ansible debug module show the variable well.

Here is my inventory.ini file:

 [windows]
ad_server ansible_host=mrspw00550.mydomain.com
[windows:vars]
ansible_connection=winrm
ansible_winrm_transport=ntlm
ansible_port=5985
ansible_become_method=runas
dns_zone=mydomain.com
ptr_zone=10.in-addr.arpa
dns_server=10.0.100.100

[linux]
web_server ansible_host=10.0.101.129
[linux:vars]
ansible_connection=ssh
ansible_become=yes
ansible_become_method=sudo
dns_zone=mydomain.com
ptr_zone=10.in-addr.arpa
dns_server=10.0.100.100
linux_home_dir=/home/ldaphome

the playbook file (set-hostname-and-dns.yml):

- name: Debug vars
  debug: var=hostvars[inventory_hostname]['dns_zone']
  delegate_to: ad_server

- name: Set  hostname and make DNS registrations
  block:
    - name: Set  hostname
      hostname:
        name: "web.{{ dns_zone }}"
      delegate_to: web_server
    - name: Create a DNS registration of type A 
      win_dns_record:
        name: "web"
        type: "A"
        value: "{{ inventory_hostname }}"
        zone: "{{ dns_zone }}"
        computer_name: "{{ dns_server }}"
      delegate_to: "{{ ad_server }}"
    - name: Create a DNS registration of type PTR 
      win_dns_record:
        name: "{{ inventory_hostname }}"
        type: "PTR"
        value: "web"
        zone: "{{ ptr_zone }}"
        computer_name: "{{ dns_server }}"
      delegate_to: "{{ ad_server }}"

Running the ansible-playbook command gives :

$ ansible-playbook playbook.yml  -i inventory-files/inventory.ini
PLAY [localhost] *********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [localhost]
TASK [create-linux-vm : Debug vars] **************************************************************************************************************************************************************************
ok: [localhost -> mrspw00550.mydomain.com] => {
    "hostvars[inventory_hostname]['dns_zone']": "VARIABLE IS NOT DEFINED!"
}
TASK [create-linux-vm : Set web_server hostname] **************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dns_zone' is undefined\n\nThe error appears to be in 'set-hostname-and-dns.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  block:\n    - name: Set {{ vm_name }} hostname\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}
PLAY RECAP ***************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Running debug module:

$ ansible -m debug -i inventory-files/inventory.ini -a "var=hostvars[inventory_hostname]['dns_zone']" all
ad_server | SUCCESS => {
    "hostvars[inventory_hostname]['dns_zone']": "mydomain.com"
}
web_server | SUCCESS => {
    "hostvars[inventory_hostname]['dns_zone']": "mydomain.com"
}
$

So as you can see, ansible-playbook command is unable to retrieve my host vars, while ansible debug module does.

The result is the same even if I define those variables in inventory-files/group_vars/linux.yml or inventory-files/group_vars/windows.yml, the variable is still as undefined by ansible. The problem is the same if try to access any other variable than special variables from inventory running ansible-playbook

What could be wrong there?

1
I think the output of your playbook shows it is running for "localhost" instead of "ad_server" or "web_server".SipSeb
@SipSeb but as you can see I specified the inventory file, and delegated task to host in the inventorynixmind
What ansible version are you running? There is a Bug report and a merge request from mid last year, apparently it was backported to Ansible 2.10. Before that, it seems that when working with "delegate_to", you still had could only access the hostvars from inventory_name instead of those of the delegate_to target.SipSeb
delegate_to only delegates the execution, not the facts and varr. If you want to get a var/fact from an other host (i.e. a windows host while running on localhost), you will have to specifically ask for it. One example from your example inventory: {{ hostvars['ad_server'].ptr_zone }}Zeitounator

1 Answers

0
votes

I tried with delegate_facts set to true, it didn't work.

Then defined a local group in the inventory containing localhost and defined my local group vars there like this :

[local]
localhost

[local:vars]
ansible_connection=local
dns_zone=mydomain.com
ptr_zone=10.in-addr.arpa
dns_server=10.0.100.100
linux_home_dir=/home/ldaphome

and remove those var from remote hosts facts, as I don't really those variables there. All my tasks are executed on the localhost and delegated to the remote hosts depending to the action.

I'd also like to precise that specifically ask for var/fact like @Zeitounator mentioned above ({{ hostvars['ad_server'].ptr_zone }}) in his comment works too.