'How to loop over simple inventory on Ansible?
I have the following Ansible inventory:
[UP]
u01
u02
I want print hostnames under the UP group and then store each hostname in a variable
I tried the following playbook with the command: ansible-playbook test_playbook.yml -i inventory
---
- name: Test
hosts: localhost
tasks:
- name: Show all the hosts in the inventory
ansible.builtin.debug:
msg: "{{ item }}"
loop: "{{ groups['UP'] }}"
register: result
I'm getting the following errors instead:
TASK [Show all the hosts in the inventory] *************************************
failed: [localhost] (item=u01) => {"ansible_loop_var": "item", "changed": false, "module_stderr": "", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 0, "item": "u01"}
failed: [localhost] (item=u02) => {"ansible_loop_var": "item", "changed": false, "module_stderr": "", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 0, "item": "u02"}
I can't seem to find the error to correct it, does anyone have any clue ?
Thank you!
Solution 1:[1]
---
- name: Test
hosts: UP
tasks:
- name: Show all the hosts in the inventory
ansible.builtin.debug:
msg: "{{ item }}"
register: result
delegate_to: localhost
run_once: true
with_inventory_hostnames:
- UP
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KrishnaR |
