如何在Ansible中提示用户输入目标主机? [英] How to prompt user for a target host in Ansible?

查看:94
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆