Introduction to the Ansible-playbook?
Ansible Playbook is a configuration management tool used to automate the deployment, configuration, and management of systems and applications. It is part of the Ansible automation framework, which is an open-source IT automation engine. Playbooks in Ansible are written in YAML (Yet Another Markup Language) format and contain a set of tasks that define the desired state of the system.
A playbook consists of a series of plays, where each play defines a set of tasks to be executed on a group of target hosts. Each task describes a specific action, such as installing packages, copying files, or starting services, that needs to be performed on the target hosts. Playbooks are designed to be idempotent, meaning that they can be run multiple times without causing any unintended side effects. Ansible uses SSH protocol to connect to the target hosts and execute the tasks defined in the playbook. Playbooks also support variables, conditionals, loops, and handlers, which allow for more advanced configurations and dynamic behaviour. Variables can be used to parameterize the playbook and make it more flexible.
Breakdown of a section of ansible-playbook
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
# Vars: variables that will apply to the play, on all target systems
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
# Handlers: the list of handlers that are executed as a notify key from a task
# Roles: list of roles to be imported into the play
# Three dots indicate the end of a YAML document
...
Prerequisites to run Ansible playbook on an AWS EC2 instance
Prerequisites to run the play-book are:
Security file: When you create any Ec2 instance in AWS then you would get
.pem
file which is used for security while running the playbook then open the pem file and copy the password, connect your instance and runcd /.ssh
and inside that directory runtouch ansible_key
and paste that copied password. In order to give appropriate permission runchmod 700 ./ssh
andchmod 600 anisble_key
Inventory file: An inventory file is very necessary to run the playbook and it consists of the details of the remote server. create the inventory file in
/home/ubuntu
directory.
Note: This security file and Inventory file must be created in the Master server and using this master server we control the remote server.
Creating the Inventory file
Let's create the four EC2 Ubuntu instances in AWS and among them assume one as the master server through which we are going to control the remaining three servers. give the name Master server to one instance and server1,server2, and server3 to remaining three.
Run vi host
command in /home/ubuntu
directory and paste the below detail and replace the actual public IP of servers in place of <public_ip_adress_server1>
[servers]
server1 ansible_host=<public_ip_adress_server1>
server2 ansible_host=<public_ip_adress_server2>
server3 ansible_host=<public_ip_adress_server2>
Writing ansible-playbook
Create the play_book.yaml file and paste the below content in that file
---
-
hosts: all
gather_facts: false # Gathering facts section will be skipped while running play book
vars:
message: This message goes to content of tasks
tasks:
- name: Message of the day
copy:
content: "{{message}}" # it copy the content from vars section
dest: /home/ubuntu/message1.txt
notify: file modified
handlers:
- name: file modified
debug:
msg: The file is changed # When something changed happen it notify us about change
Check the ping command with all servers
ansible all -m ping -i ~/host --private-key=~/.ssh/anisble_key
# It ping to all servers using ping module,inventory file located in
#~/host directory and secrete key located at ~/.ssh/ansible_key
Now, run the playbook using the below command
ansible-playbook play_book.yaml -i ~/host --private-key=~/.ssh/anisble_key