I've had to deal with a little bit of automation for foreman / satellite in which we created hosts and also set them up for building them (build: true).
Everything works for new hosts but there's a small problem for already existing hosts. With that 'build: true' a new build token will be generated and at the next reboot, hosts would be kickstarted using pxe.
The solution is simple, get the changed state of every item in the loop and if 'changed' == true, then build the host. Turns out it was a bit more complicated than I expected since the state of the task will be changed if one item in it is different.
Please note that I will be defining all variables in environment-specific inventories.
So here's my solution to conditionally build hosts depending if their state in managing them was changed or not. Also, check the indentation before running this.
Please leave feedback if this helped you or if this could have been done more elegant in a way which I didn't think of.
Everything works for new hosts but there's a small problem for already existing hosts. With that 'build: true' a new build token will be generated and at the next reboot, hosts would be kickstarted using pxe.
The solution is simple, get the changed state of every item in the loop and if 'changed' == true, then build the host. Turns out it was a bit more complicated than I expected since the state of the task will be changed if one item in it is different.
Please note that I will be defining all variables in environment-specific inventories.
So here's my solution to conditionally build hosts depending if their state in managing them was changed or not. Also, check the indentation before running this.
---
- name: Manage foreman hosts
hosts: foreman
gather_facts: false
tasks:
- name: manage foreman hosts
delegate_to: localhost
become: false
loop: "{{ foreman_hosts }}"
foreman_host:
name: "{{ item.name }}"
state: "{{ item.state }}"
hostgroup: "{{ item.hostgroup }}"
location: "{{ item.location }}"
organization: "{{ item.organization }}"
server_url: "{{ foreman_connect.server }}"
username: "{{ foreman_connect.username }}"
password: "{{ foreman_connect.password }}"
validate_certs: "{{ foreman_connect.validate }}"
register: create_hosts
#- debug: var=create_hosts.results
- name: build changed foreman hosts
delegate_to: localhost
become: false
loop: "{{ create_hosts.results }}"
when: item.changed == True
foreman_host:
name: "{{ item.item.name }}"
state: "{{ item.item.state }}"
hostgroup: "{{ item.item.hostgroup }}"
location: "{{ item.item.location }}"
organization: "{{ item.item.organization }}"
build: true
server_url: "{{ foreman_connect.server }}"
username: "{{ foreman_connect.username }}"
password: "{{ foreman_connect.password }}"
validate_certs: "{{ foreman_connect.validate }}"
- name: power on defined hosts
delegate_to: localhost
become: false
loop: "{{ foreman_hosts }}"
foreman_host_power:
name: "{{ item.name }}"
state: "{{ item.powerstate }}"
server_url: "{{ foreman_connect.server }}"
username: "{{ foreman_connect.username }}"
password: "{{ foreman_connect.password }}"
validate_certs: "{{ foreman_connect.validate }}"
...
Please leave feedback if this helped you or if this could have been done more elegant in a way which I didn't think of.
Comments
Post a Comment