Ansible deployment script, variable hosts value

75 views
Skip to first unread message

Rob Wilkerson

unread,
Oct 7, 2015, 9:53:50 AM10/7/15
to Ansible Project
I'm tipping my toe in the Ansible pool via a simple project deployment script. I have 3 environments (the usual dev, staging, production) and the deployment is exactly the same for each. In every example I've seen so far, the `hosts` are hard coded in the playbook. What I'd like to do is create a single `deploy.yml` file and specify the `hosts` value when I call it. Otherwise, at least in my limited understanding so far, we'll have to either modify the playbook every time we want to run a deployment against one of our environments.

I've seen something like this:

$ ansible deploy.yml -e "hosts=development"

where, in `deploy.yml`, we have `hosts: $hosts`, but that feels ham-fisted.

Is this the recommended/best approach? Should I be structuring things differently?

J Hawkesworth

unread,
Oct 7, 2015, 10:08:08 AM10/7/15
to Ansible Project
The value for hosts in the playbook can be a group of hosts (defined in your inventory).

I'd suggest setting up different inventory files for each environment then you can run

ansible-playbook -i /etc/ansible/dev-inventory deploy.yml
or
ansible-playbook -i /etc/ansible/staging-inventory deploy.yml

Jon

Rob Wilkerson

unread,
Oct 7, 2015, 1:47:32 PM10/7/15
to Ansible Project
Okay, that makes sense. In that scenario, I assume that deploy.yml just specifies hosts: all which would pick up everything in that inventory file, right?

Thanks!

Dan Poirier

unread,
Oct 7, 2015, 2:59:03 PM10/7/15
to Ansible Project
Or when things get more complicated, you can define groups like "webservers" and have playbooks apply to the relevant group.

Alex Leonhardt

unread,
Oct 7, 2015, 5:15:26 PM10/7/15
to ansible...@googlegroups.com
Are you using AWS or any other cloud provider ? I'd suggest using dynamic inventory then instead as it'll be a pain later to keep updating names / IPs / etc. etc. 


--
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/0e133f33-710b-4f00-8620-de33855a61b0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

gpg public key: http://dpaste.com/1CEJ38Z 
Reply all
Reply to author
Forward
0 new messages