# Create inventory in a Workflow and then use in subsequent playbooks

**URL:** <https://forum.ansible.com/t/create-inventory-in-a-workflow-and-then-use-in-subsequent-playbooks/38824>\
**Category:** Ansible Project\
**Tags:** azure, aap\
**Created:** [July 9, 2024, 3:52pm UTC](https://forum.ansible.com/t/create-inventory-in-a-workflow-and-then-use-in-subsequent-playbooks/38824 "2024-07-09T15:52:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Bachmeyer](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Chris\_Bachmeyer](https://forum.ansible.com/u/Chris_Bachmeyer)\
**Post date:** [July 9, 2024, 3:52pm UTC](https://forum.ansible.com/t/create-inventory-in-a-workflow-and-then-use-in-subsequent-playbooks/38824/1 "2024-07-09T15:52:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [July 9, 2024, 3:59pm UTC](https://forum.ansible.com/t/create-inventory-in-a-workflow-and-then-use-in-subsequent-playbooks/38824/2 "2024-07-09T15:59:38Z")

</div>

You need names and corresponding IP addresses for an inventory.

Walter

---

<div class="post-metadata">

**Author:** ![Chris\_Bachmeyer](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Chris\_Bachmeyer](https://forum.ansible.com/u/Chris_Bachmeyer)\
**Post date:** [July 10, 2024, 4:19pm UTC](https://forum.ansible.com/t/create-inventory-in-a-workflow-and-then-use-in-subsequent-playbooks/38824/3 "2024-07-10T16:19:34Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [July 10, 2024, 4:55pm UTC](https://forum.ansible.com/t/create-inventory-in-a-workflow-and-then-use-in-subsequent-playbooks/38824/4 "2024-07-10T16:55:19Z")

</div>

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
