- Changes can be made and used in a configuration file which will be processed in the following order:
* ANSIBLE_CONFIG (an environment variable)
* ansible.cfg (in the current directory)
* .ansible.cfg (in the home directory)
* /etc/ansible/ansible.cfg

2. Config File Keys
## ANSIBLE_HOST_KEY_CHECKING
It bypass the yes/no verfication while adding the remote host in known_hosts during SSH.
host_key_checking = False

## Become, Become_pass
hosts inventory file
centos1 ansible_user=root -> sshing in remote host as root user (copying the root password in authorized keys)
ubuntu1 ansible_become=true ansible_become_pass=password-> means sshing as user then becoming a root user.

## ansible_port
used to change the ssh default port to run on other port.
centos1 ansible_user=root
OR
centos1:2222
## Grouping

## Grouping & Vars & Children
[centos]
centos1 ansible_port=2222
centos[2:3] -> host-range
[centos:vars]
ansibel_user=root
[ubuntu]
ubuntu[1:3]
[ubuntu:vars]
ansible_become=true
ansible_become_pass=password
SO, [ubuntu:vars] will apply to all ubuntu group. Similary for centos

## Precedence
ansible_port=2222 will have higher precedence than the ansible_port=1234 because specific variable takes precedence over others.

So, if we ping all it should ping only centos1 and failed others(port=1234 is not a valid port number).
## Another way to run yaml.


### FILE MODULE
Sets attributes of files, symlinks, and directories, or removes files/symlinks/directories.Many other modules support the same options as the file module - including copy, template, and assemble.
* Parameters
- path : Path of the file being managed
- state:
Choices:
* absent
* directory
* hard
* link
* touch
* file
* touch
$ ansible aws -m file -a 'path=/tmp/sample state=touch'
This create a file to the defined location mentioned in path attribute . If the sample file is alredy present
then also it will not give error like file is already present.
If the file in this case sample is edited and then again touch command is executed the new file(sample) is created
with the previous content. (Note : See the TimeStamp)


* file ←
$ ansible aws -m file -a 'path=/home/sample state=file mode=600'
Above the file permission will not be changed if the file does not exist in the given location.
# Interesting facts:-
Idempotency:- An operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly
without any intervning actions.
GREEN = Success
YELLOW = Success with Changes
RED = Failure

### FETCH MODULE
It copy the file from remote to localhost. While fetching a file it creates a directory with the name of file.
$ ansible aws -m fetch -a "src=/tmp/sample dest=/tmp/sample"
OUTPUT Directory will be :-


### Ansible Playbooks, Breakdown of Section
hosts: aws
user: root
vars:
motd: "Welcome to CentOS Linux - Ansible Rocks\n"
tasks: - name: Configure a MOTD (message of the day) copy: src: centos_motd dest: /etc/motd
copy:
content: "Hi Ansible" // This content will get copied to the defined location
dest: /etc/motd
- name: Configure a MOTD (message of the day) copy: content: "{{ motd }}" dest: /etc/motd
...
# HANDLERS
hosts: centos user: root gather_facts: false
vars: motd: "Welcome to CentOS Linux - Ansible Rocks\n"
tasks: - name: Configure a MOTD (message of the day) copy: content: "{{ motd }}" dest: /etc/motd notify: MOTD changed
handlers: - name: MOTD changed debug: msg: The MOTD was changed
...
# Execute task based on Platform
hosts: linux
vars: motd_centos: "Welcome to CentOS Linux - Ansible Rocks\n" motd_ubuntu: "Welcome to Ubuntu Linux - Ansible Rocks\n"
tasks: - name: Configure a MOTD (message of the day) copy: content: "{{ motd_centos }}" dest: /etc/motd notify: MOTD changed when: ansible_distribution == "CentOS"
- name: Configure a MOTD (message of the day)
copy:
content: "{{ motd_ubuntu }}"
dest: /etc/motd
notify: MOTD changed
when: ansible_distribution == "Ubuntu"
handlers: - name: MOTD changed debug: msg: The MOTD was changed
...
## ANSIBLE Playbooks,Variables
* vars_prompt:- This will prompt to ask the username while executing the playbook.
Prompts are executed in the beginning of every play, it doesn't matter where you place vars_prompt block - before or after tasks section.
hosts: centos1 gather_facts: false
vars_prompt: - name: username
tasks: - name: Test vars_prompt debug: msg: "{{ username }}"
...
![vars_prompt]()
when used with private attribute equal to false as:-
vars_prompt:
name: username
private: false
![private]()
* Magic Variables, and How To Access Information About Other Hosts
Even if you didn’t define them yourself, Ansible provides a few variables for you automatically. The most important of these are
> hostvars, group_names,groups, environment.
Users should not use these names themselves as they are reserved.hostvars lets you ask about the variables of another host, including facts that have been gathered about that host.
### ANSIBLE Playbook Module
Register and when