LLI Wiki — My Digital Knowledge Base

Ansible ad hoc commands: ping, reboot and include_role

Ad hoc Ansible command examples to ping, reboot, run shell commands and run a single role or task file.

A few ad hoc Ansible command examples: connectivity tests, reboot, shell commands, and running a single role or task file. See the official documentation.

Ping

ansible -i env/prod/inventory.yml -m ping server01.example.com

Reboot servers

ansible -i env/prod/inventory.yml -a "/sbin/reboot" -f 10

-f 10 limits parallelism to 10 hosts at a time.

Run a shell command

ansible -i env/prod/inventory.yml -m ansible.builtin.shell -a 'echo $TERM' server01.example.com

Run a specific role or task file

The include_role module runs a role on its own, or a single task file of the role (tasks_from), without a playbook.

ansible server01.example.com -m include_role -a "name=windows/applications/monitoring" -i env/

ansible server01.example.com -m include_role -a "name=windows/applications/monitoring tasks_from=deploy_config.yml" -i env/

ansible server01.example.com -m include_role -a "name=linux/hardening/passwordhistory" -i env/prod/ --become

ansible server02.example.com -m include_role -a "name=linux/hardening/rsyslog tasks_from=file_permission.yml" -i env/dev/ --become

--become runs the command with privilege escalation.

Further reading

How to run only one task in an Ansible playbook (Stack Overflow)

Mastodon LLIMastodon SocialFosstodon