Ansible - Adhoc Run
- The command module is used by default if nothing else is specified.
- Prompting for connection password isn’t needed if you use an SSH key
Super simple examples:
ansible all -m ping -u user1 -k
ansible all -a "uname -a" -k
Use sudo for root perms and prompt for both connection password and sudo password:
ansible all -a "cat /etc/shadow" -bkK
Common useful options:
--become ( -b ) ( typcially sudo for root )
--ask-pass ( -k )
--ask-become-pass ( -K ) # prompt for become password
--become_user=produser # doesn't imply --become
--become-method=su # sudo is default
--check -C
-u user1 # connect as a user
-f 10 # 10 parallel forks
More examples:
ansible host1 -a "/sbin/reboot" -bkK
ansible prod1 -a "/sbin/reboot" -bkK
ansible webhosts -a "/sbin/reboot" -bkK -f 10 -u testuser # 10 parallel forks
ansible db -m ansible.builtin.copy -a "src=/etc/hosts dest=/tmp/hosts"
ansible webservers -m ansible.builtin.file -a "dest=/opt/data/b.txt mode=600 owner=user1 group=user1"
ansible webservers -m ansible.builtin.file -a "dest=/opt/data mode=755 owner=user1 group=user1 state=directory"
Show all facts:
ansible all -m ansible.builtin.setup