Ansible commands - dishplate/blog GitHub Wiki

Ansible ad-hoc commands

Using adhoc commands with a host/inventory file

Edit the hosts file, add your servers at the bottom [if you use your own hosts file you must specify this with -i path/name fo file. eg ansible -i /ansible/ansiblehosts --list-hosts all] #sudo nano /etc/ansible/hosts Eg. 10.1.1.3

Test it, ping all your hosts[nodes] #ansible all -m ping

Get hostnames $ansible all -a hostname Get date/time $ansible all -a date Get free space [you have to use quotes] $ansible all -a “df -h”

Basic syntax structure $ ansible -m -a

Running a command --ask-become-pass Eg. ansible nas -m apt -a 'upgrade=yes update_cache=yes' -b

Rebooting hosts $ ansible -a “/sbin/reboot”

Shutting down a system Ansible -a "/sbin/shutdown -r now"

Checking host’s system information Ansible collects the system’s information for all the hosts connected to it. To display the information of hosts, run

$ ansible -m setup | less

Secondly, to check a particular info from the collected information by passing an argument,

$ ansible -m setup -a “filter=ansible_distribution”

⚠️ **GitHub.com Fallback** ⚠️