在Ansible中使用变量作为vars_prompt中的默认值

时间:2016-07-28 20:14:22

标签: ansible ansible-playbook

我试图在Ansible中使用vars_prompt,其默认值取自事实(或以前定义的变量)。该剧本旨在用作初始配置的临时用途。

我的剧本:

---
- 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中实现?

1 个答案:

答案 0 :(得分:3)

这可以使用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 }}"
相关问题