Working with Ansible Roles | Ansible Roles Structure | Linuxtopic

Linuxtopic
0


ansible roles, ansible roles example, ansible roles tutorial, ansible tutorial, ansible, devops tools, ansible playbook, create ansible roles, ansible global variable, ansible tasks, linuxtopic


Tags: ansible roles, ansible roles example, ansible roles tutorial, ansible tutorial, ansible, devops tools, ansible playbook, create ansible roles, ansible global variable, ansible tasks, linuxtopic


















Ansible Roles: Ansible Roles are independent and small function files that used in ansible playbooks. We can say that roles it is a collections of variables, tasks, modules, files and templates.



Ansible Roles help us to break or divided big playbook in to the small files.



Creating Role : By default roles directory locate in ansible configuration directory. Below default path of ansible configuration directory is /etc/anisble



Step 1:







To Create “roles” directory, we can choose any name of roles directory, here we create linuxtopic roles under the ansible configuration directory. 


cd /etc/ansbile


mkdir -p /etc/ansible/roles/linuxtopic

This roles directory is set of other directories that will help us separate the different sections of the playbook.

Following default ansible roles directory structure



defaults
files
handlers
meta
tasks
templates
tests
vars





  • default : used for default variable

  • files & templates : it contains a files/scripts to be transferred on configuring hosts for this role. 

  • handlers: All handlers that were in your playbook previously can now be added into this directory.

  • meta: This directory can contain files that establish role dependencies. You can list roles that must be applied before the current role can work correctly.

  • templates: You can place all files that use variables to substitute information during creation in this directory.

  • tasks: This directory contains all of the tasks that would normally be in a playbook. These can reference files and templates contained in their respective directories without using a path.

  • vars: Variables for the roles can be specified in this directory and used in your configuration files.


To create directory structure under ansible roles directory


cd /etc/ansible/roles/linuxtopic/


mkdir tasks

To write a task in playbook



We will create a main.yml file in tasks directory, In this example we will copy file/script from source to destination using copy module and update dns entry in resolv.conf, following main.yml playbook


vi tasks/main.yml




---


# We can define all task






- name: Copy Script


  copy: src=script.sh dest=/usr/local/bin/script.sh mode=755






- name: Update resolve Entry


  template: src=resolv.conf.j2 dest=/tmp/resolv.conf




ansible roles, ansible roles example, ansible roles tutorial, ansible tutorial, ansible, devops tools, ansible playbook, create ansible roles, ansible global variable, ansible tasks, linuxtopic
Ansible Roles

How to use copy module Click Here…..



In first task We use copy module and define src=script.sh, src= is source path and script.sh is file, files/scripts read from files directory, so we will create a files directory under ansible roles/linuxtopic


mkdir files


touch files/script.sh


ll files/script.sh





In second task we use template module, so we will create a template file under templates directory, we use variables in this Jinja2 template. We will replace this template to resolv.conf of the hosts.


mkdir templates


vi templates/resolv.conf.j2





Here  


resolv.conf.j2 is Jinja2 template
{{ dns }} = variable, we define in vars/main.yml




To create vars directory for variables


mkdir vars


vi vars/main.yml


---


# variables file for linuxtopic


 dns: 8.8.8.8







Our basic ansible roles directory structure has been finished, following tree of linuxtopic roles


tree





Now we will create a playbook to execute roles.



How to add hosts in Ansible Inventory Click Here..



We will go to anisble configuration directory and create a yaml file to run our roles/tasks.


cd /etc/ansible


vi roles-main.yml


---


- hosts: all


  gather_facts: false




  roles:


    - {role: 'linuxtopic', tags: 'linuxtopic'}







We define role name with tags name



To run ansible playbook


ansible-playbook -l 127.0.0.1 roles-main.yml

To run ansible playbook with --tags


ansible-playbook -l 127.0.0.1 --tags "linuxtopic" roles-main.yml





Verify :







We successfully sync script.sh and replace value in resolv.conf







Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!