Hi, community,
I am creating a deployment workflow using the Ansible Automation Platform (AAP) and, right now, the workflow is composed of two job templates. The first job template creates the actual vms and the the second job template is used for some post-build configuration tasks. I want to use the inventory generated in the first job template for the second job template.
Right now, I am having AAP reach back to itself via the AAP API and create an inventory. I have also used the set_stats module to create an artifact that the second job template can see.
-
name: Set stats to pass inventory
ansible.builtin.set_stats:
data:
target_hosts: “{{ linux_hosts }}”
-
name: Get an AAP API Token and store it as new_token
register: new_token
ansible.builtin.uri:
url: “https:///api/v2/tokens/”
method: POST
user: admin
password: “{{ survey_controller_passwd }}”
validate_certs: false
force_basic_auth: true
status_code: 201
return_content: true
-
name: Azure inventory update
ansible.controller.host:
controller_host: “https:///”
controller_password: “{{ survey_controller_passwd }}”
controller_username: ‘admin’
controller_oauthtoken: “{{ new_token.json.token }}”
name: “{{ linux_hosts }}”
inventory: “Azure-test”
state: present
enabled: true
The artifact is created and the new inventory on AAP is updated when job template 1 completes.
{
“target_hosts”: [
“rheltest0701c”
]
}
But, the second job template fails as it cannot find the host(s).
[WARNING]: Could not match supplied host pattern, ignoring: target_hosts
Any idea, folks?
You need names and corresponding IP addresses for an inventory.
Walter
Thanks, Walter.
We’re not using static addresses but dynamic. If we were, it would be easier. I figured out the Ansible fact to grab the IP but this only grabs it for a single host.
ansible_facts[“azure_vm”][“network_profile”][“network_interfaces”][0][“properties”][“ip_configurations”][0][“private_ip_address”]
I wrote a simple task to loop through the host list provided in the Ansible survey to dump the ips to stdout.
- name: Gather specific Ansible facts
debug:
msg: “{{ hostvars[item][‘ansible_default_ipv4’][‘address’] }}”
loop: “{{ linux_hosts }}”
but it looks like it can’t find the host(s).
“msg”: "The task includes an option with an undefined variable. The error was: "hostvars[‘rheltest0709a’]" is undefined.
Thanks!
Do you need these plays to be in separate playbook? You can put multiple plays in a single playbook. Play one can create an inventory group. Play two can run agains that group. We use this for some of our tasks where we create new machines, create a new inventory group, then run a play on that new group.
play 1: add new machines to an inventory group
- name: Add new host to playbook inventory
hosts: localhost
gather_facts: no
tasks:
add our new host to the inventory for this play
- name: add {{ item | lower }} to group
add_host:
name: “{{ item | lower }}”
group: newvm
loop: “{{ new_vm_names }}”
play 2: customize new machines
- name: “Customize Server - Linux - Set Time Zone, Hostname, Join AD”
gather_facts: no
hosts: newvm
Notice that “hosts” in play #2 refers to the “newvm” group created in play #1.
Walter