User Manual for Automation using Ansible .
Published: 2024-05-01 13:23:36

Ansible is a powerful automation tool that simplifies complex configuration tasks, application deployment, and intra-service orchestration. Whether you're a system administrator, a developer, or a DevOps engineer, understanding how to set up and configure Ansible can significantly streamline your operations. This guide will walk you through setting up your Ansible control node and preparing your target machines for management, ensuring you have a solid foundation for running Ansible playbooks.

---

##**Section 1: Setting Up the Ansible Control Node**##

The control node is where Ansible is installed and from which all tasks are managed. Follow these steps to prepare your control node:

###**1. Grant Sudo Privileges**###
 
Why Sudo?
Sudo privileges allow the user to perform tasks that require administrative or root permissions, such as installing software or modifying important system files.

**How to Set it Up:**

- 
Switch to the root user to grant privileges : You will be asked to enter the root password of your machine.
```
su root
```

- Edit the sudoers file to add your user:
```
sudo visudo
```
In the opened file, add the following line, replacing <NAME_OF_YOUR_VM_USER> with your username. This grants sudo privileges to your user:
```
%<NAME_OF_YOUR_VM_USER> ALL=(ALL:ALL) ALL
```
Save and close the file.

###**2. Install and Setup OpenSSH-Server**###
SSH (Secure Shell) is vital for secure communications between machines. It allows the Ansible control node to connect and execute commands on target machines securely.

- Install OpenSSH-Server:

```
sudo apt install openssh-server
```

- Configure SSH to allow key-based authentication and root login:

```
sudo nano /etc/ssh/sshd_config
```
Find and modify the following lines (remove the # to uncomment):

```
PubkeyAuthentication yes
PermitRootLogin yes
```

- Restart the SSH service to apply your changes:

```
sudo systemctl restart ssh
```

- Generate an SSH key pair on the control node:

```
ssh-keygen
```
Follow the prompts to create your keys. This step is crucial for secure, passwordless login to the target machines.

###**3: Install and Configure Ansible**###
Ansible automates software configuration, application deployment, and other IT needs through scripts called "playbooks".

- 
Update your package list and install the required software:

```
sudo apt update
sudo apt install software-properties-common
```

- 
Add the official Ansible repository and install Ansible:

```
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
```
###**4. Edit the Inventory File**###
This file tells Ansible about the machines it can manage. You can define groups of machines, individual IP addresses, and even variables relevant to each host.

- Open the Ansible inventory file:

```
sudo nano /etc/ansible/hosts
```

- Add your target machines under a group named [servers] or anything of your choice but do not forget to replace it in your playbook:

```
[servers]
192.168.1.2
192.168.1.3
```
Save and exit the file. This setup allows Ansible to identify and group the servers you want to manage.

###**5. Copy SSH Key to Managed Nodes**###

Copy your public SSH key to each target machine to enable passwordless SSH access:

```
ssh-copy-id root@<ip_address_of_target_machine>
```
Replace <ip_address_of_target_machine> with the actual IP address of your target machine.

###**6. Creating an Ansible Playbook**###
What is a Playbook?
Playbooks are the core files where Ansible code is written. They describe the tasks to be performed by Ansible on target machines.

- Create a new playbook file:
```
nano NAME_OF_PLAYBOOK.yml
```
Replace NAME_OF_PLAYBOOK with the desired name of your playbook. Enter the code for your playbook. Ensure you write YAML code correctly with all the variables included in the inventory file, as it is sensitive to indentation.

###**7. Run Ansible Playbook**###
Run your playbook to configure your servers automatically:

```
ansible-playbook NAME_OF_PLAYBOOK.yml
```
This command will execute the playbook and, if set up correctly, manage your servers without errors.

##**Section 2: Preparing Target Machines for Ansible Automation**##
Preparing target machines correctly is crucial for effective management using Ansible. This section outlines the steps needed to ensure that your target machines are ready to be managed from your Ansible control node.

###**1. Grant Sudo Privileges**###

- 
Switch to the root user to grant privileges : You will be asked to enter the root password of your machine.
```
su root
```

- Edit the sudoers file to add your user:
```
sudo visudo
```
In the opened file, add the following line, replacing <NAME_OF_YOUR_VM_USER> with your username. This grants sudo privileges to your user:
```
%<NAME_OF_YOUR_VM_USER> ALL=(ALL:ALL) ALL
```
Save and close the file.

###**2. Install and Setup OpenSSH-Server**###

- Install OpenSSH-Server:

```
sudo apt install openssh-server
```

- Configure SSH to allow key-based authentication and root login:

```
sudo nano /etc/ssh/sshd_config
```
Find and modify the following lines (remove the # to uncomment):

```
PubkeyAuthentication yes
PermitRootLogin yes
```

- Restart the SSH service to apply your changes:

```
sudo systemctl restart ssh
```
With this setup, you are now equipped to leverage Ansible's powerful automation capabilities to streamline software provisioning, configuration management, and deployment tasks. This not only enhances efficiency but also ensures consistency and reliability in your IT infrastructure management.

Keep in mind to regularly update your configurations, maintain security best practices, and refer to Ansible documentation for advanced use cases and optimizations. Happy automating!