Inventory vars in a playbook

72 views
Skip to first unread message

Jeremy Gailor

unread,
Aug 27, 2014, 3:46:58 PM8/27/14
to ansible...@googlegroups.com
Can anyone explain to me if this just doesn't work or if I'm doing something wrong;

/production
[webapps:vars]
default_branch=master

/staging
[webapps:vars]
default_branch=staging

/my_playbook.yml
- hosts: webapps
  vars_prompt:
    - name: "release_branch"
      prompt: "Deploy Branch:"
      default: "{{default_branch}}"
      

Basically is just takes the text in 'default' verbatim (i.e. - no substitution).  Given how Ansible puts itself into the deployment process, and how well that actually works, this is a really common use case that it feels like should be supported.

Michael DeHaan

unread,
Aug 27, 2014, 4:14:05 PM8/27/14
to ansible...@googlegroups.com
vars_prompt variables aren't being expanded with inventory scoped variables, because the question is only going to be asked once.  Therefore it's storing the literal string value.

I really don't recommend relying on vars_prompt because playbooks really should be non-interative, "-e" extra variables also work a bit more nicely.  You can still define defaults if the variables are not there using the "| default" Jinja2 function.





--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/90b98beb-1b0a-4023-9026-c845d3f5c03d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeremy Gailor

unread,
Aug 27, 2014, 4:24:15 PM8/27/14
to ansible...@googlegroups.com, Michael DeHaan
So I’m actually using a when: condition to see it’s passed on the command line first, but —extra-args feels very clunky for specifying a branch when you want to release, and it is often the case that you have a specific branch you need to run in a particular environment, usually for testing.

- Jeremy 
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/F_xJywENOUY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
Aug 27, 2014, 4:31:33 PM8/27/14
to Jeremy Gailor, ansible...@googlegroups.com
You could also set it in a group variables file, for the staging or production group.

-e overrides inventory scope, so could be used to replace the default.

Alternatively, keep the vars prompt and do this in the task section after prompting for the variable I have named "prompted_variable", while setting the default prompt in the vars_prompt to just ''.

- set_fact: actual_branch={{ prompted_variable | default(variable_from_inventory) }} 

Jeremy Gailor

unread,
Aug 27, 2014, 4:32:48 PM8/27/14
to ansible...@googlegroups.com, mic...@ansible.com
To clarify, the full block is:

/my_playbook.yml
- hosts: webapps
  vars_prompt:
    - name: "release_branch"
      prompt: "Deploy Branch:"
      default: master
      when: not release_branch

Ansible will skip this block if release_branch is passed in --extra-args.  Thinking it through, maybe it's worth just having it passed in on the command line if it needs to be set and falling back to the inventory as a default.  Thanks for the help.

Michael DeHaan

unread,
Aug 27, 2014, 4:37:09 PM8/27/14
to Jeremy Gailor, ansible...@googlegroups.com
Yep, sounds good.

IIRC, "when" cannot be applied to a vars_prompt.



Jeremy Gailor

unread,
Aug 27, 2014, 4:38:57 PM8/27/14
to Michael DeHaan, ansible...@googlegroups.com
That is pretty weird, because “when” I tested it this morning I swear it skipped the prompt when I passed in —extra-vars “release_branch=staging”. 

- Jeremy 

From: Michael DeHaan <mic...@ansible.com>
Reply: Michael DeHaan <mic...@ansible.com>>
Date: August 27, 2014 at 1:37:06 PM
To: Jeremy Gailor <jga...@gmail.com>>
Cc: ansible...@googlegroups.com <ansible...@googlegroups.com>>
Subject:  Re: [ansible-project] Inventory vars in a playbook

Reply all
Reply to author
Forward
0 new messages