Tuesday, February 22, 2022

EXAMPLES: Ansible -file Module

 


This is standalone because it is important and is usually utilized in real-life work. The file module in ansible deals with file/directory-related operations.

Reference (documentation): 

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html 

http://www.devopsworld.co.in/p/ansible-file-module.html

https://irfandevops.blogspot.com/2022/02/hidden-file-module-ansible.html

Ansible Vault

 


Ansible Vault

Ansible Vault is a feature of ansible that allows you to keep sensitive data such as passwords or keys in encrypted files, rather than as plaintext in playbooks or roles. These vault files can then be distributed or placed in source control.


 CREATE A PLAYBOOK(PROTECTED) VIA ANSIBLE-VAULT

ansible-vault create playbookprotected1.yaml

it will promp new password and confirmation of the password

               

TRY ACCESS THE PROTECTED PLAYBOOK USING ANSIBLE-PLAYBOOK

    ansible-playbook playbookprotected1.yaml

it will show you an error. Attempting to decrypt but no vault secrets found as per image below:




ACCESS USING ANSIBLE-PLAYBOOK with --ask-vault

    ansible-playbook playbookprotected1.yaml –ask-vault

it will ask you for a password and execute the playbook! as per image below:



DECRYPT THE PROTECTED PLAYBOOK VIA ANSIBLE-VAULT DECRYPT

    ansible-vault decrypt playbookprotected1.yaml

it will ask you for a password and decrypt the playbook. now you can access the playbook (as it is decrypted already), as per image below:






ENCRYPT A NON-PROTECTED PLAYBOOK VIA ANSIBLE-VAULT ENCRYPT

    ansible-vault encrypt playbookprotected2.yaml

it will prompt a password and it will encrypt it. thus you cannot access it anymore:




EXTRA INFO: YAML origins and overview


YAML firstly introduced as a markup language. thus YAML stands for Yet Another Markup Language. but then it was found out that it actually is a data representation language so they changed the acronym to: YAML Aint'a Markup Language. 

Lol 

  • It is python based Language.
  • It is widely used in DevOps tools like Docker, Kubernetes, and Ansible.
  • You need to indent your code properly otherwise it throws syntax - Error.
  • .yml or .yaml is the extension of YAML files.
  • It stores the data in the following formats:

a) key-value pair:
                -name: ansible
b) collection:
                -names:
                        - Irfan
                        - Rahman
                        - Ayyub


YAML Ain't Markup Language is a data serialization language that matches user’s expectations about data. It designed to be human friendly and works perfectly with other programming languages. It is useful to manage data and includes Unicode printable characters. This chapter will give you an introduction to YAML and gives you an idea about its features

Now that you have an idea about YAML and its features, let us learn its basics with syntax and other operations. Remember that YAML includes a human readable structured format.

Rules for Creating YAML file

When you are creating a file in YAML, you should remember the following basic rules −

  • YAML is case sensitive

  • The files should have .yaml or .yml as the extension

  • YAML allows spaces for indentation.

Basic Components of YAML File

The basic components of YAML are described below −

Conventional Block Format

This block format uses hyphen+space to begin a new item in a specified list. Observe the example shown below −

--- # Favorite movies
 - DDLJ
 - Sholye
 - Sahid

Inline Format

Inline format is delimited with comma and space and the items are enclosed in JSON. Observe the example shown below −

--- # Shopping list
   [mobile,laptop,HDMI Cable,Router]

Folded Text

Folded text converts newlines to spaces and removes the leading whitespace. Observe the example shown below −

- {name: Raman Sharma, age: 40}
- name: Manoj
  age: 27

The structure which follows all the basic conventions of YAML is shown below −

men: [Raman, Manoj]
women:
  - Mary 
  - Susan 


Synopsis of YAML Basic Elements

  • The synopsis of YAML basic elements is given here: Comments in YAML begins with the (#) character.

  • Comments must be separated from other tokens by whitespaces.

  • Indentation of whitespace is used to denote structure.

  • Tabs are not included as indentation for YAML files.

  • List members are denoted by a leading hyphen (-).

  • List members are enclosed in square brackets and separated by commas.

  • Associative arrays are represented using colon ( : ) in the format of key value pair. They are enclosed in curly braces {}.

  • Multiple documents with single streams are separated with 3 hyphens (---).

  • Repeated nodes in each file are initially denoted by an ampersand (&) and by an asterisk (*) mark later.

  • YAML always requires colons and commas used as list separators followed by space with scalar values.

  • Nodes should be labelled with an exclamation mark (!) or double exclamation mark (!!), followed by string which can be expanded into an URI or URL.

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...