The stuff I do

Ansible tips

← Notes

Tags: [bash][ansible]

Run a command on several hosts 🔗

The documentation Introduction to Ad Hoc commands is a great reference.

  1. In the inventory file create the list of servers to target
[my_target_group_name]
host01.domain.ext
host02.domain.ext
host03.domain.ext
  1. Run the command
ansible [target_group_name] -i path/to/inventory/file -m shell -a [your_command]

# Example
ansible my_target_group_name -i inventories/inventory -m shell -a 'df -h'

The module shell allows to run any shell command but one can use any module

If you need to become root to run the command pass --become to the ansible command.

ParameterDescription
--becomeBecome root
-u <username>Change the username used
--ask-become-pass or -KPrompts you for the password to use for privilege escalation
-f 10Change how many machines are targeted simultaneously

Copying files between hosts 🔗

Refer to this link

← Notes