cstewart28
cstewart28

Reputation: 15

ansible magic variables not returning values

I'm trying to build a /etc/hosts file; however it seems that I can't get the hostvars to show up when running a playbook, but with the command line it works to a point.

Here is my ansible.cfg file:

[defaults]
ansible_managed = Please do not change this file directly since it is managed by Ansible and will be overwritten
library = ./library
module_utils = ./module_utils
action_plugins = plugins/actions
callback_plugins = plugins/callback
filter_plugins = plugins/filter
roles_path = ./roles
# Be sure the user running Ansible has permissions on the logfile
log_path = $HOME/ansible/ansible.log
inventory = hosts

forks = 20
host_key_checking = False
gathering = smart
fact_caching = jsonfile
fact_caching_connection = $HOME/ansible/facts
fact_caching_timeout = 7200
nocows = 1
callback_whitelist = profile_tasks
stdout_callback = yaml
force_valid_group_names = ignore
inject_facts_as_vars = False

# Disable them in the context of https://review.openstack.org/#/c/469644
retry_files_enabled = False

# This is the default SSH timeout to use on connection attempts
# CI slaves are slow so by setting a higher value we can avoid the following error:
# Timeout (12s) waiting for privilege escalation prompt:
timeout = 60

[ssh_connection]
# see: https://github.com/ansible/ansible/issues/11536
control_path = %(directory)s/%%h-%%r-%%p
ssh_args = -o ControlMaster=auto -o ControlPersist=600s
pipelining = True

# Option to retry failed ssh executions if the failure is encountered in ssh itself
retries = 10

Here is my playbook:

- name: host file update
  hosts: baremetal
  become: true
  gather_facts: true 
  
  vars:
    primarydomain: "cephcluster.local"

  tasks:
    - name: print ipv4 info
      debug:
        msg: "IPv4 addresses: {{ansible_default_ipv4.address }}"

    - name: update host file
      lineinfile:
        dest: /etc/hosts
        regexp: '{{ hostvars[item].ansible_default_ipv4.address }}.*{{ item }} {{item}}.{{primarydomain}}$'
        line: "{{ hostvars[item].ansible_default_ipv4.address }} {{item}} {{item}}.{{primarydomain}}"
        state: present
        with_items: "{{ groups.baremetal }}"

Here is my inventory file:

[baremetal]
svr1 ansible_host=192.168.59.10
svr2 ansible_host=192.168.59.11
svr3 ansible_host=192.168.59.12

When I run the playbook I get:

FAILED! => 
  msg: |-
    The task includes an option with an undefined variable. The error was: 'ansible_default_ipv4' is undefined

However when I run

ansible baremetal -m gather_facts -a "filter=ansible_default_ipv4"

I get:

| SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.168.59.20", 
            "alias": "eno1", 
            "broadcast": "192.168.59.255", 
            "gateway": "192.168.59.1", 
            "interface": "eno1", 
            "macaddress": "80:18:44:e0:4b:a4", 
            "mtu": 1500, 
            "netmask": "255.255.255.0", 
            "network": "192.168.59.0", 
            "type": "ether"
        }
    }, 
    "changed": false, 
    "deprecations": [], 
    "warnings": []
}

But; if I run

ansible baremetal -m gather_facts -a "filter=ansible_default_ipv4.address"

I get nothing in the return:

SUCCESS => {
    "ansible_facts": {}, 
    "changed": false, 
    "deprecations": [], 
    "warnings": []
}

I have even tried in the playbook:

- debug:
    msg: "IPv4 addresses: {{ansible_default_ipv4 }}"

and nothing gets returned.

Not sure what I'm missing.

Upvotes: 1

Views: 510

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39129

You should get used to use and abuse the debug module.

Provided that the ansible ad-hoc command raised you that the facts are under ansible_facts, you could have done a simple task in your playbook:

- debug:
    var: ansible_facts

From that, you would have discovered that the fact you are looking for is nested in the dictionary ansible_facts under the key default_ipv4. And that you can access its address with the property address.

So, your debug task ends up being:

- debug:
    msg: "IPv4 addresses: {{ ansible_facts.default_ipv4.address }}"

Upvotes: 1

Related Questions