Reload Ansible's dynamic inventory

笑着哭i 提交于 2019-11-27 19:36:21
James Orenthal

With Ansible 2.0+, you can refresh your inventory mid-play by running the task:

- meta: refresh_inventory

I found the meta: refresh_inventory to be insufficient.
I had to add an explicit call to ec2.py --refresh-cache first.

- name: refresh inventory
  hosts: localhost
  connection: local
  gather_facts: False
  tasks:
    - name: Refresh EC2 cache
      command: /etc/ansible/ec2.py --refresh-cache
    - name: Refresh in-memory EC2 cache
      meta: refresh_inventory

Ansible currently doesn't support this. If you look at the source code of the ansible or ansible-playbook commands you'll see that the inventory is loaded first and then the inventory object is passed to the ansible command that runs the specified task or playbook. Moving the inventory processing so that it happens within the task/playbook handlers would probably be a pretty major undertaking for a number of reasons.

Your best bet when doing something like this is to simply break your playbook into two and wrap their calls in a shell script that you only have to invoke once.

You can also edit the ec2.ini file and set the option:

cache_max_age = 0

to prevent the need for reload by making sure that nothing is cached in the first place.

Take a look at add_host.

It does add a host (and alternatively a group) to the ansible-playbook in-memory inventory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!