Reference:
user name geerlingguy is very famous in galaxy.ansible.com:
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, then 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. |
Step 1: Download and use apache role from Ansible Galaxy.
Step 2: Go to this Ansible Galaxy Page ( which I got by searching apache2 in galaxy.ansible website).
Step 3: Copy the installation step. (ansible-galaxy install geerlingguy.apache)
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: Go to /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.
Step 10: remove the role directory from /etc/ansible/roles
cd /etc/ansible/roles
rm -ifr geerlingguy.apache/
EXAMPLE 2: MAKE YOUR OWN PRE-DEFINED ROLE
Step 1: Initiate an apache user-defined role in the roles directory.cd /etc/ansible/roles
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.
cd /etc/ansible/roles/apache/tasks
Step 4: In the tasks folder, open main.yml file and add below code. Then create 2 more yaml files, one for installing apache(step4.1) and second for configuring apache service(step4.2).
---
- include: install.yml
- include: configure.yml
Step 4.1: Create Install.yml file in /tasks/
- name: Installing apache on ubuntu
Step 4.2: Create Configure.yml file in /tasks/
--- - name: apache status copy: src=status.txt dest=/tmp/status.txt #should be in the files folder notify: #notify will call the handler main.yml 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 #destination at files folder
Step 5: Define handler in /handlers/main.yml (usually being called by 'notify')
---
Step 6: 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 7: Create playbook (apache.yaml) and call apache role.
vi apache.yaml
---
- hosts: webservers
roles:
- apache
Step 8: Execute playbook and verify on webservers that apache is installed and running.
ansible-playbook apache.yaml
No comments:
Post a Comment