Wednesday, August 21, 2019

Scaling out with multiple files

We can scale the variable values using variable files.
Which we can span to multiple folders.


Structure inventory files and variables using the directory structure and spanning data to multiple files.

Below strcuture, two inventory files: inventory_prod and inventory_test.
Other files are variable files kept inside group_vars and host_vars directories, respectively for groups and individual hosts.


Basic Directory structure.




Above we have common group_vars and host_vars directories.
They got split now below on separate directories. Now, inventory files are in different directories.



While Adding variables, the order of precedence for the values is given below.

1. (Group_Vars) All
2. (Group_Vars) GroupName
3. (Host_Vars) HostName


Top to down is the higher.So, All is given less preference over Hostname.

Variable files Structure:

It starts with 3 hyphens.



Comments with #






























Now, let's test above precedence.


production/group_vars/all

---
username: all_user

cd to production, where the inventory file exists.


The below command will read value from all file above and create a new user.

ansible webservers -i inventory_prod -m user -a “name={{username}}” password=12345” --sudo

--sudo will give access to the remote box root level permissions.


production/group_vars/webservers

---
username: group_user

If we run the above command now, the precedence will be given to webservers group variable file.

production/host_vars/web1

---
username: web1_user

Now, for web1 server, it will give precedence to this web1 variable file and for other machines, the precendence picks accordingly.


No comments:

Post a Comment