Ansible Commands

Ansible Commands Cheat Sheet
ansible all -m ping

Checks connectivity to all hosts defined in your inventory.

ansible <group_name> -m ping

Checks connectivity to hosts within a specific group in your inventory.

ansible <hostname> -m ping

Checks connectivity to a specific host.

ansible all -m command -a ‘uptime’

Executes the ‘uptime’ command on all managed hosts.

ansible <group_name> -m shell -a ‘df -h’

Executes the ‘df -h’ command on hosts in the specified group using the shell module.

ansible all -m copy -a ‘src=/path/on/local dest=/path/on/remote’

Copies a file from the Ansible control node to all managed hosts.

ansible all -m template -a ‘src=/path/on/local.j2 dest=/path/on/remote’

Deploys a Jinja2 template from the Ansible control node to all managed hosts.

ansible all -m service -a ‘name=httpd state=started’

Ensures the httpd service is started on all managed hosts.

ansible all -m package -a ‘name=nginx state=present’

Ensures the nginx package is installed on all managed hosts.

ansible all -m user -a ‘name=deployuser state=present groups=wheel’

Ensures a user named ‘deployuser’ exists and is in the ‘wheel’ group on all managed hosts.

ansible all -m file -a ‘path=/tmp/testdir state=directory’

Ensures a directory named ‘testdir’ exists in /tmp on all managed hosts.

ansible-playbook <playbook_name.yml>

Runs an Ansible playbook.

ansible-playbook -i <inventory_file> <playbook_name.yml>

Runs an Ansible playbook using a specific inventory file.

ansible-playbook <playbook_name.yml> –limit <group_name>

Runs an Ansible playbook, limiting execution to a specific group of hosts.

ansible-playbook <playbook_name.yml> -K

Runs an Ansible playbook and prompts for the sudo password.

ansible-vault encrypt <file_to_encrypt>

Encrypts a file using Ansible Vault.

ansible-vault decrypt <file_to_decrypt>

Decrypts a file using Ansible Vault.

ansible-vault view <file_to_view>

Views an encrypted file using Ansible Vault.

ansible-doc -l

Lists all available Ansible modules.

ansible-doc <module_name>

Displays documentation for a specific Ansible module.

ansible –version

Displays the installed Ansible version.