Vars_prompt in playbooks

2019-08-01 11:09发布

问题:

This is my playbook,

---
- hosts: alpha
  vars:
  company: vogo
  tasks:
     - name: debugging
       debug:
         msg: "{{ansible_hostname}}"
  vars_prompt:
     - name: "company"
       prompt: "Where do you work"
       private: no

- hosts: webservers
  vars_prompt:
     - name: "fathercompany"
       prompt: "Where your father works"
       private: no
  tasks:
     - name: test
       debug:
         msg: just testing "{{company2}}"

Here are the steps of execution flow when I run the playbook,

1 - prompt 1 (Where do you work)

2 - task debugging

3 - prompt 2 (Where your father works)

4 - task test

I have some questions.

First, When I run this playbook, the task "debugging" should run first and then the prompt should ask for the company name. But, when I run this playbook, at the very first step, it asks for "Where do you work ?". Why prompt first ? Am I missing some kind of paramater which I should have passed ?

Second, I have put 2 prompts here,

I want to use the prompt value company, in the webservers host block. But it gives me error when I try to do that.

Cannot I use the prompt value from one host block into another ?

Third,

How can I use prompts in roles ?

回答1:

vars_prompt are bound to specific play, so you can't use them in other plays directly. Though you can use set_fact in the first play to set a fact for some host, and access it in later plays via hostvars.

Prompts are executed in the beginning of every play, it doesn't matter where you place vars_prompt block - before or after tasks section (it is a YAML dictionary after all, where sequence of keys doesn't mean anything).

I'd suggest not to use prompts at all, if you need some external data, pass it via extra variables.



标签: ansible