Ansible: no time to play…book

Ansible is an open-source software provisioning, configuration management, and application-deployment tool, which basically provides automation for different infrastructure related repetitive tasks.

Ad-hoc commands are very powerful yet often overlooked . They come in handy when you need to reboot servers, copy files, manage packages and users , basically they’re great when you want to run tasks instantly.


The default module for the /usr/bin/ansible command-line utility is the command module, e.g display the users that are currently logged on the target machine:

$ ansible <target_machine>  -a “who” -i <inventory>

it’s equivalent with:

$ ansible <target_machine>  -m command -a “who” -i <inventory>

Bellow you can find different use-cases for ad-hoc commands:

  • Transfer file to target_machine, e.g .gitconfig:
$ ansible <target_machine> -m copy -a "src=/home/user/alex/.gitconfig dest=/tmp/.gitconfig" -i <inventory>
  • Fetch file from target_machine, e.g. download /home/tmp/file :
$ ansible <target_machine> -m fetch -a "src=/home/tmp/file dest=/home/alex/ flat=yes" -i <inventory>
  • Delete file from target-machine, e.g. /home/alex/test_file :
$ ansible <target_machine> -m file -a "dest=/home/alex/test_file state=absent" -i <inventory>
  • Check the package manager on target machine :
$ ansible <target_machine> -m setup -a "filter=ansible_pkg_mgr" -i <inventory>
  • Check the file-system on the target_machine:
$ ansible <target_machine> -m shell -a "df -hP" -i <inventory>
  • Restart service from the target_machine (privilege escalation is achieved using — become flag) e.g. ngnix :
$ ansible <target_machine> -m service -a 'name=nginx state=restarted' --become

Last but not least, it’s wise before running the ad-hoc commands on the target machine, to do a dry-run and this can be done using the --check flag:

$ ansible <target_machine> -m file -a "dest=/home/alex/test_file state=absent" -i <inventory> --check

Use these commands on your ansible setup and run different tasks without writing any playbooks .

Seasoned DevOps engineer — Jack of all trades master of none