Ansible For Everyone (Ansible Playbooks) - Part 3
Tags: webdev, tutorial, devops, productivity
Published: 2022-06-16 04:49:46

## DAY 34- Ansible For Everyone (Ansible Playbooks) - Part 3 - Day Thirty four

[![Image tweet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49arpnjjyf762k9w8vuz.png)![Image cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aafidadgyj81m8soxntp.png)](https://twitter.com/intent/tweet?text=Ansible%20For%20Everyone%20ANSIBLE%20PLAYBOOKS%20-%20Part%203%20DEPLOY%20YOUR%20INFRASTRUCTURE%20WITH%20ANSIBLE%20%2C%20a%20blog%20by%20%40anuvindhs%20%0Ahttps%3A%2F%2Fdev.to%2Faws-builders%2Fansible-for-everyone-ansible-playbooks-part-3-2dgj%20)

[100 days of Cloud on GitHub](https://github.com/anuvindhs/100daysofcloud) - [Read On iCTPro.co.nz](https://ictpro.co.nz) - [Read on Dev.to](https://dev.to/anuvindhs)

------




## 🧮 Prerequisite



Please refer to Ansible [Part 1](https://dev.to/aws-builders/ansible-for-everyone-part-1-5998) and [Part 2](https://dev.to/aws-builders/ansible-for-everyonepractical-part-2-58a5) for a detailed understanding. 
![Image Prerequisite](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3l5vzwes29kv3m4t6kw.png)

{% embed https://dev.to/aws-builders/ansible-for-everyone-part-1-5998 %}

{% embed https://dev.to/aws-builders/ansible-for-everyonepractical-part-2-58a5 %}



 

## 🛠️ Adding Ansible inventory

Lets add our cluster of servers to Ansible infrastucture
In this demo i will be only using `AnsibleSlaves-YourProdServer` as my prod server. 

### Goto your Ansible-Control-Center and update ansible hosts file

```
sudo nano /etc/ansible/hosts
```
- In this file you will find templetes to add hosts
- lets add our production group of servers
 - Add `[YourgGroupName]` 
 - next line , give a name to server which you are going to add. 
   Lets name `app1`  then `ansible_ssh_host=` your ip address of your slave.

```
[production]
app1 ansible_ssh_host= ipaddress of your slave
```

### Let's test the connections
![Image test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ruw41y6pcqujrng2w72q.png)
 
- To test all connections

```
ansible -m ping all
```

- To test all connections in a group

```
ansible -m ping production
```

- To test connection of a server named app1 in a group

```
ansible -m ping app1
```

Below image shows the result of the ping

![Image pong](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vc9h15t6aeimmvmf0vjc.png)
 

## ✨ Modules
- Modules are the commands which help to perform task,in other words modules are kind of library that help to perform tasks.
- List of all modules - [link](https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html)


 **What are Tasks?**
A set of instructions performed with modules.

------

## ▶️ Ansible Playbooks
![Image ansible](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nccox1tgdrz5zuu53c5f.png)
 
Playbooks are written in YAML also Playbooks are lists of tasks that automatically execute against hosts.

Lets create an nginx playbook
ansible modules - https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html

- lets create a YAML file 
```
sudo nano nginxplaybook.yaml
```
- yaml scriping start with three dashes `---`
- then you can give a name with start a single dash `-`
- now we will update the next parameter which is our hosts, in this case it will be `production` if you wanna do for all servers use `all`
- so if we need to run as root on targets, we will be setting `become` parameter to `true`.
- define task now under `task`
 - we will be using a `apt module` to install `nginx`. Refernce [link](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html) 
 - We have given a`name` to the task as `install nginx`.Then will be using `apt` module as we are using ubuntu. 
 - name of the `module` and `state`, refer the [link](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html) for detailed understanding.



```
---
- name: install nginx server
  hosts: production
  become: yes
  tasks:
  - name: install nginx
    apt:
      name: nginx
      state: latest
```
### Now lets run the playbook

- Before running the playbook lets check it.
`ansible-playbook nginxplaybook.yaml --check`
- Now lets run the play book 
```
ansible-playbook nginxplaybook.yaml
```

## Deploy code from git 
- Here we will deploy a website to the target server using Ansible.

```
---
- name: install nginx server
  hosts: production
  become: yes
  tasks:
  - name: install nginx
    apt:
      name: nginx
      state: latest
  - name: Clone a repo with separate git directory
    ansible.builtin.git:
      repo: https://github.com/anuvindhs/CLOUD-is-AWSome.git
      dest: /var/www/html/app1
```
- Lets run the play book 
```
ansible-playbook nginxplaybook.yaml
```
![Image playbook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x36gi371qosmb43bzilw.gif)

- Now go to your http://yourslaveip/app1 to see the magic
 
🎉🎉🎉🎉Congratulations on you first ansible infrastucture deployment with aws using playbook🎉🎉🎉🎉.

![Image first](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8l06v1c8ogjnws4jci8h.png)
 ----

I highly recommend this Tips & Tricks with Examples from [spacelift](https://spacelift.io/blog/ansible-playbooks)
, this is def comes handy when you are writing complex ansible scripts.
{% embed https://spacelift.io/blog/ansible-playbooks) %}

Ansible [documentation links](https://docs.ansible.com/)
Ansible [Modules link](https://docs.ansible.com/ansible/latest/)

{% embed https://dev.to/aws-builders/ansible-for-everyone-part-1-5998 %}
{% embed https://dev.to/aws-builders/ansible-for-everyonepractical-part-2-58a5 %}

----------

**✅Connect with me on [Twitter](https://twitter.com/intent/follow?ref_src=anuvindhs&screen_name=anuvindhs)
 🤝🏽Connect with me on [Linkedin](https://www.linkedin.com/in/anuvindhs)
🧑🏼‍🤝‍🧑🏻 Read more post on [dev.to](https://dev.to/anuvindhs) or  [iCTPro.co.nz](https://ictpro.co.nz)
💻 Connect with me on [GitHub](https://github.com/anuvindhs)**

----------
{% user anuvindhs %}