Ansible Roles
Overtime working with ansible a user may create hundreds of playbooks, variables, templates, defaults etc. Roles allow users to group this logic into an organized manner making reusability and sharing of ansible structure easier.
• Overtime working with ansible a user may create hundreds of playbooks, variables, templates, defaults etc.
• Roles allow users to group this logic into an organized manner making reusability and sharing of ansible structure easier.
• Roles uses directories to structure and group all the playbooks, variables, templates, tasks, handlers, files, and defaults.
• This collected logic can be grouped in any way the user wants, for example you can group server specific roles together
• These roles can then be used inside playbooks and even as in-line commands
Ansible Galaxy
• Galaxy provides pre-packaged units of work known to Ansible as roles
• Roles can be dropped into Ansible PlayBooks and immediately put to work
• To create a Ansible roles, use ansible-galaxy command which has the templates to create it
• This will create it under the default directory /etc/ansible/roles and do the modifications else we need to create each directories and files manually # ansible-galaxy init /etc/ansible/roles/apache –offline
• where, ansible-glaxy is the command to create the roles using the templates
• init is to initiliaze the role
• apache is the name of role
• offline - create offline mode rather than getting from online repository
• You can also download Ansible roles from https://galaxy.ansible.com/
Exercise - Ansible Roles
To create a Ansible roles, use ansible-galaxy command which has the templates to create it. This will create it under the default directory /etc/ansible/roles and do the modifications else we need to create each directories and files manually
# ansible-galaxy init /etc/ansible/roles/apache –offline
where, ansible-glaxy is the command to create the roles using the templates
- init is to initiliaze the role
- apache is the name of role
- offline - create offline mode rather than getting from online repository
List out the directory created under /etc/ansible/roles
# tree /etc/ansible/roles/apache/
We have got the clean directory structure with the ansible-galaxy command. Each directory must contain a main.yml file, which contains the relevant content
Directory Structure:
- tasks - contains the main list of tasks to be executed by the role
- handlers - contains handlers, which may be used by this role or even anywhere outside this role
- defaults - default variables for the role
- vars - other variables for the role. Vars has the higher priority than defaults
- files - contains files required to transfer or deployed to the target machines via this role
- templates - contains templates which can be deployed via this role
- meta - defines some data / information about this role (author, dependency, versions, examples, etc,.)
Below is a sample playbook codes to deploy Apache web server. Lets convert this playbook codes into Ansible roles
---
- hosts: all
tasks:
- name: Install httpd Package
yum: name=httpd state=latest
- name: Copy httpd configuration file
copy: src=/data/httpd.original dest=/etc/httpd/conf/httpd.conf
- name: Copy index.html file
copy: src=/data/index.html dest=/var/www/html
notify:
- restart apache
- name: Start and Enable httpd service
service: name=httpd state=restarted enabled=yes
handlers:
- name: restart apache
service: name=httpd state=restarted
First, move on to the Ansible roles directory and start editing the yml files
# cd /etc/ansible/roles/apache
1. Tasks
Edit main.yml available in the tasks folder to define the tasks to be executed
---
# tasks file for apache
- name: Install httpd package
yum: name=httpd state=latest
- name: Copy httpd configuration file
copy: src="/etc/ansible/roles/apache/files/httpd.original"
dest="/etc/httpd/conf/httpd.conf" owner=root remote_src=True
- name: Copy index.html file
copy: src="/etc/ansible/roles/apache/files/index.html" dest="/var/www/html" owner=root remote_src=True
notify:
- restart apache
- name: Start and Enable htpd service
service: name=httpd state=restarted enabled=yes
2. Files
Copy the required files (httpd.conf and index.html) to the files directory
# cat files/index.html
This is a homepage created for ansible roles.
3. Handlers
Edit handlers main.yml to restart the server when there is a change. Because we have already defined it in the tasks with notify option. Use the same name "restart apache" within the main.yml file as below
# cat handlers/main.yml
---
# handlers file for /etc/ansible/roles/apache
- name: restart apache
service: name=httpd state=restarted
4. Meta
Edit meta main.yml to add the information about the roles like author, descriptions, license, platforms supported
# cat meta/main.yml
galaxy_info:
author: manika
description: Apache Webserver Role
company: Leading Partners
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Some suggested licenses:
# - BSD (default)
# - MIT
# - GPLv2
# - GPLv3
# - Apache
# - CC-BY
license: license (GPLv2, CC-BY, etc)
min_ansible_version: 1.2
# If this a Container Enabled role, provide the minimum Ansible Container version.
------skipped
List out the created files
# tree
.
|-- README.md
|-- defaults
| `-- main.yml
|-- files
| |-- httpd.conf
| `-- index.html
|-- handlers
| `-- main.yml
|-- meta
| `-- main.yml
|-- tasks
| |-- configure.yml
| |-- install.yml
| |-- main.yml
| `-- service.yml
|-- templates
|-- tests
| |-- inventory
| `-- test.yml
`-- vars
`-- main.yml
8 directories, 13 files
We have got all the required files for Apache roles. Lets apply this role into the ansible playbook "runsetup.yml" as below to deploy it on the client nodes
# cat /etc/ansible/runsetup.yml
---
- hosts: all
remote_user: test
become: yes
become_method: sudo
roles:
- apache
Lets verify for syntax errors:
# ansible-playbook /etc/ansible/runsetup.yml --syntax-check
Lets deploy the role
# ansible-playbook /etc/ansible/runsetup.yml
Comments
Post a Comment