# Ansible deployment script, variable hosts value

**URL:** <https://forum.ansible.com/t/ansible-deployment-script-variable-hosts-value/19455>\
**Category:** Ansible Project\
**Created:** [October 7, 2015, 12:23pm UTC](https://forum.ansible.com/t/ansible-deployment-script-variable-hosts-value/19455 "2015-10-07T12:23:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_Wilkerson](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@Rob\_Wilkerson](https://forum.ansible.com/u/Rob_Wilkerson)\
**Post date:** [October 7, 2015, 12:23pm UTC](https://forum.ansible.com/t/ansible-deployment-script-variable-hosts-value/19455/1 "2015-10-07T12:23:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 7, 2015, 2:08pm UTC](https://forum.ansible.com/t/ansible-deployment-script-variable-hosts-value/19455/2 "2015-10-07T14:08:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rob\_Wilkerson](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@Rob\_Wilkerson](https://forum.ansible.com/u/Rob_Wilkerson)\
**Post date:** [October 7, 2015, 5:47pm UTC](https://forum.ansible.com/t/ansible-deployment-script-variable-hosts-value/19455/3 "2015-10-07T17:47:32Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Dan\_Poirier](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dan\_Poirier](https://forum.ansible.com/u/Dan_Poirier)\
**Post date:** [October 7, 2015, 6:25pm UTC](https://forum.ansible.com/t/ansible-deployment-script-variable-hosts-value/19455/4 "2015-10-07T18:25:23Z")

</div>

Or when things get more complicated, you can define groups like “webservers” and have playbooks apply to the relevant group.

---

<div class="post-metadata">

**Author:** ![Alex\_Leonhardt](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Alex\_Leonhardt](https://forum.ansible.com/u/Alex_Leonhardt)\
**Post date:** [October 7, 2015, 9:14pm UTC](https://forum.ansible.com/t/ansible-deployment-script-variable-hosts-value/19455/5 "2015-10-07T21:14:59Z")

</div>

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.

[https://docs.ansible.com/ansible/intro\_dynamic\_inventory.html](https://docs.ansible.com/ansible/intro_dynamic_inventory.html)

Alex
