What is prompt variable working scope?

1,143 views
Skip to first unread message

rell...@gmail.com

unread,
Apr 11, 2013, 9:43:45 PM4/11/13
to ansible...@googlegroups.com
Prompt variable working scope is inventory host scope or playbook scope?
Basically, variable is inventory host scope. But prompt vars seem not.

main.yml includes 1.yml and 2.yml.
1.yml creates prompt variable var1 and its target hosts are host1 and host2.
2.yml wants to use the var1 and its target host is host1.

I suppose 2.yml can get var1, but indeed not. Who can confirm it?

main.yml
---
- include: 1.yml
- include: 2.yml


1.yml
---
- name: test prompt
  hosts: test
  gather_facts: false
  user: root
  sudo: false
  vars_prompt:
  - name: "password"
    prompt: "Please enter password"
    private: yes
    confirm: yes

  tasks:
  - action: debug msg="$password"
  - action: shell echo -n $password

2.yml
---
- hosts: 192.168.126.143
  user: root
  sudo: false

  tasks:
  - action: shell echo -n $password

  - action: template src=template.conf dest=/root/out.conf owner=root group=root mode=0644

template.conf
{{ password }}

playbook execute result:
PLAY [test prompt] ********************* 

TASK: [debug msg="123"] ********************* 
ok: [192.168.126.143]
ok: [192.168.126.150]

TASK: [shell echo -n 123] ********************* 
changed: [192.168.126.150]
changed: [192.168.126.143]

PLAY [192.168.126.143] ********************* 

GATHERING FACTS ********************* 
ok: [192.168.126.143]

TASK: [shell echo -n $password] ********************* 
changed: [192.168.126.143]

TASK: [template src=template.conf dest=/root/out.conf owner=root group=root mode=0644] ********************* 
ok: [192.168.126.143]

PLAY RECAP ********************* 
192.168.126.143                : ok=5    changed=2    unreachable=0    failed=0    
192.168.126.150                : ok=2    changed=1    unreachable=0    failed=0 

In out.conf, there is nothing.



Michael DeHaan

unread,
Apr 12, 2013, 9:49:37 AM4/12/13
to ansible...@googlegroups.com
vars_prompt variables are set on the play.

In nearly all cases, --extra-vars on the ansible-playbook command line is a better way to go.









--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Garrett Plasky

unread,
Mar 25, 2014, 6:54:03 PM3/25/14
to ansible...@googlegroups.com
Can you explain why you think this is a "better way to go" in nearly all cases? I recently found out the hard way (after a bit of head-banging) that vars_prompt only applies to the current play and also doesn't apply to included playbooks. Since you also cannot pass parameterized options to included playbooks as you can with roles, there's effectively no way to pass a variable gathered via vars_prompt to a child playbook without using -e. I've resorted to using -e for now, but I find it rather odd that I now have to wrap Ansible in "something else" (Shell script, Python, etc.) in order to programmatically input a simple variable to the playbook and have it accessible in places one could reasonably expect it to be.

Michael DeHaan

unread,
Mar 26, 2014, 7:41:45 PM3/26/14
to ansible...@googlegroups.com
On Tue, Mar 25, 2014 at 6:54 PM, Garrett Plasky <gpl...@evernote.com> wrote:
Can you explain why you think this is a "better way to go" in nearly all cases?


For one, because it can be used non-interactively.   For two, for the question you raise below...

 
I recently found out the hard way (after a bit of head-banging) that vars_prompt only applies to the current play and also doesn't apply to included playbooks.


This is implied by the fact that vars_prompt is an attribute of the play.    It's just the same as if doing "vars" or "vars_files" in that play.



 
Since you also cannot pass parameterized options to included playbooks as you can with roles, there's effectively no way to pass a variable gathered via vars_prompt to a child playbook without using -e. I've resorted to using -e for now, but I find it rather odd that I now have to wrap Ansible in "something else" (Shell script, Python, etc.) in order to programmatically input a simple variable to the playbook and have it accessible in places one could reasonably expect it to be.

Sorry!

There's no construct for asking questions at "overall playbook" level since playbooks are just a list data structure of plays, there is no where to attach this.

We could add a 'vars_prompt_global' kind of thing that left them in global scope, but I'd be worried a bit more about language sprawl, so you may be stuck.
 
Reply all
Reply to author
Forward
0 new messages