inventory inside play

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

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/meta_module.html#parameter-free_form

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/group_by_module.html#ansible-collections-ansible-builtin-group-by-module

Regards,

JYL

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

I think that is how it is supposed to work…

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