Skip to content

Commit c799f41

Browse files
committed
Add parsing external inventory file and add host
Sometimes, the VMs on which action would be done are not available when the main Ansible playbook is executed. In that case, to parse the new inventory file use `inventory_file.yml` task, then you would be able to use delegation to execute tasks on new host. Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 008f26f commit c799f41

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

roles/cifmw_helpers/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,22 @@ To parse them and set as a fact, use `various_vars.yml` task file.
190190
"Value for file is: {{ cifmw_repo_setup_os_release }}"
191191
"Value for dict is: {{ test }}"
192192
```
193+
194+
#### Parse inventory file and add it to inventory
195+
196+
Sometimes, the VMs on which action would be done are not available when the
197+
main Ansible playbook is executed. In that case, to parse the new inventory file
198+
use `inventory_file.yml` task, then you would be able to use delegation to
199+
execute tasks on new host.
200+
201+
```yaml
202+
- name: Test parsing additional inventory file
203+
hosts: localhost
204+
tasks:
205+
- name: Read inventory file and add it using add_host module
206+
vars:
207+
include_inventory_file: vms-inventory.yml
208+
ansible.builtin.include_role:
209+
name: cifmw_helpers
210+
tasks_from: inventory_file.yml
211+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
- name: Read inventory file
3+
ansible.builtin.slurp:
4+
src: "{{ include_inventory_file }}"
5+
register: _inventory_file
6+
7+
- name: Parse inventory file content
8+
ansible.builtin.set_fact:
9+
inventory_data: "{{ _inventory_file.content | b64decode | from_yaml }}"
10+
11+
- name: Process each group with hosts
12+
ansible.builtin.include_tasks:
13+
file: parse_inventory.yml
14+
loop: "{{ inventory_data | dict2items | selectattr('value.hosts', 'defined') | list }}"
15+
loop_control:
16+
loop_var: group_item
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
- name: "Add hosts for group {{ group_item.key }}"
3+
ansible.builtin.add_host:
4+
name: "{{ host_item.key }}"
5+
groups: "{{ group_item.key }}"
6+
ansible_host: "{{ host_item.value.ansible_host | default(omit) }}"
7+
ansible_ssh_common_args: "{{ host_item.value.ansible_ssh_common_args | default(omit) }}"
8+
ansible_ssh_private_key_file: "{{ host_item.value.ansible_ssh_private_key_file | default(omit) }}"
9+
ansible_user: "{{ host_item.value.ansible_user | default(omit) }}"
10+
ansible_connection: "{{ host_item.value.ansible_connection | default(omit) }}"
11+
cifmw_hypervisor_host: "{{ host_item.value.cifmw_hypervisor_host | default(omit) }}"
12+
loop: "{{ group_item.value.hosts | dict2items }}"
13+
loop_control:
14+
loop_var: host_item

0 commit comments

Comments
 (0)