Don Branson
Don Branson

Reputation: 13709

Ansible not seeing ansible_eth1 device

Given this debug:

  - name: Display all variables/facts known for a host
    debug: var=hostvars[inventory_hostname]

Producing, in part, this output:

ok: [ny2-uat-app02] => {
    "var": {
        "hostvars[inventory_hostname]": {
            ...
            "ansible_eth1": {
                "active": true,
                "device": "eth1",
                "ipv4": {
                    ...
                },
                "ipv6": [
                    {
                        ...
                    }
                ],
    ...

Why does this line in a template:

- seeds: "{% for host in groups['seeds'] %}{{ hostvars[host]['ansible_' + internode_interface]['ipv4']['address'] }}{% if not loop.last %},{% endif %}{% endfor %}"

Produce this error:

fatal: [ny2-uat-app02] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict object' has no attribute u'ansible_eth1'", 'failed': True}

The debug task runs right before the ansible task to process the template.

What I'm trying to accomplish is to add a new node to an existing cluster.

Upvotes: 2

Views: 3845

Answers (2)

Peter De Zwart
Peter De Zwart

Reputation: 179

One more reminder: do not limit the hosts in the ansible-playbook command to a specific host when that hosts needs variables form other hosts!

ansible-playbook -l 'node1' hosts.yml 

can not find ansible_eth1 form other hosts from 'all' so should be

ansible-playbook hosts.yml 

Upvotes: 0

nghnam
nghnam

Reputation: 671

Because when you run {% for host in groups['seeds'] %}, a specific host can not access variables of other hosts in group seeds, so the task was fail.

To access variables of other hosts, you should enable fact caching.

In playbook files, you should adding gather_facts: True to update facts.

Upvotes: 4

Related Questions