From the course: Red Hat Certified Engineer (EX294) Cert Prep: 2 Using Ansible Playbooks

Create structured configuration

From the course: Red Hat Certified Engineer (EX294) Cert Prep: 2 Using Ansible Playbooks

Start my 1-month free trial

Create structured configuration

- [Instructor] As we've seen, it's fairly easy to create a global Ansible host file in a single Ansible playbook for testing. We created both of these in the EX294 Ansible Foundations Course. However, your infrastructure configuration will quickly outgrow this very simple setup, and it's a good time to start planning for the future. To get our Ansible configuration scheme to scale, we need to create a structure that separates group variables, roles, tasks, and templates. This structure will allow us to grow our configuration setup beyond where we are now. Before we create our configuration structure, we need to make sure our name resolution is working. We need to do this so we can use host names in our Ansible inventory files. You can manage name resolution by creating records in a DNS server if you have one, but to keep things simple, we'll configure the ATSE host files on our virtual machines. First, get the IP address of rhhost1. In a terminal, type in ifconfig and hit enter. You can also use the newer IP command if you wish. In the VirtualBox lab setup for this EX294 series of courses, we configured two network interfaces, one that attaches to the VirtualBox host network via NAT for accessing the internet. The other connector, the internal network named vboxnet0, which our VMs will use to communicate with each other. This internal network address should be 192.168.3.0, so find the network interface and the output of ifconfig and configure name resolution for it. I can see that my IP address for my enp0s8 interface on rhhost1 is 192.168.3.108. This will be the interface on the vboxnet0 internal network. Now switch over to your rhhost2 VM and check its interface on the 192.168.3 network, using the ifconfig command again. I can see that the address for my enp0s8 interface is 192.168.3.110. With this information, we can create our ATSE host files for both VMs. Now go back to your rhhost1 VM. Type in clear, and then edit the host file by typing in sudo vi /etc/hosts, and hit enter. Enter your password if prompted. Go into insert mode by pressing the I key, and then add a new line, and add your IP address for rhhost1. Mine is 192.168.3.108, space, rhhost1, space rhhost1.localnet.com, and a new line. Now type in your IP address for rhhost2. Mine would be 192.168.3.110 rhhost2 rhhost2.localnet.com. Save and exit by pressing escape colon x and hitting enter. Now let's copy this file to rhhost2 using secure copy. Type in scp /etc/hosts root@rhhost2:/etc/hosts, and hit enter. Accept the fingerprint if prompted. Also type in root's password if it prompts you. We had to copy as root because we're copying this file to a system directory in rhhost2. If it prompted you for root's password, then your SSH keys are not properly configured. You want to use the SSH copy ID command to copy your keys across to the root user's authorized keys file. Be sure you do this before moving on. Type in SSH-copy-ID root@rhhost2 and hit enter. Type in root's password and hit enter again. Now let's create our static inventory file. On rhhost1, change into your Ansible files directory in your home by typing in CD ~/Ansible-files and hit enter. And type in clear. We're going to create our structure here. Let's start by creating a file called hosts. Type in VI host and hit enter. Press your I key to go into insert mode and add left square bracket webservers. This is our first Ansible group. Webservers, right square bracket. This is our first Ansible group. Now add rhhost2.localnet.com, and a new line. For our second group, [dbservers], new line. And once again, rhhost3.localnet.com. Since we only have one managed host, we'll put it in both webservers and dbservers groups. If you have more managed hosts, you can mix it up a bit and add them here. Save and exit by pressing escape colon ! and hitting enter. Now that we have our static inventory taken care of, let's create a directory for our group variables. Type in mkdir group_VARs, and hit enter. Later, we'll be creating variable files in this directory for all hosts, hosts in the webservers group, and hosts in the dbservers group. But for now, we just wanted the directory created. Now let's great directories for our roles. We'll use BASH's brace expansion to simplify this. Type in mkdir -p for parent directories, space roles/{base,webservers,dbservers} /{handlers,tasks,templates} and hit enter. Now let's look at the results with the tree command. Type in tree and hit enter. If you don't have the tree command, you can install it with YUM. Tree shows that we now have role directories for base, webservers, and dbservers, and inside of each role directory are directors for handlers, tasks, and templates. The base role will contain configuration information that's common between all hosts. This would be general send.safe software, networking configuration, and user information. This directory structure will keep templates, tasks, and change handlers apart and allow us to scale. It would be very easy to recursively duplicate this entire structure to be modified for other configurations. It would also be simple to create a GetProject to put all the configuration data under version control.

Contents