Ansible Useful Ad-hoc commands

Vineet Kumar
3 min readJun 25, 2021

Ansible ad hoc commands are great for tasks you repeat rarely. For example, you could execute a quick one-liner in Ansible without writing a playbook. An ad hoc command looks like this:
$ ansible [pattern] -m [module] -a "[module options]"

Some Examples are given Below:

  1. Ansible ad-hoc against ‘all’ hosts on the inventory file and using the ‘ping’ module
    $ ansible all -m ping
    The first parameter ‘all’ for all hosts on the inventory file.
    The second parameter inside the ‘-m’ option for the module, running the ping module
    To List All Hosts: $ ansible all --list-hosts
  2. Now you can use the Ad-Hoc command against a group of hosts that are already defined on the inventory file. You can use your custom inventory file or using the default inventory file ‘/etc/ansible/hosts’
    Below is an example to run the ad-hoc command against the group of hosts called ‘testing’ that are already defined on the default inventory configuration file
    $ ansible testing -m ping
    If you’re using the custom inventory file, add the ‘-i’ option followed the inventory file name
    $ ansible hakase-testing -i hosts -m ping
  3. By default Ansible uses only 5 simultaneous processes. If you have more hosts than the value set for the fork count, Ansible will talk to them, but it will take a little longer. To reboot the [atlanta] servers with 10 parallel forks:
    $ ansible atlanta -a “/sbin/reboot” -f 10
  4. The ansible provides features for the privilege escalation against servers. If you want to run the ad-hoc command as a non-root user, you can use the ‘- -become’ option to get the root privileges and the ‘-K’ option to prompt the password.
    Run the ad-hoc command ‘fdisk -l’ as a user ‘hakase’ with the privilege option ‘- -become’ and the ‘-K’ to prompt the ‘SUDO Password’.
    $ ansible testing -m shell -a ‘fdisk -l’ -u hakase — become -K
  5. File/Directory Related
    File Transfer to Host:
    $ ansible testing -m copy -a ‘src=/home/hakase/hakase-sudo.conf dest=/etc/sudoers.d/hakase owner=root mode=0644’ -u test --become -K Download File From Host:
    $ ansible testing -m fetch -a 'src=/etc/sudoers.d/hakase dest=/home/hakase/backup/hakase-sudoers flat=yes'
    Create Directory:
    $ ansible abc -m file -a "dest = /path/user1/new mode = 777 owner = user1 group = user1 state = directory"
    Delete Directory:
    $ ansible abc -m file -a “dest = /path/user1/new state = absent”
    Copy File From Remote machine to remote Machine:
    $ ansible DB -m copy -a ‘src=run.sh dest=/root remote_src=yes’
    Rename File:
    $ ansible web -m command -a "mv /home/u2/magi/ansible.txt /home/u2/magi/2g"
    Create the file, if it does NOT exist (or) absent
    $ ansible testservers -a "touch /tmp/testfile creates=/tmp/testfile" -i ansible_hosts
    Remove the file, if it does exist (or) present
    $ ansible testservers -a "rm -rf /tmp/testfile removes=/tmp/testfile" -i ansible_hosts
  6. Update Repository and Upgrade Packages:
    update:
    $ ansible testing -m apt -a ‘update_cache=yes’
    upgrade:
    $ ansible testing -m apt -a ‘upgrade=dist update_cache=yes’
  7. Package Related:
    Install Package:
    For Debian: $ ansible testing -m apt -a ‘name=nginx state=latest’
    For Centos:$ ansible all -b -m yum -a ‘name=ntp state=present'
    Remove Package: The example below is removing the nginx package and purge all configuration related and then remove all unused packages on the system.
    $ ansible testing -m apt -a 'name=nginx state=absent purge=yes autoremove=yes'
  8. Manage Service:
    Start Service:
    ansible testing -m service -a 'name=nginx state=started enabled=yes’
    Restart Service:
    ansible testing -m service -a ‘name=nginx state=restarted’
    Stop Service:
    ansible testing -m service -a ‘name=nginx state=stopped’
  9. User Management:
    Change user password
    :
    ansible testing -m shell -a "echo '<user-name>:test@<password>'|sudo chpasswd"
    User Creation:
    $ ansible all -m user -a "name=foo password=[crypted password here]
    For Crypted passwordRun Below command and pate it in above ansible command:
    mkpasswd --method=sha-512
    Password:
    $6$ieMLxPFShvi6rao9$XEAU9ZDvnPtL.sDuSdRi6M79sgD9254b/0wZvftBNvMOjj3pHJBCIe04x2M.JA7gZ7MwpBWat1t4WQDFziZPw1.
  10. Miscellaneous
    List Of Databases:
    $ ansible DB -m shell -a "/usr/bin/mysql -uroot -password -e'show databases;'"
    Run ansible for single Ip from Host list: In below example service only in the affected server using --limit option
    $ ansible app -s -a "service ntpd restart"--limit 192.168.33.20
    List ansible module:
    $ ansible-doc -l
    $ ansible-doc -s <module-name> : To describe Ansible Module
    Append the word and it also take backup before edit:
    $ ansible DB -m lineinfile -a 'line="HELLO" dest=/etc/abc.conf insertafter=BOF backup=yes'
    System Related Info: Enternet info
    $ ansible others -m setup -a 'filter=ansible_eth0*'
    Note : stat module : give all file information uid gid time inode creation time etc
    When all fails: Using raw module to run command similar to running directly via SSH:
    ansible test -m raw -s -a "yum install libselinux-python -y"
    Run script from host machine: Save the any script <script-name.sh> in the same directory as your hosts file.
    ansible webservers -i hosts -m script -a "check-db-access.sh"

--

--