如何在 Ansible 中提示用户输入目标主机? [英] How to prompt user for a target host in Ansible?
问题描述
我想为 Ansible 中的新机器编写一个引导程序剧本,它将重新配置网络设置.在第一次执行时,目标机器会有 DHCP 分配的地址.
I want to write a bootstrapper playbook for new machines in Ansible which will reconfigure the network settings. At the time of the first execution target machines will have DHCP-assigned address.
应该执行剧本的用户知道分配给新机器的 IP 地址.我想提示用户输入值.
The user who is supposed to execute the playbook knows the assigned IP address of a new machine. I would like to prompt the user for is value.
vars_prompt
模块允许从用户那里获取输入,但是它在 hosts
部分下定义有效地防止主机地址作为所需的值.
vars_prompt
module allows getting input from the user, however it is defined under hosts
section effectively preventing host address as the required value.
是否可以不使用包装脚本修改库存文件?
Is it possible without using a wrapper script modifying inventory file?
推荐答案
正确的做法是使用 add_host 创建一个动态主机,并将其放置在一个新组中,然后针对该组开始新的播放.这样,如果您有其他需要提前设置的连接变量(凭据/密钥/等),您可以将它们设置在清单中的一个空组上,然后将主机动态添加到其中.例如:
The right way to do this is to create a dynamic host with add_host and place it in a new group, then start a new play that targets that group. That way, if you have other connection vars that need to be set ahead of time (credentials/keys/etc) you could set them on an empty group in inventory, then add the host to it dynamically. For example:
- hosts: localhost
gather_facts: no
vars_prompt:
- name: target_host
prompt: please enter the target host IP
private: no
tasks:
- add_host:
name: "{{ target_host }}"
groups: dynamically_created_hosts
- hosts: dynamically_created_hosts
tasks:
- debug: msg="do things on target host here"
这篇关于如何在 Ansible 中提示用户输入目标主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!