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 understanding.
Ansible For Everyone - Part 1
Anuvindh for AWS Community Builders ・ May 21 '22
Ansible For Everyone(Practical) - Part 2
Anuvindh for AWS Community Builders ・ Jun 10 '22
🛠️ 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
thenansible_ssh_host=
your ip address of your slave.
- Add
[production]
app1 ansible_ssh_host= ipaddress of your slave
Let's test the connections
- 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
✨ 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
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 useall
- so if we need to run as root on targets, we will be setting
become
parameter totrue
. - define task now under
task
---
- 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
- Now go to your http://yourslaveip/app1 to see the magic
🎉🎉🎉🎉Congratulations on you first ansible infrastucture deployment with aws using playbook🎉🎉🎉🎉.
I highly recommend this Tips & Tricks with Examples from spacelift
, this is def comes handy when you are writing complex ansible scripts.
Ansible documentation links
Ansible Modules link
Ansible For Everyone - Part 1
Anuvindh for AWS Community Builders ・ May 21 '22
Ansible For Everyone(Practical) - Part 2
Anuvindh for AWS Community Builders ・ Jun 10 '22
✅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)