Difference between revisions of "Ansible Ad-Hoc Commands"

From rbachwiki
Jump to navigation Jump to search
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]'''

Revision as of 21:10, 24 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