Nerd For Tech
Published in

Nerd For Tech

Ansible Vault | Role | Conditions

Prerequisite:

Condition:

Conditions mean working on different scenarios different conditions are required to execute those conditions.

In ansible YAML script we use the “when” command to give the condition.

Let’s take an example of a script:

Script:

--- # My Condition Playbook
- hosts: developers
user: ansible
become: yes
connection: ssh
gather_facts: yes
tasks:
- name: installing apache2 on debian
command: apt-get -y install apache2
when: ansible_os_family == "Debian"
- name: installing httpd on linux
command: yum install httpd -y
when: ansible_os_family == "RedHat"

Commands:

vi conditions.yml // write the above YAML script

ansible-playbook conditions.yml

Node1 Output:

Ansible Vault =>

It keeps sensitive data like playbooks in an encrypted manner.

ansible-vault create vault.yml // create a script in an encrypt mode

ansible-vault edit vault.yml // to edit in an existing encrypted script

Editing encrypted file using VI editor

vault.yml // script output will be in an encrypted mode

To add an existing script to ansible-vault[i.e. In an encryption mode]

ansible-vault encrypt <file-name.yml>

ansible-vault encrypt target.yml

To decrypt YAML script ansible-vault decrypt target.yml

Ansible Roles =>

We can use two techniques for reusing a set of tasks: include and roles.
Roles are better for organizing tasks and encapsulating the data needed to accomplish those tasks.

Types of roles:

  • Default
  • files
  • Handlers
  • Meta
  • Templates
  • Tasks
  • Vars (variables)

In roles, playbooks can be organized in a directory structure.
Roles are important because adding more and more functionally to any file/script will make it more complex to handle.

Types of Roles:
Default: It stores the data about role/application default variables. example: port number.
Files: It contains files needed to be transferred to the remote VM
Handler: They are triggered from another task. they are also a task.
Meta: The directory contains the file that establishes role dependencies. eg: name of the author, supported platform, and so on.
Tasks: Contains all the tasks that are present in the playbooks.
Vars: variables stored in this directory and can be used further for configurations

Let’s understand the structure of roles.
Here playbook is the directory and master.yml and roles are the file and directory present inside the Playbook directory.
targets[host name, connection type, user name] and roles are defined inside the master.yml and the roles information is stored inside the roles directory. master.yml and roles are at the same level or say present inside the same directory.
In roles, different processes like Task, Handlers, var, and so on are executed one by one.

Present tree structure on a Linux machine:

Let’s create a role and run it from the playbook by YAML script.
Create parent directory means directory inside the directory. ‘-p’ flag is used to perform such task.

mkdir -p playbook/roles/webserver/tasks

Create a main.yml file inside the tasks directory and master.yml at the same level of the roles directory.

Commands:

cd playbook

touch roles/webserver/tasks/main.yml

touch master.yml

Create master.yml inside the playbook directory [at the same level of roles directory.

Script of master.yml:

--- # The master script
- hosts: developers
user: ansible
become: yes
connection: ssh
roles:
- webserver

Now here we have defined a role name webserver. let us create a process/task inside this role.

vi roles/webserver/tasks/main.yml

Script for main.yml:

- name: install apache on RedHat
yum: pkg=httpd state=latest

ansible-playbook master.yml

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store