# Ansible

### **what is ansible**

Ansible is an open-source configuration management and automation tool that helps to streamline the process of deploying, configuring, and managing software applications and IT infrastructure.

With Ansible, you can automate tasks such as software deployment, server configuration, and application management. It uses a simple and easy-to-read language called YAML (Yet Another Markup Language) to describe automation tasks in the form of Ansible Playbooks.

One of the key advantages of Ansible is its agentless architecture, which means that it does not require any additional software to be installed on the remote hosts. Instead, Ansible uses SSH (Secure Shell) to connect to the remote hosts and execute tasks. This makes Ansible easy to set up and use, even in complex environments.

Ansible also provides a rich set of modules for performing a wide variety of tasks, including package installation, file management, and network configuration. It has a large and active community that provides support and contributes to the development of new features and modules.

### **ansible master and node configuration**

To configure Ansible master and node, you need to follow these steps:

1. Install Ansible: First, you need to install Ansible on your master and nodes. You can install Ansible using your operating system's package manager or by downloading it from the Ansible website.
    
2. Configure SSH: Ansible uses SSH to connect to remote nodes. You need to configure SSH access between the master and nodes by creating SSH keys and adding them to the authorized\_keys file on the nodes.
    
3. Set up inventory: Inventory is a file that contains a list of nodes that Ansible will manage. You need to create an inventory file on the Ansible master that contains the IP addresses or hostnames of the nodes that you want to manage.
    
4. Test connection: Once you have configured SSH and set up inventory, you can test the connection between the Ansible master and nodes by running the "ping" module. This module will send a ping command to the nodes and verify that they are accessible.
    
5. Write playbooks: Playbooks are YAML files that contain a list of tasks to be executed on the nodes. You can write playbooks to install packages, configure servers, and perform other tasks on the nodes.
    
6. Execute playbooks: You can execute playbooks on the nodes using the "ansible-playbook" command. This command will run the tasks specified in the playbook on the nodes.
    

Overall, configuring Ansible master and nodes involves setting up SSH, creating an inventory file, writing playbooks, and executing them on the nodes.

Step: 1

Launch 3 instances on AWS , 2 ubuntu 1 amz-linux

Login into those instances

```bash
# sudo apt update -y
# sudo yum update -y
```

Step: 2

Now setup the hostname for all server.

```bash
$ sudo vi /etc/hostname

ex:
ubuntu-server
```

Step: 3

now configure DNS entries in each server

```bash
# sudo vi /etc/hosts

example
172.31.33.240 ansible-control
172.31.42.23 ubuntu
172.31.33.6 centos


save & exit
```

Step: 4

install ansible into your control machine

```bash
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
```

Step: 5

Create a common user in all server and assign sudo privileges

```bash
sudo adduser ansadm # ubuntu server
sudo useradd ansadm # azm-linux machine
sudo passwd ansadm # amz-linux machine
```

Step: 6

```bash
sudo visudo
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
ansadm ALL=(ALL) NOPASSWD:ALL

save & exit 
```

Step: 7

Make sure PasswordAuthentication is yes

```bash
vi /etc/ssh/sshd_config
PasswordAuthentication yes

save and exit

$ sudo systemctl restart ssh (ubuntu)
$ sudo systemctl restart sshd (centos)
$ sudo systemctl enable sshd
```

Step: 8

On ansible control-machine

```bash
su - ansadm

# generate ssh key by running below command
ssh-keygen

cd .ssh
ssh-copy-id ansadm@ubuntu
ssh-copy-id ansadm@centos
```

Step: 9

Note: Default configuration of ansible is **/etc/ansible/ansible.cfg** default inventory file is **/etc/ansible/hosts**

Edit the ansible hosts file n update your node entries

```bash
$ sudo vi /etc/ansible/hosts

example
#192.168.0.1
#test.com
centos
ubuntu

save & exit.
```

Step: 10

Check the connectivity from the ansible control machine to your node machine

```bash

ansible -m ping all
```
