The common directory contains a single directory called tasks that includes a main.yaml file with a single task to run an apt update operation.
The main directory contains ansible.cfg, hosts, and playbook.yaml files. While the hosts and ansible.cfg files are the same as the last exercise, the playbook.yaml file looks like the following:
- hosts: webservers
become: true roles: - common
- apache
- hosts: dbservers
become: true roles: - common
- mysql
The playbook is now a concise one with a lot of reusable elements. It consists of two plays. The first play will run on all web servers with the root user and apply common and apache roles to them. The second play will run on all nodes with the dbservers role with the root user and use common and mysql roles.
Tip
Always keep roles loosely coupled. In the preceding example, the apache role has no dependency on mysql and vice versa. This will allow us to reuse configuration with ease.
Now, let’s go ahead and execute the playbook:
$ ansible-playbook playbook.yaml
PLAY [webservers]
…
PLAY [dbservers]
…
PLAY RECAP
db: ok=10 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web: ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
And, as we can see, there are no changes to the configuration. We’ve applied the same configuration but in a better way. If we want to share our configuration with people within the team, we can share the roles directory, and they can apply the role within their playbook.
There may be instances where we want to use a different value for the variable defined in the roles section. You can override variables within the playbook by supplying the variable values with the extra-vars flag, as follows:
$ ansible-playbook playbook.yaml –extra-vars “mysql_user=foo mysql_password=bar@123”
When we apply the playbook using the preceding command, we’ll see that the user now changes to foo and that the password changes to bar@123 in both the Apache and MySQL configurations:
…
PLAY RECAP
db: ok=9 changed=1 unreachable=0 failed= skipped=0 rescued=0 ignored=0
web: ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
So, if we run the curl command to the web host, we will get the same response as before:
…
Database Connected successfully
…
Our setup is working correctly with roles. We’ve set up the Ansible playbook by following all the best practices and using reusable roles and templates. That is the way to go forward in designing powerful Ansible playbooks.
Summary
In this chapter, we’ve discussed Ansible and its core functionalities from a hands-on perspective. We began by understanding CaC, looked at Ansible and Ansible architecture, installed Ansible, understood Ansible modules, tasks, and playbooks, and then applied our first Ansible configuration. We then looked at fostering reusability with Ansible variables, Jinja2 templates, and roles and reorganized our configuration with reusability in mind. We also looked at several best practices along the way.
In the next chapter, we will combine Terraform with Ansible to spin up something useful and look at HashiCorp’s Packer to create immutable infrastructure.
Leave a Reply