โ† Back to list

๐Ÿ“ANSIBLE day-03

ANSIBLE ADHOC-COMMANDS

Skmswetha ยท 2023-10-10 15:30 ยท 3 claps ยท 4.6 min read
#ansibe #ansible-playbook #devops #devops-tool #devopsdays
Open on Medium โ†—
Wiki topics: โ˜๏ธ ยท DevOps & Cloud ๐ŸฅŠ ยท Combat Sports

๐Ÿ“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