Ansible Inventory Automation Using Consul and Orchestrator
Inventory Management in Ansible
In Ansible there are two key methods for managing inventory. You can either create and maintain an Ansible inventory file , or you can point your Ansible playbook at a single database server, then use modules like add_host to manage inventory in memory once the playbook has launched. The problem is that both of these methods can lead to some extra work and possibly expose you to human error. If you opt to go with in-memory inventory management using modules, you may have to write some complex code to not only to parse the environment, but monitor for changes as well. For example, if you have a playbook that involves a promotion of a replica to become a new master, you may need to place additional code to validate the promotion, then update your in-memory inventory before continuing to the next task in the playbook. If you’re using an inventory file you have to take on the responsibility of keeping it up to date. Between the initial authoring and the time you execute a playbook, there may have been changes. Someone may have forgotten to take that recently promoted replica out of the replica group, leading to some pretty catastrophic results. In short, you always need to verify the accuracy of your inventory prior to playbook execution. This always makes me a bit nervous. This drove me to question whether I could automate Ansible inventory management. It turns out the answer is yes!Ansible Inventory Automation
My thought stemmed from the idea that if we trust Consul enough to have it drive our proxy configuration, we should be able to trust it enough to drive inventory management for process automation. I launched my lab MySQL containers in two clusters, one called “osdb-lab-cluster” and the other called “pythian-lab-cluster.” Both of these clusters had a single master and four replicas. Orchestrator was managing both these clusters. Using the native Consul capabilities for the master of the cluster, as well as our scripts to pass along replica information, I ensured that information for both of these clusters was being passed into Consul. Master information was placed in Consul under /mysql/master/{cluster-name}/{hostname}. Replica information was placed in Consul under /mysql/slave/{cluster-name/{hostname}. Please note that we have expanded the replica script to include information about datacenter, downtiming, etc. Given this structure, I created the following Consul-template file.cluster= role=master cluster= role=replica datacenter= [] [-master] [-replicas]After configuring Consul-template to work with this template file, it created the following Ansible inventory file:
mysql1 cluster=osdb-lab-cluster role=master mysql6 cluster=pythian-lab-cluster role=master mysql2 cluster=osdb-lab-cluster role=replica datacenter=DC-1 mysql3 cluster=osdb-lab-cluster role=replica datacenter=DC-1 mysql4 cluster=osdb-lab-cluster role=replica datacenter=DC-2 mysql5 cluster=osdb-lab-cluster role=replica datacenter=DC-2 mysql10 cluster=pythian-lab-cluster role=replica datacenter=DC-2 mysql7 cluster=pythian-lab-cluster role=replica datacenter=DC-1 mysql8 cluster=pythian-lab-cluster role=replica datacenter=DC-1 mysql9 cluster=pythian-lab-cluster role=replica datacenter=DC-2 [osdb-lab-cluster] mysql1 mysql2 mysql3 mysql4 mysql5 [pythian-lab-cluster] mysql6 mysql10 mysql7 mysql8 mysql9 [osdb-lab-cluster-master] mysql1 [pythian-lab-cluster-master] mysql6 [osdb-lab-cluster-replicas] mysql2 mysql3 mysql4 mysql5 [pythian-lab-cluster-replicas] mysql10 mysql7 mysql8 mysql9This is a complete and accurate representation of my clusters that will automatically update as Orchestrator provides new information to Consul.
Conclusion
By leveraging Consul-template, you no longer have to worry about the creation, updating, or, accuracy of your inventory. When using a playbook that includes a promotion, all you have to do is:- End your playbook at the end of the promotion.
- Pause the process to allow Consul to receive new information.
- Resume your process launching a new playbook which reads from the same, freshly updated, inventory file.
On this page
Share this
Share this
More resources
Learn more about Pythian by reading the following blogs and articles.
MySQL high availability with ProxySQL, Consul and Orchestrator
MySQL high availability with ProxySQL, Consul and Orchestrator
Nov 18, 2019 12:00:00 AM
6
min read
MySQL high availability with HAProxy, Consul and Orchestrator
MySQL high availability with HAProxy, Consul and Orchestrator
Apr 23, 2018 12:00:00 AM
5
min read
Docker Orientation
Docker Orientation
Jul 1, 2020 12:00:00 AM
9
min read
Ready to unlock value from your data?
With Pythian, you can accomplish your data transformation goals and more.