Ansible: Working with Variables and Hostvars

Gather and Display Host Fact

- name: Retrieve host vars
hosts:
- raspis
- server
tasks:
- debug:
var=hostvars[inventory_hostname]
ansible-playbook retrieve_host_vars —limit "raspi-4-1"TASK [get variables] **********************************************************************************
ok: [raspi-4-1] => {
"hostvars[inventory_hostname]": {
"_facts_gathered": true,
"all_ipv4_addresses": [
"192.168.2.111"
],
"architecture": "armv7l",
"distribution": "Debian",
"distribution_release": "buster",
"distribution_version": "10",
"hostname": "raspi-4-1",
"hostnqn": "",
"kernel": "4.19.97-v7l+",
"kernel_version": "#1294 SMP Thu Jan 30 13:21:14 GMT 2020",
[...]
"machine": "armv7l",
"machine_id": "edaca707ad4b4b8991d2341c902160f5",
"memfree_mb": 3670,
"memtotal_mb": 3906,
"nodename": "raspi-4-1",
"os_family": "Debian",
"pkg_mgr": "apt",
"processor_cores": 1,
"processor_count": 4,
"processor_threads_per_core": 1,
"processor_vcpus": 4,
"product_name": "",
"product_serial": "",
"product_uuid": "",
"product_version": "",
"service_mgr": "systemd",
"uptime_seconds": 8359
}
}
ansible -i hosts all -m debug -a "var=hostvars[inventory_hostname]"
ansible -i hosts all -m setup -a "filter=architecture"

Define and use variables

- name: Retrieve hostname
hosts:
- raspis
- server
tasks:
- name: Retrieve the hostname
command: hostname
ansible-playbook playbook/retrieve_hostname.yml --limit raspisPLAY [Retrieve and show hostname of nodes] *****************************************************TASK [Gathering Facts] ********************************************************************************
ok: [raspi-4-2]
ok: [raspi-4-1]
ok: [raspi-3-2]
ok: [raspi-3-1]
TASK [Retrieve the hostname] **************************************************************************
changed: [raspi-4-1]
changed: [raspi-4-2]
changed: [raspi-3-1]
changed: [raspi-3-2]
PLAY RECAP ********************************************************************************************
raspi-3-1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
raspi-3-2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
raspi-4-1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
raspi-4-2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- name: Retrieve hostname
hosts:
- raspis
- server
tasks:
- name: Retrieve the hostname
command: hostname
register: result
- debug:
var: hostname
TASK [debug] ******************************************************************************************
ok: [raspi-3-1] => {
"result": {
"changed": true,
"cmd": [
"hostname"
],
"delta": "0:00:00.006215",
"end": "2020-02-23 10:35:52.757907",
"failed": false,
"rc": 0,
"start": "2020-02-23 10:35:52.751692",
"stderr": "",
"stderr_lines": [],
"stdout": "raspi-3-1",
"stdout_lines": [
"raspi-3-1"
]
}
}
- name: Retrieve hostname
hosts:
- raspis
- server
tasks:
- name: Retrieve the hostname
command: hostname
register: result
- set_fact:
hostname: {{ result.stdout }}
- debug:
var: hostname
TASK [debug] ******************************************************************************************
ok: [raspi-3-1] => {
"hostname": "raspi-3-1"
}
ok: [raspi-3-2] => {
"hostname": "raspi-3-2"
}
ok: [raspi-4-1] => {
"hostname": "raspi-4-1"
}
ok: [raspi-4-2] => {
"hostname": "raspi-4-2"
}

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store