Ansible roles - building a small Apache role
Lets’ use an Ansible role to deploy Apache with a single static page on multiple environments: Dev and Prod.
For these exercises, checkout the role branch from our repository:
# stash our current changes
# changes may be retrieve using git stash pop
git stash
# checkout role branch
git checkout role
This branch has a very simple playbook referencing the apache role. The role can be found under roles/apache and define tasks to install Apache with a simple index.html template with a few variables.
Explore role structure
Explore the files unders roles/apache and:
- Find where the role’s tasks are defined
- Find where the role’s default variables are defined
- Find in Ansible documentation’s the recommended role layout and goal of each folder’s in a role
Try to run the playbook with our defined role.
- What value of the
apache_hello_messagevariable was used?
Define multiple inventories and configuration
We now want to define multiple environments, each Ansible inventory representing a different environment:
devenvironment showing messageHello from Dev!prodenvironment showing messageHello from Prod!- Some overrides in Prod environments
Using Ansible inventories:
- Copy
inventories/devtoinventories/prod - Configure each inventory to use a single host by adding a
2to your original host, for example:pierre1.training.crafteo.io pierre2.training.crafteo.io - Configure your inventories to ensure
index.htmlshows message:Hello from Dev!withdevinventoryHello from Prod!withprodinventory
- Permissions of the
index.html- In Dev, we want to have permissions
0774onindex.html - In Prod, we want to keep role’s default permissions for
index.html - Note: this may require some changes in role
- In Dev, we want to have permissions
Run your playbook and check changes were applied. Reminder: use -i INVENTORY to specify inventory to use, for example:
# use dev inventory
ansible-playbook -i inventories/dev playbook.yml