Ansible Roles It is a standard directory structure to abstract the functionality of Ansible playbooks. You can search and download some of...
Ansible Roles
- It is a standard directory structure to abstract the functionality of Ansible playbooks.
- You can search and download some of the existing roles from Ansible Galaxy website ( https://galaxy.ansible.com/)
Ansible Roles Directory Structure
When you create or install an ansible galaxy role than the following directories automatically get created and these directories have a special purpose. Except for the templates and tests directory, all the pre-defined directories contain main.yml, if any of these directories get called then the execution of that directory is started from main.yml code only.
defaults:- For default variables values.
files: - files which are referred to in the role.
handler:- Whenever there is any notification is triggered when the handler's main.yml file gets called which should have a definition of those notification services.
meta:- It stores role's metadata like Authorname, version etc
tasks:- Contains the definition of modules which are supposed to execute as a task.
templates:- It includes the Jinja templates definition.
tests:- Contains test cases for role
vars:- To define global variables for the roles.
To download and install an ansible-galaxy role
Step 1:- Download and use apache role from Ansible Galaxy.
Step 2: Goto this Ansible Galaxy Page ( which I got by searching apache2 in ansible-galaxy website).
Step 3: Copy the installation step.
Step 4: Uncomment roles_path in ansible.cfg file
Step 5: Run the installation step which will download the apache role
ansible-galaxy install geerlingguy.apache
Step 6: Goto /etc/ansible/roles directory and you will find a folder "geerlingguy.apache"
Step 7: To use this role you need to write a playbook
- name: using predefined ansible galaxy role to install apache
roles:
- geerlingguy.apache
Step 8: Execute the playbook
ansible-playbook apacherole.yaml
Step 9:- Check all the webservers, apache should be installed on these servers and apache service is up and running.
To create user-defined roles.
Step 1:- Create an apache user-defined role
ansible-galaxy init apache --offline
Step 2:- By Executing the above command will create a folder under /etc/ansible/roles directory which has the required directory structure for the apache role.
Step 3:- Create tasks to install apache on agent node.
cd /etc/ansible/roles/apache/tasks
Step 4:- Open main.yml file and add below code where I am including 2 yaml files, one for installing apache and second for configuring apache service
---
- include: install.yml
- include: configure.yml
Step 5:- Create Install.yml file
---
- name: Installing apache on ubuntu
apt:
name: apache2
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Installing httpd on Redhat
yum:
name: httpd
state: present
when: ansible_facts['os_family'] == "RedHat"
Step 6:- Create Configure.yml file
---
- name: apache status
copy: src=status.txt dest=/tmp/status.txt
notify:
restart apache service
when: ansible_facts['os_family'] == "Debian"
- name: httpd status
copy: src=status.txt dest=/tmp/status.txt
notify:
restart httpd service
when: ansible_facts['os_family'] == "RedHat"
- name: send the file
copy: src=test.html dest=/var/www/html/test.html
Step 7:- Define handler in main.yml file.
--- # handlers file for apache - name: restart apache service service: name=apache2 state=restarted - name: restart httpd service service: name=httpd state=restarted
Step 8:- Create test.html and status.txt file in files folder.
cd /etc/ansible/roles/apache/files
echo "<h1> Super Cool Html file </h1>" >> test.html
touch status.txt
Step 9:- Create playbook (apache.yaml) and call apache role.
---
- hosts: webservers
roles:
- apache
Step 10: Execute playbook and verify on webservers that apache is installed and running.
ansible-playbook apache.yml
COMMENTS