# inventory inside play

**URL:** <https://forum.ansible.com/t/inventory-inside-play/34646>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [February 18, 2021, 6:45pm UTC](https://forum.ansible.com/t/inventory-inside-play/34646 "2021-02-18T18:45:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [February 18, 2021, 6:45pm UTC](https://forum.ansible.com/t/inventory-inside-play/34646/1 "2021-02-18T18:45:42Z")

</div>

I’m trying to deploy vms from template on esx with vmware\_guest module and this work great, but I would like to do inside this playbook some modification on vm after deployment. My inventory file is being populated with the same playbook with hosts that are being deployed, but after that I’m doing some modification, problem is that this hosts are being skipped because ansible doesn’t know for this hosts, setup module doesn’t do the job. How would I convinced ansible to reread inventory file?

Hear is last part of the playbook:

- name: add to inventoryfile  
lineinfile:  
dest: /home/ansible/vmware/inventory  
line: ‘{{ item.vm\_name }}’  
loop: ‘{{ vm }}’  
delegate\_to: localhost

- setup:

- name: Read device information  
community.general.parted: device=/dev/sda unit=MiB  
register: sda\_info  
when: inventory\_hostname in groups.deploy

- name: Extend an existing partition to fill all available space  
community.general.parted:  
device: /dev/sda  
number: “{{ sda\_info.partitions | length }}”  
part\_end: “100%”  
resize: true  
state: present  
when: inventory\_hostname in groups.deploy

- name: pv resize  
community.general.lvg:  
vg: vg\_root  
pvs: /dev/sda2  
pvresize: yes  
when: inventory\_hostname in groups.deploy

- name: fqdn  
hostname:  
name: “{{ ansible\_fqdn }}”  
when: inventory\_hostname in groups.deploy

Thanks

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 18, 2021, 7:06pm UTC](https://forum.ansible.com/t/inventory-inside-play/34646/2 "2021-02-18T19:06:44Z")

</div>

[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/meta\_module.html#parameter-free\_form](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/meta_module.html#parameter-free_form)

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [February 18, 2021, 7:49pm UTC](https://forum.ansible.com/t/inventory-inside-play/34646/3 "2021-02-18T19:49:50Z")

</div>

Hi,

Another way of doing…

You can also change the inventory live with some modules

[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add\_host\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html)

[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/group\_by\_module.html#ansible-collections-ansible-builtin-group-by-module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/group_by_module.html#ansible-collections-ansible-builtin-group-by-module)

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [February 19, 2021, 5:50pm UTC](https://forum.ansible.com/t/inventory-inside-play/34646/4 "2021-02-19T17:50:09Z")

</div>

Thanks

meta: refresh\_inventory doesn’t work inside vmware\_guest module, I have to do separate tasks directive for the configuration of vm’s after deployment in the playbook

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 19, 2021, 6:09pm UTC](https://forum.ansible.com/t/inventory-inside-play/34646/5 "2021-02-19T18:09:44Z")

</div>

I think that is how it is supposed to work…

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [February 19, 2021, 6:44pm UTC](https://forum.ansible.com/t/inventory-inside-play/34646/6 "2021-02-19T18:44:54Z")

</div>

it thought that should works something like this, for example:

- hosts: all  
vars\_files:  
tasks:
- name: create the VM  
vmware\_guest:  
…
- name: add to /etc/hosts file
- name: Refresh inventory  
meta: refresh\_inventory
- name: change something1 on new vm’s
- name: change something2 on new vm’s

but it doesn’t

this works:

- hosts: all  
vars\_files:  
tasks:

- name: create the VM  
vmware\_guest:  
…

- name: add to /etc/hosts file

- name: reconfigure other stuff  
hosts: deploy  
tasks:

- name: Refresh inventory  
meta: refresh\_inventory

- name: change something1 on new vm’s

- name: change something2 on new vm’s
