---
# System prepended metadata

title: DevOps Tools (Ansible)
tags: [Terraform, Docker, Devops, Ansible, K8s, Jenkins]

---

# DevOps Tools (Ansible)

###### tags: `Devops` `K8s` `Docker` `Jenkins` `Ansible` `Terraform`


## Ansible


**Ansible (following push process) is a configuration management tool. 
Chef(following pull process) and puppet is also configuration management tools.**
```java=
1. New Configuration management tool using YAML (Yet another 
markup language)scripting language.
2. New tool as compare to chef and puppet. Redhat is currently 
managing this tool. Have GUI tool names Ansible Tower (paid).
3. Ansible is an open source configuration management, 
deployment and orchestration tool it aims to provide larage 
productivity gains to a wide varity of automation challenges.
4. Michal Dehaan developed ansible and the ansible project 
began in february 2012.
5. Ansible is avilable for RHEL, Debian, CentOs, Oracle Linux 
and Windows as well.
6. Can use this tool whether your servers are in on-prem or in 
the cloud.
7. It turns your code into infrastructure i.e. your  computing 
environment has some of the same attributes as your application.
8. Ansible Server can use playbook to access node via SSH.
```
### Advantage:
```java=
1. Free for everyone except enterprise version.
2. It is very consitent and lightweight and no contraints 
regarding the OS or underlyiung hardware are present.
3. It is very secure due to its agentless capabilities and 
open SSH security features.
4. Ansible does not need any special system admin skills to 
install and use it (YAML).
5. Use Push Mechanism.
```
### Disadvantages:
```java=
1. Insufficient user interface, through ansible tower is GUI, 
but it is still in development stages.
2. Cannot achieve full automation by ansible.
3. New to the market, therefore limited support and document is avialable.
```
### Terms use in Ansible:
```java=
1. Ansible server: The Machine where ansible as installed and 
from which all tasks and playbooks will be ran.
2. Module: Basically a module is a command or set of similar 
command meant to be executed on the client-side.
3. Taskl: A task is a section that consists of a single procedure 
to the completed.
4. A way of organising tasks and related files to be later 
called in a playbook.
5. Fact: Information fatched from the client system the global 
variables with the gather-facts operations.
6. Inventory: File containing data about the ansible client 
servers.
7. Play: Execution of a playbook.
8. Handler: Task which is called only if a notifier is present.
9. Notifier: Section attributed to a task which calles a 
handler if the output is changed.
10. Playbooks: it consist code in YAML format, which describe
task to be executed.
11. Host: Nodes, Which are automated in ansible.
```

### Install the Ansible
```javascript=
# sudo apt-update
# sudo apt install ansible
# 
```
### inventory (Store the detail about the other machines like ip.hostnames etc)
```javascript=
<IPaddrress1>
<IPaddrress1>
<IPaddrress1>
```
### Install the SSH on each machine to access via Ansible.
### Setting up the Github Repo to store Ansible playbooks.
follow this link to setup git https://hackmd.io/Po9zrse7QOOccl-rgPJO-Q
## Ad-hoc Commands of Ansible:
checking the connection is eastablished well with each server.
```javascript=
# ansible all --key-file ~/.ssh/ansible -i inventory -m ping
```
### Create Ansible.cfg file (we do have one file in /etc folder but when we create this file in working directory this file is more priortise)
```javascript=
[defaults]
private_key_file = ~/.ssh/ansible
```
When you create ansible.cfg file now you need to write the short command like:
```javascript=
# ansible all -m ping
```
### List of host:
```javascript=
# ansible all --list-hosts
```
### check the detail of all hosts or one hosts specify using limit and Ip addr:
```javascript=
# ansible all -m gather-facts 
# ansible all -m gather-facts --limit <ip address>
```

## Elevated Ad-hoc Commands of Ansible: (like apt lock in linux same in ansible ans solve this --become --ask-become-pass) It will ask the password so use sudo password
```javascript=
# ansible all -m apt -a update_cache=true --become --ask-become-pass
```
## Lets Install vim-nox or tmux on all the servers using sudo priviledge. 
```javascript=
# ansible all -m apt -a name=vim-nox --become --ask-become-pass
# ansible all -m apt -a name=tmux --become --ask-become-pass
# ansible all -m apt -a "name=snapd state=latest" --become --ask-become-pass
```

## Lets update the specific package snapd on all the servers. the argument "name=snapd state=latest" 
```javascript=
# ansible all -m apt -a "name=snapd state=latest" --become --ask-become-pass
```

## Lets update all the packages snapd on all the servers. the argument "dist-upgrade"
```javascript=
# ansible all -m apt -a "dist-upgrade" --become --ask-become-pass
```

## Create a Sample playbook and install. e.g. Install apache2 package into all the hosts.
```javascript=
# nano install_apache.yml
```
![](https://i.imgur.com/yG1vsGU.png)

Execute the playbook
```javascript=
# ansible-playbook --ask-become-pass install_apache.yml
```
Install and update the repo apt, install packages sample: 

![](https://i.imgur.com/BWRMFcs.png)

## Remove the existing packages using playbook:
```javascript=
# ansible-playbook --ask-become-pass remove_apache.yml
```
![](https://i.imgur.com/mvcqQEc.png)
