将主机添加到Ansible主机文件 [英] Adding hosts to Ansible host file
问题描述
我一直试图使用Ansible剧本将主机名添加到我的主机文件中.我的Ansible播放如下,我的主机文件位于/etc/ansible/hosts
:
I have been trying to add a host name to my hosts file using an Ansible playbook. My Ansible play look as below and my host file resides at /etc/ansible/hosts
:
- name: adding host playbook
hosts: localhost
connection: local
tasks:
- name: add host to ansible host file
add_host:
name: myvm.cloud.azure.com
groups: mymasters
Playbook成功执行,但是新的主机名未添加到Ansible主机文件中.有人可以帮我吗?
Playbook executes successfully, but the new host name is not added to the Ansible hosts file. Can anybody help me on this?
推荐答案
add_host
模块不会将主机添加到清单文件中,而是创建主机并将其添加到仅存在于内存中的清单中.您可以在以后的播放中使用此清单,但不会将其保存到文件中.
add_host
module does not add a host to your inventory file, but instead creates and adds a host to an inventory existing only in memory. You can use this inventory in subsequent plays, but it won't be saved to a file.
如果您真的想使用Ansible将主机添加到清单文件中,则需要使用常规的文件编辑模块,例如 blockinfile
.
If you really wanted to add a host to the inventory file with Ansible, you'd need to use a regular file-editing module, like lineinfile
or blockinfile
.
您还可以欺骗 inifile
模块来处理Ansible广告资源,但这是确实是一种黑客,因为库存文件实际上没有适当的INI文件结构:
You can also trick inifile
module to handle the Ansible inventory, but it's really a hack, as the inventory file does not really have a proper INI-file structure:
- ini_file:
dest: /etc/ansible/hosts
section: mymasters
option: myvm ansible_host
value: myvm.cloud.azure.com
no_extra_spaces: yes
这篇关于将主机添加到Ansible主机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!