Ansible For Everyone (Ansible Playbooks) – Part 3

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

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 understa…


This content originally appeared on DEV Community and was authored by Anuvindh Sankaravilasam

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

Image tweetImage cover

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
  • 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

  • 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

✨ 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
  • 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

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
  • Now lets run the play book
ansible-playbook nginxplaybook.yaml

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.

Working with Ansible Playbooks - Tips & Tricks with Examples

Playbooks are one of the basic components of Ansible. Learn how to use them and see detailed examples with best practices.

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


This content originally appeared on DEV Community and was authored by Anuvindh Sankaravilasam


Print Share Comment Cite Upload Translate Updates
APA

Anuvindh Sankaravilasam | Sciencx (2022-06-16T04:49:46+00:00) Ansible For Everyone (Ansible Playbooks) – Part 3. Retrieved from https://www.scien.cx/2022/06/16/ansible-for-everyone-ansible-playbooks-part-3/

MLA
" » Ansible For Everyone (Ansible Playbooks) – Part 3." Anuvindh Sankaravilasam | Sciencx - Thursday June 16, 2022, https://www.scien.cx/2022/06/16/ansible-for-everyone-ansible-playbooks-part-3/
HARVARD
Anuvindh Sankaravilasam | Sciencx Thursday June 16, 2022 » Ansible For Everyone (Ansible Playbooks) – Part 3., viewed ,<https://www.scien.cx/2022/06/16/ansible-for-everyone-ansible-playbooks-part-3/>
VANCOUVER
Anuvindh Sankaravilasam | Sciencx - » Ansible For Everyone (Ansible Playbooks) – Part 3. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/16/ansible-for-everyone-ansible-playbooks-part-3/
CHICAGO
" » Ansible For Everyone (Ansible Playbooks) – Part 3." Anuvindh Sankaravilasam | Sciencx - Accessed . https://www.scien.cx/2022/06/16/ansible-for-everyone-ansible-playbooks-part-3/
IEEE
" » Ansible For Everyone (Ansible Playbooks) – Part 3." Anuvindh Sankaravilasam | Sciencx [Online]. Available: https://www.scien.cx/2022/06/16/ansible-for-everyone-ansible-playbooks-part-3/. [Accessed: ]
rf:citation
» Ansible For Everyone (Ansible Playbooks) – Part 3 | Anuvindh Sankaravilasam | Sciencx | https://www.scien.cx/2022/06/16/ansible-for-everyone-ansible-playbooks-part-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.