Ansible - Day 1

Ansible Basics -

What is Ansible?

  • Ansible is an open-source automation platform that can be used to manage large groups of computer systems. It helps you automate application deployment, configuration management, cloud provisioning, updating workstations and servers, and many other tasks.

  • Ansible is a configuration Management tool.

    Why Ansible?

  • Simplicity: Ansible uses plain English and YAML files for configuration, making it accessible to everyone.

  • Agentless: You don't need to install agents on target servers. Ansible works over SSH (for Linux) or WinRM (for Windows), which most systems support by default.

Ansible Components

  • Control Node: This is your Ansible machine, from where you run commands/scripts and manage everything.

  • Managed/Target Nodes: These are the servers or devices you want to automate. Ansible communicates with them via SSH (Linux) or WinRM (Windows).

  • Playbooks: Think of them as recipes. Playbooks define the tasks you want to perform on your managed nodes.

  • Modules: These are Ansible's building blocks. Modules are used to execute tasks, such as installing software or creating files.

Task 1: Execute commands on the target node from the controller node.

  • To perform this task, we will use the AWS cloud platform to create vm/instance.

  • Created 3 Redhat instances including 1 Controller node and 2 target nodes.

  • Ansible uses SSH protocol if the target node is Linux based machine and uses Winrm if the target node is a Windows-based machine.

  • Ansible should know the IP address of the Target node to execute the command.

  • Inventory - Database in Ansible.

  • #vim /etc/ansible/hosts Default location where we manage invendotry.

Steps:

  • Login to the first instance/controller node.

    #sudo su - root

    #yum install ansible-core

    #ansible --version

    Now this instance becomes a controller node as we install Ansible here.

    #ansible all --list-hosts Check all host/db inventory

    • #vim /etc/ansible/hosts

      esc > press i

      copy ip address of both target nodes and paste it on top.

      • Now login to Target Node 1 and Target Node 2:

        We will perform the same steps on both nodes.

        #sudo su - root

        #vim /etc/ssh/sshd-config

        esc > press i

        PermitRootLogin yes

        PasswordAuthentication yes

        :wq!

        #systemctl restart sshd

      • Also, need to set the root password on both target nodes.

        #passwd

      • Then update the root password in vim /etc/ansible/hosts in the controller node.

  • #ansible all --list-hosts, run this command on the controller node. Both target nodes' IPs were added successfully.

  • Note- Here when we try to execute any command controller node on the target node, it will not execute. The reason is when we do SSH to any instance, have to select yes for host key verification.

    We will make it 'yes' automatic.

    • On controller node-

#vim /etc/ansible/ansible.cfg

On the editor, copy the line

#ansible-config init --disabled -t all > ansible.cfg

:q!

#ansible-config init --disabled -t all > ansible.cfg

#vim /etc/ansible/ansible.cfg

#vim /etc/ansible/ansible.cfg

esc > press i

Here we will make some changes.

# (boolean) Set this to "False" if you want to avoid host key checking by the underlying tools Ansible uses to connect to the host

host_key_checking = False

ALL SET.

  • Now we can execute commands from the controller node

    #ansible all -m command -a cal

    #ansible all -m command -a date