本文介绍了在Ansible中的vars_prompt中使用变量作为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在Ansible中使用vars_prompt
,并将默认值从事实中提取(或从以前定义的变量中获取).该剧本旨在用作初始配置的临时书.
I was trying to use vars_prompt
in Ansible with default values taken from facts (or otherwise a previously defined variable). The playbook is intended be used as an ad-hoc one for initial provisioning.
我的剧本:
---
- hosts: server01
gather_facts: True
vars_prompt:
- name: new_hostname
prompt: please enter the name for the target
default: "{{ ansible_hostname }}"
private: no
tasks:
- debug: msg="{{ new_hostname }}"
当前结果:
please enter the name for the target [{{ ansible_hostname }}]:
ERROR! 'ansible_hostname' is undefined
预期结果(假设ansible_hostname=server01
:
please enter the name for the target [server01]:
可以在Ansible中实现吗?
Is it possible to achieve in Ansible?
推荐答案
可以使用 pause
模块:
This can be implemented using the pause
module:
---
- hosts: server01
gather_facts: True
tasks:
- pause:
prompt: please enter the name for the target [{{ ansible_hostname }}]
register: prompt
- debug:
msg: "{{ prompt.user_input if prompt.user_input else ansible_hostname }}"
这篇关于在Ansible中的vars_prompt中使用变量作为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!