DEV Community

Cover image for Ansible For Everyone (Ansible Playbooks) - Part 3
Anuvindh for AWS Community Builders

Posted on • Updated on

Ansible For Everyone (Ansible Playbooks) - Part 3

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

Image tweet

100 days of Cloud on GitHub - Read On iCTPro.co.nz - Read on Dev.to


🧮 Prerequisite

Please refer to Ansible Part 1 and Part 2 for a detailed understanding.
Image Prerequisite

🛠️ 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Let's test the connections

Image test

  • To test all connections
ansible -m ping all
Enter fullscreen mode Exit fullscreen mode
  • To test all connections in a group
ansible -m ping production
Enter fullscreen mode Exit fullscreen mode
  • To test connection of a server named app1 in a group
ansible -m ping app1
Enter fullscreen mode Exit fullscreen mode

Below image shows the result of the ping

Image pong

✨ 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

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


▶️ Ansible Playbooks

Image ansible

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
Enter fullscreen mode Exit fullscreen mode
  • 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
    • We have given aname 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 for detailed understanding.
---
- name: install nginx server
  hosts: production
  become: yes
  tasks:
  - name: install nginx
    apt:
      name: nginx
      state: latest
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Lets run the play book
ansible-playbook nginxplaybook.yaml
Enter fullscreen mode Exit fullscreen mode

Image playbook

🎉🎉🎉🎉Congratulations on you first ansible infrastucture deployment with aws using playbook🎉🎉🎉🎉.

Image first


I highly recommend this Tips & Tricks with Examples from spacelift
, this is def comes handy when you are writing complex ansible scripts.

Ansible Playbooks: Complete Guide with Examples

What are Ansible playbooks? Learn how to structure, write, and run a playbook. See the practical examples.

favicon spacelift.io

Ansible documentation links
Ansible Modules link



✅Connect with me on Twitter
🤝🏽Connect with me on Linkedin
🧑🏼‍🤝‍🧑🏻 Read more post on dev.to or iCTPro.co.nz
💻 Connect with me on GitHub


Top comments (0)