Ansible Roles using ansible-galaxy
Ansible Roles :
- Roles are consist of more than one playbooks like modules in puppet and cook books in Chef.
- Roles are used to perform mulitple tasks under one container with clean and neat directory structure.
- Roles are set of tasks and additional files to breakup the configuration for easy handling
- Tasks under role can be used by anyone if those rules/tasks maches with their needs.
What is Ansible Galaxy?
Ansible Galaxy is used to initiate a role which will create respective directory structure under that the role name.
How to initiate a role using Galaxy?
Command to initiate the role using Galaxy is
ansible-galaxy init <directory path>
Eg:
ansible-galaxy init /etc/ansible/roles/apache
Now let us create the tasks
Go to /etc/ansible/roles/apache/tasks
vi main.yml
Above tasks does take care of installing httpd (apache) service, copying index.html to target server, starting service and enabling at the boot config.
In single task file we are performing more than one task, this can be done it through mulple yml files and then we can import all the yml files into tasks using below syntax
Install.yml will look like below
---
- name: Install apache httpd package
- yum: name=httpd state=latest
Configure.yml will look like below
---
- name: copy httpd.conf file to the target servers
copy: src=files/httpd.cfg dest=/etc/httpd/conf/httpd.cfg
- name: copy index.html to the target servers
copy: src=files/index.html desk=/var/www/index.html
notify:
- retart apache
Note: src files kept it in 'files' directory so make sure you keep the httpd.cfg and index.html in that directory
service.yml will look like below
---
- name: enable service to come up during reboot
service: name=httpd state=restarted enabled=yes
Next
Now you can update the meta data main.yml with author, purpose of this playbook, version, date etc
Restarting httpd service whenever there is any configuration change can be updated into hander section of the role.
Finally, we can create Run.yml to call the apache role to execute the underneath tasks
Run.yml
---
- hosts: all/node1 (all means calling inventory)
- roles:
- apache
if you want to call more than one role then mention all of them one by one
Execution:
validation
ansible-playbook Run.yml --syntax-check
ansible-playbook Run.yml



Comments
Post a Comment