Ansible - 使用用户输入选择变量 [英] Ansible - Using user input to chose a variable
问题描述
我想根据 Ansible 剧本中的用户输入选择特定变量.具体来说,我想在服务器的位置上请求用户输入,然后根据输入执行特定操作.
I would like to select a specific variable based on user input in an Ansible playbook. Specifically, I would like to ask for user input on a location of a server, and then execute a specific action based on the input.
这是当前的 ansible 剧本:
This is the current ansible playbook:
- hosts: all
remote_user: root
gather_facts: True
vars:
loc1: "10.13.1.140"
loc2: "10.13.1.141"
loc3: "10.13.1.142"
vars_prompt:
- name: location
prompt: "Location of server? Input options: loc1/loc2/loc3"
private: no
tasks:
- name: Test connectivity to user selected location
wait_for: host={{ vars.location }} port=9999 delay=0 timeout=10 state=started
运行剧本时的输出:
[root@ansmgtpr-labc01 cfengine]# ansible-playbook testpoo.yaml -i /tmp/test
SSH password:
Location of server? Input options: loc1/loc2/loc3: loc2
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [hostname.domain.com]
TASK [Test connectivity to user selected location] *****************************
fatal: [hostname.domain.com]: FAILED! => {"changed": false, "elapsed": 10, "failed": true, "msg": "Timeout when waiting for loc2:9999"}
PLAY RECAP *********************************************************************
hostname.domain.com : ok=1 changed=0 unreachable=0 failed=1
我想知道如何或最佳方式将位置的读入用户输入与在变量部分顶部定义的位置的实际值(IP 地址)相关联.可能是 eval 还是其他什么?
I would like to know how or the best way to link the read-in user input of the location with the actual value (IP address) of the location that is defined at the top in the variables section. Possibly eval or something else?
推荐答案
您的任务正在等待 loc2
,因此消息 Timeout when waiting for loc2:9999
.
Your task is waiting for loc2
, hence the message Timeout when waiting for loc2:9999
.
使用 host={{ vars[location] }}
代替.
比较以下任务的输出:
tasks:
- name: Show the value user entered
debug: var=vars.location
- name: Use the entered value as an index
debug: var=vars[location]
结果(缩写):
TASK [Show the value user entered] *********************************************
ok: [localhost] => {
"vars.location": "loc2"
}
TASK [Use the entered value as an index] ***************************************
ok: [localhost] => {
"vars[location]": "10.13.1.141"
}
这篇关于Ansible - 使用用户输入选择变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!