Files
my-docs/Ansible/Document.md
2024-04-24 15:10:47 +03:30

1.5 KiB

Ansible Module Usage

Ansible modules are standalone scripts that can be used within Ansible to perform various tasks on managed nodes. Here are some examples of using Ansible modules:

Basic Module Execution

To execute a module against all hosts in your inventory file:

ansible -m <module> all -i <inventory_file>

Example:

ansible -m ping all -i server.ini

Module Execution with Arguments

You can pass arguments to modules using the -a flag:

ansible -m <module> -a <arguments> -i <inventory_file> <group_of_servers>

Examples:

ansible -m command -a "uptime" -i server.ini all
ansible -m command -a "uname -a" -i server.ini all

Running Commands as sudo

If the command requires root privileges, you can use the --become or -b flag:

ansible -m <module> -a "<command>" --become -i <inventory_file> <group_of_servers>

Example:

ansible -m command -a "reboot" --become -i server.ini all

More Examples

Here are some additional examples demonstrating Ansible module usage:

  • Gathering facts from all hosts:
ansible -m setup all -i server.ini
  • Copying a file to all hosts:
ansible -m copy -a "src=/path/to/src/file dest=/path/to/destination/" -i server.ini all
  • Installing a package using apt module:
ansible -m apt -a "name=<package_name> state=present" -i server.ini all
  • Restarting a service:
ansible -m service -a "name=<service_name> state=restarted" -i server.ini all