From the course: Red Hat Certified Engineer (EX294) Cert Prep: 1 Foundations of Ansible

Build static inventory

From the course: Red Hat Certified Engineer (EX294) Cert Prep: 1 Foundations of Ansible

Start my 1-month free trial

Build static inventory

- [Instructor] Ansible works against multiple nodes or hosts at the same time. This list of nodes is called an inventory and can be static or dynamic. Once you've defined your inventory, you can target certain hosts or groups of hosts using patterns The default inventory file location is /etc/ansible/hosts. However, you are free to specify a different static inventory file using the -i command option. You can also use more than one static inventory file at the same time. The inventory file can be in one of many different formats depending on the inventory plug-ins you have installed. The most common formats are INI and YAML. The INI file is the simplest, so we'll look at it first. A basic INI inventory file looks like this. The headings in brackets are group names which are used in classifying hosts and deciding which host to control at one time. The single entry is specified but not grouped. The same file in YAML format is a bit more complex looking. The YAML formatted file has multiple levels of indention, and the lines end with a colon. You may notice that we also have listed an all group, and everything is nested under it. This is a default group. There are two such default groups, all and ungrouped. The all group contains every host. The ungrouped group contains all hosts that don't belong to another group. Every host will always belong to at least two groups, or all and ungrouped or all and another specific group we've defined. You will most likely place your host in more groups than these two. Often groups are categorized in what. What, which is a service or services a host provides such as Web or DNS. Where, which is where a host is located if it's in a multi-location company. The where may be north or south or more specific like Seattle. When, such as development stage, test stage, or production stage. Our previous YAML file already satisfies what in the case of web servers and db servers. If we were to expand our YAML inventory file to include where and when, it would look like this. To satisfy the where, we have a group for Seattle and for San Francisco, each with one web server and one db server. To satisfy when, we also have a production group with web1, web2, db1, and db2 in it. The test group has web3 and db3. We can also nest. For instance, if all hosts in prod were in San Francisco, we could call the San Francisco group instead of listing the servers again. There are more shortcuts that we can employ in our inventory files, for instance, ranges. In this file, we're specifying web1, web2, and web3. We can use a range to compress them into one line. We can add alphabetic ranges using the same syntax. The format is the same for INI files, just include the range in the host name. If you need to access a host on a non-standard SSH port, you can add it to the host name. Ansible lets us specify the inventory file using the -i option followed by the path to the file. We can provide more than one inventory file in the same manner by providing more than one -i option with path. Keep in mind that if there's a conflict in the two files, the variables in the latter file will be used. So in this case, variables in production will override those in staging because it's processed last. In our small lab set up, we have a very simple inventory file because rh host2 is our only managed host. If you want to make your system more interesting to manage and you have more resources, you can clone rh host2 and add it to your static inventory. Now, let's prepare for the future and modify our static inventory file. Type in sudu space vi space /etc/ansible/hosts and hit enter. Type in your password if prompted. Currently we have one line in this file with rh host2's IP address. Let's include rh host2 in a couple of groups. We'll keep our inventory file in INI format for this course. Go down below the IP address and go into insert mode by pressing the i key and then add left square bracket webservers right square bracket and then below that rhhost2.localnet.com. And then we'll add another group, left square bracket dbservers right square bracket and then the host name again. We have a default group of all which includes all hosts. We also have a web servers group which includes rh host2 in it as well as a db servers group with the same host. We could target this host directly or specify either of these groups. Let's save and exit by pressing escape, colon, exclamation mark and hitting enter. Note that before we can target by domain name, we need name resolution either static or dynamic. For this we'll edit the Etsy host file in rh host1. Type in sudu space vi space /etc/hosts and hit enter. Go into insert mode and add a new line. For the first column, add in rh host2's IP address. For me this is 192.168.3.110 and then space and rh host2. Then space again and rhhost2.localnet.com. Now save and exit by pressing escape, colon, exclamation mark and hitting enter. The last thing we'll do is test it. To test it, we'll type in ping space -c1 for one count rhhost2 and hit enter. If that works, bring your line back and add .localnet.com and hit enter again. If that works, we can proceed.

Contents