Create inventory in a Workflow and then use in subsequent playbooks

32 views
Skip to first unread message

HBach

unread,
Jul 9, 2024, 11:52:05 AMJul 9
to Ansible Project
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://<redacted>/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://<redacted>/"
    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?

Rowe, Walter P. (Fed)

unread,
Jul 9, 2024, 12:00:09 PMJul 9
to 'Felix Fontein' via Ansible Project
You need names and corresponding IP addresses for an inventory.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/aa0fcd58-3f55-4c9a-8ea6-2d0e28fa3495n%40googlegroups.com.

HBach

unread,
Jul 10, 2024, 12:19:34 PMJul 10
to Ansible Project
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!

Rowe, Walter P. (Fed)

unread,
Jul 10, 2024, 12:55:39 PMJul 10
to 'Felix Fontein' via Ansible Project
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
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123
Reply all
Reply to author
Forward
0 new messages