# Scoping of playbooks

**URL:** <https://forum.ansible.com/t/scoping-of-playbooks/22831>\
**Category:** Ansible Project\
**Created:** [October 19, 2016, 7:10pm UTC](https://forum.ansible.com/t/scoping-of-playbooks/22831 "2016-10-19T19:10:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aenagy](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@aenagy](https://forum.ansible.com/u/aenagy)\
**Post date:** [October 19, 2016, 7:10pm UTC](https://forum.ansible.com/t/scoping-of-playbooks/22831/1 "2016-10-19T19:10:38Z")

</div>

My first Ansible project is configuring the DNS client of our Linux machines. This configuration is different depending on the managed node’s location within our network (west dmz, west non-dmz, east dmz, east non-dmz). I’m trying to figure out how to scope my playbook so that each location has its own configuration applied. We don’t have Ansible Tower and I’m not sure if we are getting it. I am confident that I can create the logic to determine the location based on the managed node’s default gateway. Our environment is approaching 2000+ systems. So, I’m wondering:

1. I’m assuming (hoping) that there is a way to do this in a single playbook using some kind of conditional like “switch/caseof server\_location”. How do I do this?

2. It is possible that this configuration information is stored in a text file external to the playbook?

3. The thought of creating and managing inventory files for all of this seems rather daunting. Is there a better way to create & manage an inventory of this size assuming that our organization is not willing to pay for Ansible Tower? Would this be dynamic inventory via some kind of vSphere/vCenter plugin?

Thanks.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 20, 2016, 4:54am UTC](https://forum.ansible.com/t/scoping-of-playbooks/22831/2 "2016-10-20T04:54:19Z")

</div>

Many ways to do this, I recommend using different groups for each and then assigning variables with the different configurations, use those variables in the plays.

To store this data ‘externally from playbook’ I would use group/host\_vars.

As for inventory, it does not need to be files, via the dynamic scripts you can use any source of data to generate the inventory for Ansible, check the existing ones as they should cover most common cases.

---

<div class="post-metadata">

**Author:** ![aenagy](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@aenagy](https://forum.ansible.com/u/aenagy)\
**Post date:** [October 20, 2016, 8:47pm UTC](https://forum.ansible.com/t/scoping-of-playbooks/22831/3 "2016-10-20T20:47:42Z")

</div>

Brian:

After reading [http://docs.ansible.com/ansible/intro\_inventory.html](http://docs.ansible.com/ansible/intro_inventory.html) I’m thinking that I will end up with huge inventory files (hundreds or even thousands of systems) that are difficult to maintain – something I was hoping to avoid. My thought was that if I had a (CSV) file with default gateway addresses and associated location names and another (CSV) file with location names with configuration information (DNS server IP addresses, DNS suffix search order) that Ansible could iterate over both to make the necessary changes. This way I only need to worry about the ‘all’ inventory.

---

<div class="post-metadata">

**Author:** ![Jinesh\_Choksi](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jinesh\_Choksi](https://forum.ansible.com/u/Jinesh_Choksi)\
**Post date:** [October 21, 2016, 9:04am UTC](https://forum.ansible.com/t/scoping-of-playbooks/22831/4 "2016-10-21T09:04:43Z")

</div>

If in your inventory file you were targeting all possible nodes which require configuring:

Then in a playbook (run on each of those nodes):

- You could use: set\_fact ([http://docs.ansible.com/ansible/set\_fact\_module.html](http://docs.ansible.com/ansible/set_fact_module.html)) to register a fact containing the current node’s gw. I think you can use the Jinja2 ipaddr() filter to get the gateway ([http://docs.ansible.com/ansible/playbooks\_filters\_ipaddr.html](http://docs.ansible.com/ansible/playbooks_filters_ipaddr.html)). Alternatively, use the command module and any cli method you are familiar with to get the gw.

- You could use: CSV File Lookup ([http://docs.ansible.com/ansible/playbooks\_lookups.html#the-csv-file-lookup](http://docs.ansible.com/ansible/playbooks_lookups.html#the-csv-file-lookup)) to lookup the current node’s gw (which was registered as a fact above) and get the location name. Then use CSV File Lookup again to get the configuration information from the location and then do you configuration.

Does that help?

---

<div class="post-metadata">

**Author:** ![aenagy](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@aenagy](https://forum.ansible.com/u/aenagy)\
**Post date:** [October 21, 2016, 3:19pm UTC](https://forum.ansible.com/t/scoping-of-playbooks/22831/5 "2016-10-21T15:19:11Z")

</div>

Jinesh:

This looks promising. I’m sure I’ll have more questions as I get deeper into this.

Thanks.
