Tuesday, July 26, 2022

Ansible

Ansible Controller

Ansible Target 1

Ansible Target 2


Post OS provision with 2 vCPU, 2 GB Memory and Bridged network.

Controller

vim /etc/hostname

ansiblecontroller

vim /etc/hosts

127.0.0.1 localhost ansiblecontroller

::1            localhost ansiblecontroller

shutdown -r now

Target 1

vim /etc/hostname

target1

vim /etc/hosts

127.0.0.1 localhost target1

::1            localhost target1

shutdown -r now

Target 2

vim /etc/hostname

target2

vim /etc/hosts

127.0.0.1 localhost target2

::1            localhost target2

shutdown -r now

Controller

yum install epel-release

yum install ansible

ansible --version

Ansible Inventory

Done through SSH

Linux - SSH

Windows - Powershell Remoting

Default location of Inventory files -

/etc/ansible/hosts

Inventory Parameters

ansible_connection - ssh /winrm / localhost

ansible_port - 22/5986

ansible_user - root/administrator

ansible_ssh_pass - Password

Sample Inventory File -

web ansible_host=server1.example.com ansible_connection=ssh ansible_user=root

db ansible_host=server2.example.com ansible_connection=winrm ansible_user=administrator

mail ansible_host=server3.example.com ansible_connection=ssh ansible_ssh_pass=<Password>

web2 ansible_host=server4.example.com ansible_connection=winrm

localhost ansible_connection=localhost

Defining password is not good, so suggested to use passwordless ssh.

Stop and disable the Firewall service on the controller and target systems.

Examples -

pwd - /root

mkdir ansible-demos

cd ansible-demos

cat > inventory.txt

target1 ansible_host=192.168.1.24 ansible_ssh_pass=<password>

ansible target1 -m ping -i inventory.txt

Add the target2 now in inventory.txt

target2 ansible_host=192.168.1.25 ansible_ssh_pass=<password>

ansible target2 -m ping -I inventory.txt

This would fail

vim /etc/ansible/ansible.cfg

Uncomment the below line

host_key_checking=False

ansible target2 -m ping -I inventory.txt

YAML

Key value pair -

fruit: apple

vegetable: cucumber

Array -

fruits:

- orange

- apple

vegetables:

- cucumber

- carrot

Dictionary - 

Banana:

    Calories: 105

    Fat: 0.4g

Grapes:

    Calories: 62

    Fat: 0.3g

Key Value / Dictionary/Lists

Fruits:    

-    Banana:

        Calories: 105

        Fat: 0.4g

-    Grapes:

        Calories: 62

        Fat: 0.3g

Dictionaries are mainly used while representing properties of a particular item.
Lists are used for listing the items of same category. 
However, lists can have dictionaries defined in them.

The order for the dictionary for example calories and fat need to be in same order. 

Ansible Playbooks





No comments: