Ansible - 5

         Conditions, Vaults and Roles in Ansible

                 =================================================

Conditions

---------

-> whenever we have different different scenarios, we put conditions according to the scenario


When statement

--------------

Sometimes you want to skip a particular command on a particular node.


--- # Condition Playbook

- hosts: demo

  user: ansible

  become: yes 

  connection: ssh

  task:

   - name: install apache on debian

     command: apt-get -y install apache2

     When: ansible_os_family == "Debian"

   - name: install apache for redhat

     command: yum -y install httpd

     When: ansible_os_family == "RedHat"


esc --> :wq!


Note: we can use command module for linux command in playbook.


===============================================================


Vault

                        -----

-> ansible allows keeping sensitive data such as passwords or key in encrypted files,rather than a plaintext in your playbooks.

it use AES256 technology to encryption to encrypt the file.

-> creating a new encrypted playbook.

ansible-vault create vault.yml


-> Edit the encrypted playbook

ansible-vault edit vault.yml


-> To change the password

ansible-vault rekey vault.yml


->to encrypt an existing playbook.

ansible-vault encrypt target.yml


->to decrypt an encrypted playbook

ansible-vault decrypt target.yml         

======================================================

Roles

-----

-> we can use two techniques for reusing a set of tasks:- includes adn roles

-> Roles are good for organising tasks and encapsulating data needed to accomplish those task.


Ansible Roles

=============

a) Default

b) Files

c) Handlers

d) Meta

e) Templates

f) Tasks

g) vars


-> we can organise palybooks into a directory structure called roles.

-> Adding more and more functionality to the playbooks will make it difficult to maintain in a single file.


Roles

------

-> Default = It stores the data about role/application default variables eg -> if you want to run to port 80 or 8080 then variables needs to define in this path.

-> Files = it contains files need to be transferred to the remote VM (static files)

-> Handlers = They are triggers or task we can segregate all the handlers required in playbook.

-> Meta = This directory contain files that establish roles dependencies eg-> Author name, Supported platform, Dependencies if any.

-> Tasks = It contains all the tasks that is normally in the playbook eg-> installing packages and copies files etc.

-> Vars = Variables for the role can be specified in the directory and used in your configuration files both vars and default stores variables.


[ansible@ip]$ mkdir -p playbook/roles/webserver/tasks

[ansible@ip]$ tree

o/p -> playbook

        |---->roles

                |---->webserver

                          |----->tasks

[ansible@ip]$ cd playbook/

[ansible@ip]$ tree

0/p  .

     |--->roles

            |--->webserver

                     |----->tasks

[ansible@ip]$ touch roles/webserver/tasks/main.yml

[ansible@ip]$ touch master.yml

[ansible@ip]$ vi roles/webserver/tasks/main.yml


-name: install apache

 yum: pkg=httpd state=latest


esc --> :wq!

[ansible@ip playbook]$ vi master.yml

--- # master playbook for webservers

- hosts : demo

  user : ansible

  become : yes

  connection : ssh

  roles:

   - webserver


[ansible@ip playbook]$ ansible-playbook master.yml


 Ansible is a tool that allows you to create and control three key areas within the operations environment of the software development lifecycle. The first one is IT automation which allows you to write instructions to automate the IT professional's work that you would typically do manually in the past, the second is configuration management which allows you to maintain consistency of all systems in the infrastructure and the third is automatic deployment which allows you to deploy applications automatically on a variety of environments. Now let us get started and understand Ansible and it's architecture.






Comments

Popular posts from this blog

GIT - 3

Docker - 6

GIT - 1