๐ANSIBLE day-03
ANSIBLE ADHOC-COMMANDS
๐ANSIBLE day-03
ANSIBLE ADHOC-COMMANDS
some basic commands are:
๐It will ping all the servers which you have mentioned in inventory file(/etc/ansible/hosts)
ansible all -m ping
๐It will display the output in single line
ansible all -m ping -o
ansible all -m shell -a 'date'
๐To check the Date

๐To check the OS version
ansible all -m shell -a ' cat /etc/*release* '

๐ To check the service sshd status
ansible all -m shell -a ' service sshd status'

๐ To check the git version
ansible all -m shell -a 'git --version'

git not installed.
๐To install git in slave machine
ansible all -m shell -a 'sudo yum install -y git-all'

๐To install the httpd package in latest version
ansible all -m yum -a 'sudo yum install httpd -y'

๐Q: Irrespective of underlying os which module we can use to manage packages(softwares) using package manager in ansible?
A: Ansible introduced โpackage manager โ to work with underlying package manager
๐WHAT IS YAML?
โ The full form of yaml is yet another markup language.
โ This yaml file is both human readable and machine-readable format.
โ The extension of yaml is in .yml
โ The official website is โ https://yaml.org/
โ Let us see how to write yml file โ
โ The yml file starts with โ โ and ends with โ -
โ Between these โ โ we can write our requirements and the requirements should be entered in key value pair
๐WHAT IS PLAYBOOK:
โ Playbook are single yaml file that contains one or more plays in the list.
โ Here the plays represent set of tasks to be performed when the playbook executed.
โ The task can be โ
๐Executing a command
๐Executing a script
๐Install the package
๐Remove the package
๐Updating the existing package.
โ The playbook contains the following sections:
โ Starting and ending section ( โ -) and ( โ -)
โ Second the host section [ on which host or slave we need to run this task]
โ Third variable section [ we can define the set of variables to define in yml file]
โ Task section โ this section lists out the all the task to be executed on the target host machine. It specifies the list of modules.
๐Sample yml file to ping all host servers:
๐demo.yml
---
- hosts: all
tasks:
- name: Ping All Host Nodes
ping:
remote_user: ansible
how can we know that our written syntax is correct? There is one way we can let ansible to check by using the command โ ansible-playbook <filename> โ syntax-check

๐Then we can execute the command by using โ ansible-playbook <filename>
ansible-playbook ping.yml

๐We can able to ping the slave server. If we want to know more details we can do one more command โ ansible-playbook ping.yml -vv
ansible-playbook ping.yml -vv

๐Again, if we want to full details, we need to increase one more command โ ansible-playbook ping.yml -vvv
ansible-playbook ping.yml -vvv

๐It will provide help on ansible-playbook command
ansible-playbook --help
๐It will check the syntax of a playbook
ansible-playbook ping.yml --syntax-check
๐It will do in dry run
ansible-playbook ping.yml --check
๐It will display the which hosts would be effected by a playbook before run

ansible-playbook ping.yml --list-hosts
๐It execute one step-at-a-time, confirm each task before running with (N)o/(Y)es/(c)ontinue
ansible-playbook ping.yml --step
๐Create a playbook called create file.yml
- hosts: all
tasks:
- name: Create file in all host nodes
file:
path: /home/ec2-user/hello.txt
state: touch

๐created file in โslave machineโ

๐copy the content into the created file
- hosts: all
tasks:
- name: Copy the content into the file
copy: content="welcome swetha!"dest="/home/ec2-user/hello.txt"


vi httpd.yml
๐To install httpd package in all host nodes
# write a playbook to install httpd package in all host nodes
- hosts: all
become: true
tasks:
- name: install httpd package
yum:
name: httpd
state: latest

vi httpdstart.yml
๐To start the httpd server in all host nodes
# write a playbook to start httpd server in all host nodes
- hosts: all
become: true
tasks:
- name: install httpd package
yum:
name: httpd
state: latest
- name: start httpd server
service:
name: httpd
state: started

๐To copy the public ip address and paste the browser

๐write a playbook to install httpd, copy index.html and start the httpd server
- hosts: all
become: true
tasks:
- name: install httpd package
yum:
name: httpd
state: latest
- name: copy index.html file
copy:
src: index.html
dest: /var/www/html/index.html
- name: start httpd service
service:
name: httpd
state: started
๐create one file called index.html in master machine
<h1>Heyyyyyyy swetha!</h1>

๐To copy the public ip address and paste the browser

๋ฉํ๋ฐ์ดํฐ
- post_id
- 8bce43e06ce5
- slug
- ansible-day-03-8bce43e06ce5
- url
- https://medium.com/@skmswetha22/ansible-day-03-8bce43e06ce5
- canonical_url
- https://medium.com/@skmswetha22/ansible-day-03-8bce43e06ce5
- author_url
- https://medium.com/@skmswetha22
- status
- ok
- fetched_at
- 2026-07-25 02:51:09