Followers

Ansible- Roles

  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
            hosts: webservers
            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

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,git quiz,1,Git Worksheet,1,ITIL,1,ITSM,1,Jira,3,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,5,SonarQube,3,Splunk,2,vagrant kubernetes,1,Windows,1,YAML Basics,1,
ltr
item
DevOpsWorld: Ansible- Roles
Ansible- Roles
DevOpsWorld
https://www.devopsworld.co.in/2022/02/ansible-roles.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/02/ansible-roles.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content