Refresh dynamic inventory during playbook run

36 Views Asked by At

Is it possible to refresh inventory during ansible playbook execution?

I create AWS objects (VPC, subnets, security groups etc.) from scratch with playbook which uses plugin amazon.aws.aws_ec2 for dynamic host inventory.

When I run playbook, instances do not exist yet. Later I want to connect to freshly created instances, but ansible does not know about them.

I can run two playbooks consecutively in a script, then second one would query AWS and see instances, but is there a better way?

1

There are 1 best solutions below

0
Zeitounator On BEST ANSWER

After your hosts are created by your playbook, run the following task before using them elsewhere in a consecutive play:

- name: Refresh inventory from existing sources
  ansible.builtin.meta: refresh_inventory

Reference: Ansible meta module