Difference between revisions of "Ansible Ad-Hoc Commands"

From rbachwiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Ad-hoc commands=
=Ad-hoc commands=
''' Update and upgrade servers '''
ansible all -m shell -a 'apt update' --private-key ~/.ssh/ansible
ansible -all -m shell -a "apt upgrade -y" --private-key ~/.ssh/ansible


'''Ansible [target] -m [module] -a [module-options]'''
'''Ansible [target] -m [module] -a [module-options]'''
*Ping localhost
==Ping localhost==
  # ansible localhost –m ping
  # ansible localhost –m ping


*Creating a file on all remote clients
==Creating a file on all remote clients==
  # ansible all –m file –a “path=/home/iafzal/adhoc1 state=touch mode=700”
  # ansible all –m file –a “path=/home/iafzal/adhoc1 state=touch mode=700”


*Deleting a file on all remote clients
==Deleting a file on all remote clients==
  # ansible all –m file –a “path=/home/iafzal/adhoc1 state=absent”
  # ansible all –m file –a “path=/home/iafzal/adhoc1 state=absent”


*Copying a file to remote clients
==*Copying a file to remote clients==
  # ansible all –m copy –a “src=/tmp/adhoc2 dest=/home/iafzal/adhoc2”
  # ansible all –m copy –a “src=/tmp/adhoc2 dest=/home/iafzal/adhoc2”


*Installing package (telnet and httpd-manual)
==Installing package (telnet and httpd-manual)==
  # ansible all –m yum –a “name=telnet state=present”
  # ansible all –m yum –a “name=telnet state=present”
  # ansible all –m yum –a “name=httpd-manual state=present”.  
  # ansible all –m yum –a “name=httpd-manual state=present”.  


*Starting httpd package service
== Remove package==
ansible all -m apt -a "name=apache2 state=absent" --private-key ~/.ssh/ansible
 
==*Starting httpd package service==
  # ansible all –m service –a “name=httpd state=started”
  # ansible all –m service –a “name=httpd state=started”


*Start httpd and enable at boot time
==*Start httpd and enable at boot time==
  # ansible all –m service –a “name=httpd state=started enabled=yes”
  # ansible all –m service –a “name=httpd state=started enabled=yes”


Checking httpd service status on remote client
==*Checking httpd service status on remote client==
# ansible all –m shell -a “systemctl status httpd”
# ansible all –m shell -a “systemctl status httpd”


Remove httpd package
==*Remove httpd package==
# ansible all –m yum –a “name=httpd state=absent”
# ansible all –m yum –a “name=httpd state=absent”
OR
==*OR==
# ansible all –m shell -a “yum remove httpd”.
# ansible all –m shell -a “yum remove httpd”.


*Creating a user on remote clients
==*Creating a user on remote clients==
  # ansible all –m user –a “name=jsmith home=/home/jsmith shell=/bin/bash state=present”
  # ansible all –m user –a “name=jsmith home=/home/jsmith shell=/bin/bash state=present”


*To add a user to a different group
==*To add a user to a different group==
  # ansible all –m user –a “name=jsmith group=iafzal”
  # ansible all –m user –a “name=jsmith group=iafzal”


*Deleting a user on remote clients
==*Deleting a user on remote clients==
  # ansible all –m user –a “name=jsmith home=/home/jsmith shell=/bin/bash state=absent”
  # ansible all –m user –a “name=jsmith home=/home/jsmith shell=/bin/bash state=absent”
OR
==OR==
  # ansible all –m shell –a “userdel jsmith”
  # ansible all –m shell –a “userdel jsmith”


*Getting system information from remote clients
==*Getting system information from remote clients==
  # ansible all –m setup
  # ansible all –m setup


*You can run commands on the remote host without a shell module e.g. reboot client1
==*You can run commands on the remote host without a shell module e.g. reboot client1==
  # ansible client1 –a “/sbin/reboot”
  # ansible client1 –a “/sbin/reboot”



Latest revision as of 15:48, 28 June 2022

Ad-hoc commands

Update and upgrade servers

ansible all -m shell -a 'apt update' --private-key ~/.ssh/ansible 
ansible -all -m shell -a "apt upgrade -y" --private-key ~/.ssh/ansible 

Ansible [target] -m [module] -a [module-options]

Ping localhost

# ansible localhost –m ping

Creating a file on all remote clients

# ansible all –m file –a “path=/home/iafzal/adhoc1 state=touch mode=700”

Deleting a file on all remote clients

# ansible all –m file –a “path=/home/iafzal/adhoc1 state=absent”

*Copying a file to remote clients

# ansible all –m copy –a “src=/tmp/adhoc2 dest=/home/iafzal/adhoc2”

Installing package (telnet and httpd-manual)

# ansible all –m yum –a “name=telnet state=present”
# ansible all –m yum –a “name=httpd-manual state=present”. 

Remove package

ansible all -m apt -a "name=apache2 state=absent" --private-key ~/.ssh/ansible 

*Starting httpd package service

# ansible all –m service –a “name=httpd state=started”

*Start httpd and enable at boot time

# ansible all –m service –a “name=httpd state=started enabled=yes”

*Checking httpd service status on remote client

# ansible all –m shell -a “systemctl status httpd”

*Remove httpd package

# ansible all –m yum –a “name=httpd state=absent”

*OR

# ansible all –m shell -a “yum remove httpd”.

*Creating a user on remote clients

# ansible all –m user –a “name=jsmith home=/home/jsmith shell=/bin/bash state=present”

*To add a user to a different group

# ansible all –m user –a “name=jsmith group=iafzal”

*Deleting a user on remote clients

# ansible all –m user –a “name=jsmith home=/home/jsmith shell=/bin/bash state=absent”

OR

# ansible all –m shell –a “userdel jsmith”

*Getting system information from remote clients

# ansible all –m setup

*You can run commands on the remote host without a shell module e.g. reboot client1

# ansible client1 –a “/sbin/reboot”




Ansible Menu