Reload Ansible's dynamic inventory

2019-01-14 00:28发布

I'm using Ansible to setup EC2 instances and deploy an app. There's a hosts script which gathers tags related servers and groups info. I'd like to run these actions as a single playbook, so

  1. New instances are created if needed
  2. Hosts script loads inventory (including servers' facts)
  3. Deployment playbook works

However, inventory loaded in advance so there is no servers/groups data if servers created/updated during the play. I can 1) separate provision and deployment playbooks 2) use add_host trick to emulate dynamic inventory when servers are updated, but there are drawbacks in those approaches.

Can I force Ansible to reload inventory? My test files are: hosts script:

#!/bin/sh
echo `date` >> log.log
echo "{\"standalone\":[\"localhost\"]}"

Sample playbook.yml:

---
- hosts: all
  tasks:
    - name: show inventory_hostname
      command: echo {{ inventory_hostname }}

I run it ansible-playbook -i hosts playbook.yml -v and see two runs:

$> cat log.log
Thu Mar 12 09:43:16 SAMT 2015
Thu Mar 12 09:43:16 SAMT 2015

but I haven't found a command to double it.

4条回答
贪生不怕死
2楼-- · 2019-01-14 00:39

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

- meta: refresh_inventory
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-14 00:46

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.

查看更多
Rolldiameter
4楼-- · 2019-01-14 00:51

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
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-14 00:57

Take a look at add_host.

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

查看更多
登录 后发表回答