重新加载 Ansible 的动态库存 [英] Reload Ansible's dynamic inventory
问题描述
我正在使用 Ansible 设置 EC2 实例并部署应用程序.有一个主机脚本,用于收集与标签相关的服务器和组信息.我想将这些操作作为一个剧本来运行,所以
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
- 根据需要创建新实例
- Hosts 脚本加载库存(包括服务器的事实)
- 部署手册有效
但是,库存是提前加载的,因此如果在播放期间创建/更新服务器,则没有服务器/组数据.我可以 1) 将供应和部署剧本分开 2) 使用 add_host 技巧在服务器更新时模拟动态库存,但这些方法存在缺陷.
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.
我可以强制 Ansible 重新加载库存吗?我的测试文件是:hosts
脚本:
Can I force Ansible to reload inventory? My test files are:
hosts
script:
#!/bin/sh
echo `date` >> log.log
echo "{\"standalone\":[\"localhost\"]}"
示例playbook.yml
:
---
- hosts: all
tasks:
- name: show inventory_hostname
command: echo {{ inventory_hostname }}
我运行它 ansible-playbook -i hosts playbook.yml -v
并看到 两次 运行:
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.
推荐答案
借助 Ansible 2.0+,您可以通过运行 任务:
With Ansible 2.0+, you can refresh your inventory mid-play by running the task:
- meta: refresh_inventory
这篇关于重新加载 Ansible 的动态库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!