An ad-hoc command is something that you might type in to do something really quick, but don’t want to save for later. Helps us to understand a little bit about how ansible works. Perhaps it is something that is for one-time use as well.
Make sure to remember to have ssh-keys all set up.
Ad-hoc commands
ansible nancy -m shell -a 'echo $TERM'
nancy is the group of computers the comand will apply to
shell is the name of the module to execute
-a is the stuff we will pass into the shell module.
Ad-hoc commands
ansible atlanta -a "/usr/bin/foo" -u username --become [--ask-become-pass]
Allows you to become another user to execure the command. Will prompt you for the sudo password.
Ad-hoc commands
ansible nancy -m copy -a "src=foo.txt dest=/tmp/foo"
This will scp foo.txt from my local machine to the destination on all target machines in the nancy group.
What will this do?
ansible nancy -m shell -a 'cat /tmp/foo'
Ad-hoc commands (Files)
Guess what these do:
ansible nancy -m file -a "dest=/tmp/foo mode=600"
ansible nancy -m file -a "dest=/tmp/foo mode=600" owner=carlos group=carlos"
ansible nancy -m file -a "dest=/tmp/bar mode=755 state=directory"
ansible nancy -m file -a "dest=/tmp/bar state=absent"
More Ad-hoc commands (APT)
Update apt repo:
ansible nancy -m apt -a "update_cache=yes" --become --ask-become-pass
Install apt package:
ansible nancy -m apt -a "name=apache2 state=present" --become --ask-become-pass
Ad-hoc commands (APT)
Remove package:
ansible nancy -m apt -a "name=apache2 state=absent" --become --ask-become-pass
Ad-hoc commands (Users)
Add user:
ansible all -m user -a "name=foo password=<crypted password here>"
ansible all -m user -a "name=foo password=.zxM.u/V5xrtc" --become --ask-become-pass
I generated encrypted password above with mkpasswd
Ad-hoc commands (Users)
ansible all -m user -a "name=foo state=absent" --become --ask-become-pass
Ad-hoc commands (Services)
Ensure that httpd process is started:
ansible all -m service -a "name=httpd state=started" --become --ask-become-pass
Or restart
ansible all -m service -a "name=httpd state=restarted" --become --ask-become-pass