Understanding Ansible Roles
Ansible roles is a structured way of grouping tasks, handlers, vars and other properties. They increase reusability. You can easily share roles through Ansible Galaxy. If you are brand new to Ansible first read the tutorial for beginners.
Creating an Apache Server on Ubuntu Using Ansible Roles
For this project, you'll need two Ubuntu machines. The first one will be your Ansible controller and the second one will be your target machine for Apache installation. Before starting you should make sure you can connect to your target machine from your controller through Ansible.
You can use the following command to see if everything is working:
# ansible all -m ping172.17.0.3 | SUCCESS =>
"changed": false,
"ping": "pong"
The 172.17.0.3 is defined in the /etc/ansible/hosts file as:
[myserver1]172.17.0.3 ansible_user=zakh
Ansible Roles
In your /etc/ansible, there should be a roles folder. Go into the folder and issue the following command:
# ansible-galaxy init apache --offline- apache was created successfully
The command should automatically create the following structure:
'-- apache|-- README.md
|-- defaults
| '-- main.yml
|-- files
|-- handlers
| '-- main.yml
|-- meta
| '-- main.yml
|-- tasks
| '-- main.yml
|-- templates
|-- tests
| |-- inventory
| '-- test.yml
'-- vars
'-- main.yml
Here are the main components we will use in this lesson:
- tasks/main.yml - It is the starting point of the role tasks. You can use the main.yml to point to other task files.
- handlers/main.yml - It contains the handlers.
- files - You can keep your files and resources that you want to deploy here.
The other folders (not used in this tutorial):
- defaults/main.yml - It contains the default variables for the role.
- meta/main.yml - It contains the metadata information for the role.
- templates - It is a folder to place Jinja2 templates.
- test - It can be used for setting up inventory and test cases.
- vars/main.yml - It is used for variable setup.
Let's start with the tasks/main.yml. Paste the following code inside:
---# tasks file for apache
- include_tasks: install.yml
- include_tasks: configure.yml
- include_tasks: service.yml
We are dividing the tasks into smaller portions and pointing to other YAML files. So we need to create those files.
install.yml
Inside /etc/ansible/roles/apache/tasks, let's create install.yml with the following code:
---# installing apache2
- name: installing apache2 server
apt:
name: apache2
state: present
It is installing apache2 on the Apache server. It's using apt because our target machine is running Ubuntu.
files, configure.yml and handlers/main.yml
Let's set up some files and resources in the /etc/ansible/roles/apache/files/ folder. First, you can get a standard apache2.conf file, make your custom changes and put it in the folder. In our case, we are just going to add “# Custom config” comment on the top. During the run process, ansible will take this apache2.conf file and replace it on the target machine.
Then we are going to create an index.html in the /etc/ansible/roles/apache/files/ folder with the following code.