Closed goetzk closed 4 years ago
To add some context, I have various tasks like this which use the code
- hosts: cc
tags: vm_setup
tasks:
- name: Add host to pxetool inventory
shell: |
pxetool set "{{ hostvars[item]['inventory_hostname'] }}" pxemac "{{ hostvars[item]['mac_address'] }}"
pxetool set "{{ hostvars[item]['inventory_hostname'] }}" pxeip "{{ hostvars[item]['ip_address'] }}"
pxetool set "{{ hostvars[item]['inventory_hostname'] }}" boot "{{ hostvars[item]['boot_os'] }}"
when: hostvars[item]['boot_os']|length > 0
with_items: "{{ groups['new_instances'] }}"
- name: Start VMs
command: virsh start "{{ item }}"
with_items: "{{ groups['new_instances'] }}"
ignore_errors: yes
Since submitting this PR I've begun to generate my new_instances group in a different way, an example of which is below. I may need the code from this PR one day if things change again but for now I will be closing it.
- hosts: localhost
tags: vm_setup,vm_puppetise
tasks:
- name: Fill with systems who's boot_os is specified
add_host:
name: "{{ item }}"
groupname: "new_instances"
# See also https://www.reddit.com/r/ansible/comments/c1ssl6/hide_skipped_hosts_for_one_task/ for a quieter way
with_items: "{{ groups['all'] }}"
when:
- ( hostvars[item]['boot_os'] |default('')) |length > 0
- new_instances is not defined
This allows following tasks to work with a group of hosts which have been reported as relevant instead of surmising the system state from initial configuration.