我只想更新清单文件/etc/ansible/hosts中的一个条目,这取决于某些条件(例如,网络属性的变化)。根据清单文件的这段代码,我想更新[south_side_hosts]下的条目。有没有办法,用ansible来更新这个文件?我可以写一个python脚本来解析和更新文件,但我希望能用ansible找到一个解决方案。
[south_side_hosts]
sshost.eng.corp.com
[south_side_ips]
192.168.100.2
[num_hosts]
83发布于 2019-04-30 03:36:06
清单文件的格式为INI,如in the documentation所述。
这样ini_file module就可以工作了。使用allow_no_value: true和两个任务删除旧的“选项”并添加新的“选项”:
- name: Remove host from 'south_side_hosts' group
ini_file:
path: /etc/ansible/hosts
section: south_side_hosts
option: sshost.eng.corp.com
state: absent
- name: Add host to 'south_side_hosts' group
ini_file:
path: /etc/ansible/hosts
section: south_side_hosts
option: sshost2.eng.corp.com
allow_no_value: true如果您想从同一行动手册中配置新主机,则需要在此之后刷新清单:
- name: Refresh the inventory
meta: refresh_inventory请注意,如果您打算使用在命令行传递的随机主机名执行此操作,那么从长远来看,动态清单可能确实是您所需要的。
https://stackoverflow.com/questions/55908213
复制相似问题