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

Setting inventory and playbook variables

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

Start my 1-month free trial

Setting inventory and playbook variables

- Although it can be an incredible time-saver to create static configurations and push them out to nodes, we often need more flexibility. For instance, we may want to include the IP address of the host in its own configuration or in the configuration of another node. In setting up a name resolution we put the IP addresses of rhhost1 and rhhost2 in our etc host file. Instead of hard coding the address in the file, we can provide a variable that is populated from another source if we are configuring our systems with Ansible. Before we can start using variables, we need to follow some guidelines as far as naming them. Variable names can contain letters, numbers, and underscores. Also, variable names should always start with a letter. Valid variable names could be, rhhost2 underscore port or rhhost2port. Assigning variables is pretty similar to other systems. For instance, to assign a value to rhhost2 underscore port we'd type the variable name equals and then the value. Invalid variable names would be rhhost2 dash port, rhhost2 space port and rhhost2 dot port. Ansible also support dictionaries that hold key value pairs. For instance, for rhhost2 we can assign values to SSH port and web port. We would access each specific field in the dictionary by specifying the name of the dictionary and then the name of the key inside square brackets. There is also a dotted notation, but I'll avoid using it as there can be conflicts with Python methods. It looks like this, dictionary name dot in the key. We can also set variables in our inventory file. Our inventory file is in INI format so it looks like this. To add a variable you just had a space at the end of the node and add the key value pair. The same inventory file would look like this in the YAML format. For simple inventory setups I think INI is better. It's shorter and easier to read and the YAML format we add entries after the host name with our variables and values. However, if you want to set a lot of variables, INI format very quickly becomes clumsy. I don't find this easier to read at all. When my inventory starts to get complex, I then move my variables somewhere else, or switch to YAML format. The same information in the YAML format would look like this, which I find easier to read. I can quickly scan the file and know which variables belong to which hosts. If you want to set a variable for all nodes in an Ansible group, you'd structure it a bit differently. In INI format, we create a new variable section that matches the Ansible group. To set variables for our group in YAML from format, you just add a vars section. I find group variables easier to read in either INI or YAML formats. We can also set variables in our playbooks. This is our site dot YMI file with a vars section. You may remember that when we created our Ansible files directory structure, we created a group vars directory. We can create a file in that directory named after the group and put our variables in it. For instance, for the web service group, we'd create Ansible files, slash group vars, slash web servers and inside of that, we'd add our variables. I need to add one more node that's specific to my example, choice a variables. I've been specifying SSH port in my examples. This is a valid variable that we can define for the SSH service, but keep in mind that setting the SSH port this way only affects open SSH connections. If you use impair AMECO, it won't use this value. Just keep this in mind if you're using this variable in your configuration.

Contents