Ansible Modules
ansible webservers -m service -a "name=httpd state=started" ansible webservers -m ping ansible webservers -m command -a "/sbin/reboot -t now"
Use Case(Taken From Ansible official page)
Let’s use Ansible’s command line tool to reboot all web servers in Atlanta, 10 at a time. First, let’s set up SSH-agent so it can remember our credentials:$ ssh-agent bash $ ssh-add ~/.ssh/id_rsaIf you don’t want to use ssh-agent and want to instead SSH with a password instead of keys, you can with--ask-pass
(-k
), but it’s much better to just use ssh-agent.Now to run the command on all servers in a group, in this case, atlanta, in 10 parallel forks:$ ansible atlanta -a "/sbin/reboot" -f 10
/usr/bin/ansible will default to running from your user account. If you do not like this behavior, pass in “-u username”. If you want to run commands as a different user, it looks like this:$ ansible atlanta -a "/usr/bin/foo" -u username
Often you’ll not want to just do things from your user account. If you want to run commands through privilege escalation:$ ansible atlanta -a "/usr/bin/foo" -u username --become [--ask-become-pass]Use--ask-become-pass
(-K
) if you are not using a passwordless privilege escalation method (sudo/su/pfexec/doas/etc). This will interactively prompt you for the password to use. Use of a passwordless setup makes things easier to automate, but it’s not required.It is also possible to become a user other than root using--become-user
:$ ansible atlanta -a "/usr/bin/foo" -u username --become-user otheruser [--ask-become-pass]
Comments
Post a Comment