在Ansible的vars_提示符中将变量用作默认值
我试图在Ansible中使用在Ansible的vars_提示符中将变量用作默认值,ansible,ansible-playbook,Ansible,Ansible Playbook,我试图在Ansible中使用vars\u prompt,默认值取自事实(或以前定义的变量)。playbook计划用作初始资源调配的临时手册 我的剧本: --- - hosts: server01 gather_facts: True vars_prompt: - name: new_hostname prompt: please enter the name for the target default: "{{ ansible_hostname }}"
vars\u prompt
,默认值取自事实(或以前定义的变量)。playbook计划用作初始资源调配的临时手册
我的剧本:
---
- 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中实现?这可以通过以下方式实现:
我打赌你在这里运气不好。在这个阶段没有主机变量-提示是为了整个游戏,在为任何主机完成任何任务(包括设置)之前向你提问。可能是
{{new_hostname | default(ansible_hostname)}
和-e
传递新主机名是一个解决方案。“此阶段没有主机变量”--问题是:此阶段是否有解释的变量(似乎任何变量都被视为字符串)。除此之外,我开始怀疑是否可以从j2模板本地创建一个动态剧本,并将其包括在内。。快速查看代码提示,剧本的这部分没有模板化。是的,这是正确的。我的意思是我将尝试创建一个剧本,该剧本将从j2模板本地创建另一个(短暂的)剧本(填充值)并从主剧本中运行此子剧本。是的,甚至我也遇到了相同的问题。在一个错误场景中,我希望获得用户输入是继续流程还是退出。但提示发生在执行任何任务之前。
---
- 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 }}"