Ansible Command Modules
1. ansible - Basic Command Syntax
The basic structure for ad hoc commands is:
bash
Copy
ansible [pattern] -m [module] -a "[module options]"
Where:
[pattern] specifies the group or host to target (e.g., webservers, localhost).
[module] is the name of the Ansible module (e.g., command, file, yum).
[module options] provides the arguments for the module (e.g., file path, user name, etc.).
2. command Module - Run Commands on Remote Systems
The command module allows you to run commands on the target systems.
Example: Rebooting servers:
bash
Copy
$ ansible webservers -m command -a "reboot"
Explanation: This command uses the command module to reboot all servers in the webservers
group. It runs the reboot command on each machine.
3. shell Module - Run Shell Commands
The shell module is used for executing shell commands. It is useful when the command involves
special shell syntax like pipes (|), redirection (>), etc.
Example:
bash
Copy
$ ansible webservers -m shell -a "echo $TERM"
Explanation: This example prints the terminal type ($TERM) on all web servers in the webservers
group. Since $TERM is a shell variable, you must use the shell module.
4. copy Module - Copy Files to Remote Hosts
The copy module is used to copy files from the local machine to remote machines.
Example:
bash
Copy
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/11
$ ansible webservers -m copy -a "src=/tmp/myfile dest=/etc/myfile"
Explanation: This command copies the file /tmp/myfile from the local machine to /etc/myfile on all
the servers in the webservers group.
5. file Module - Manage File Properties
The file module allows you to manage file properties such as permissions, ownership, and file state
(e.g., directory, file, absent).
Examples:
Change file permissions:
bash
Copy
$ ansible webservers -m file -a "dest=/etc/myfile mode=644"
Explanation: This command sets the file /etc/myfile to have 644 permissions on all web servers.
Create a directory:
bash
Copy
$ ansible webservers -m file -a "dest=/tmp/mydir state=directory"
Explanation: This command creates the directory /tmp/mydir on all web servers if it doesn't already
exist.
6. yum Module - Manage Packages on RedHat-based Systems
The yum module allows you to manage packages on RedHat-based systems like CentOS, RHEL, etc.
Examples:
Install a package:
bash
Copy
$ ansible webservers -m yum -a "name=httpd state=present"
Explanation: This command installs the httpd package on all web servers.
Remove a package:
bash
Copy
$ ansible webservers -m yum -a "name=httpd state=absent"
Explanation: This removes the httpd package from all web servers.
7. user Module - Manage Users
The user module is used to create, modify, or delete user accounts.
Examples:
Create a user:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/11
bash
Copy
$ ansible webservers -m user -a "name=alice state=present"
Explanation: This command creates the user alice on all web servers.
Delete a user:
bash
Copy
$ ansible webservers -m user -a "name=alice state=absent"
Explanation: This command removes the user alice from all web servers.
8. group Module - Manage Groups
The group module allows you to manage groups (add or remove users from groups).
Examples:
Create a group:
bash
Copy
$ ansible webservers -m group -a "name=admins state=present"
Explanation: This command creates the group admins on all web servers.
Add a user to a group:
bash
Copy
$ ansible webservers -m user -a "name=alice groups=admins"
Explanation: This command adds the user alice to the admins group.
9. service Module - Manage Services
The service module allows you to manage system services (start, stop, restart, etc.).
Examples:
Start a service:
bash
Copy
$ ansible webservers -m service -a "name=httpd state=started"
Explanation: This command starts the httpd service on all web servers.
Stop a service:
bash
Copy
$ ansible webservers -m service -a "name=httpd state=stopped"
Explanation: This command stops the httpd service on all web servers.
10. setup Module - Gather System Facts
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/11
The setup module is used to gather facts about the system, which can be useful for conditional logic
or troubleshooting.
Example:
bash
Copy
$ ansible all -m setup
Explanation: This command gathers and displays all system facts (e.g., OS, IP addresses, hardware
details) for all machines.
11. cron Module - Manage Cron Jobs
The cron module is used to manage cron jobs for scheduling tasks on remote systems.
Example:
bash
Copy
$ ansible webservers -m cron -a "name='backup' minute='0' hour='2' job='/usr/bin/backup.sh'"
Explanation: This command adds a cron job to execute /usr/bin/backup.sh every day at 2 AM on all
web servers.
12. get_url Module - Download Files from a URL
The get_url module is used to download files from an HTTP, HTTPS, or FTP server.
Example:
bash
Copy
$ ansible webservers -m get_url -a "url=http://example.com/file.tar.gz dest=/tmp/file.tar.gz"
Explanation: This command downloads the file from the specified URL and saves it to /tmp/file.tar.gz
on all web servers.
13. git Module - Manage Git Repositories
The git module is used to manage Git repositories on remote systems.
Example:
bash
Copy
$ ansible webservers -m git -a "repo=https://github.com/example/repo.git dest=/opt/repo"
Explanation: This command clones the Git repository to the /opt/repo directory on all web servers.
14. wait_for Module - Wait for a Condition
The wait_for module is used to wait for a specific condition (e.g., a port to be open or a file to
appear) before proceeding.
Example:
bash
Copy
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/11
$ ansible webservers -m wait_for -a "host=localhost port=80 state=started"
Explanation: This command waits for port 80 to be open on the localhost before continuing.
15. lineinfile Module - Manage Lines in Files
The lineinfile module allows you to add, modify, or remove lines in a file.
Example:
bash
Copy
$ ansible webservers -m lineinfile -a "path=/etc/hosts line='192.168.1.10 myserver'"
Explanation: This command adds the line 192.168.1.10 myserver to the /etc/hosts file on all web
servers.
This is a great summary of some essential Ansible commands and modules. Here is a breakdown of the
important commands and their use cases:
1. Basic Command Syntax:
Syntax: `ansible [pattern] -m [module] -a "[module options]"`
Example: `ansible webservers -m command -a "reboot"`
2. Command Module:
Runs commands on remote systems.
Example: Reboot servers: `ansible webservers -m command -a "reboot"`
3. Shell Module:
Executes shell commands with special syntax like pipes or redirection.
Example: `ansible webservers -m shell -a "echo $TERM"`
4. Copy Module:
Copies files from local to remote hosts.
Example: `ansible webservers -m copy -a "src=/tmp/myfile dest=/etc/myfile"`
5. File Module:
Manages file properties such as permissions and state.
Example: Set file permissions: `ansible webservers -m file -a "dest=/etc/myfile mode=644"`
6. Yum Module:
Manages packages on RedHat-based systems.
Example: Install a package: `ansible webservers -m yum -a "name=httpd state=present"`
7. User Module:
Manages user accounts.
Example: Create a user: `ansible webservers -m user -a "name=alice state=present"`
8. Group Module:
Manages groups and user memberships.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/11
Example: Create a group: `ansible webservers -m group -a "name=admins state=present"`
9. Service Module:
Manages services (start, stop, restart).
Example: Start a service: `ansible webservers -m service -a "name=httpd state=started"`
10. Setup Module:
Gathers facts about the system.
Example: `ansible all -m setup`
11. Cron Module:
Manages cron jobs.
Example: Add a cron job: `ansible webservers -m cron -a "name='backup' minute='0' hour='2'
job='/usr/bin/backup.sh'"`
12. Get_url Module:
Downloads files from a URL.
Example: `ansible webservers -m get_url -a "url=http://example.com/file.tar.gz
dest=/tmp/file.tar.gz"`
13. Git Module:
Manages Git repositories.
Example: Clone a repository: `ansible webservers -m git -a
"repo=https://github.com/example/repo.git dest=/opt/repo"`
14. Wait_for Module:
Waits for a specific condition before proceeding.
Example: `ansible webservers -m wait_for -a "host=localhost port=80 state=started"`
15. Lineinfile Module:
Manages lines in files.
Example: Add a line: `ansible webservers -m lineinfile -a "path=/etc/hosts
line='192.168.1.10 myserver'"`
This covers some of the fundamental modules and commands in Ansible. Let me know if you need more
examples or clarifications!
explain in better way
Certainly! Let's break down each part in a clearer, more understandable way, while keeping technical
accuracy. I'll explain each module and its function in simple terms:
1. Basic Command Syntax
The basic syntax for running an Ansible ad-hoc command is:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/11
bash
ansible [target] -m [module] -a "[module options]"
`[target]`: This specifies the group of hosts or a single host to target. For example, `webservers` (a
group) or `localhost` (a single machine).
`[module]`: This is the Ansible module you want to use. A module is simply a tool that Ansible uses
to perform tasks (e.g., copy files, manage services).
`[module options]`: These are the arguments or options you pass to the module. For example, if
you're copying a file, you provide the source (`src`) and destination (`dest`) paths.
2. Command Module
The command module allows you to run commands directly on the remote hosts.
Example: Rebooting servers
bash
ansible webservers -m command -a "reboot"
This command tells Ansible to use the `command` module to run the `reboot` command on all servers in
the `webservers` group.
3. Shell Module
The shell module is similar to the `command` module, but it allows you to run shell commands that may
use things like pipes (`|`) or redirection (`>`), which the `command` module cannot handle.
Example: Checking terminal type
bash
ansible webservers -m shell -a "echo $TERM"
This will print the terminal type (like `xterm`) on all `webservers`. The `$TERM` variable is a shell-specific
variable, so we use the `shell` module.
4. Copy Module
The copy module allows you to copy files from your local machine to remote systems.
Example: Copying a file
bash
ansible webservers -m copy -a "src=/tmp/myfile dest=/etc/myfile"
This copies the file `/tmp/myfile` from the local machine to `/etc/myfile` on all web servers.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/11
5. File Module
The file module allows you to manage file properties such as permissions, ownership, and state
(whether it's a file or a directory).
Examples:
Change file permissions:
bash
ansible webservers -m file -a "dest=/etc/myfile mode=644"
This command sets the file `/etc/myfile` to have `644` permissions (read and write for owner,
read-only for others).
Create a directory:
bash
ansible webservers -m file -a "dest=/tmp/mydir state=directory"
This creates the directory `/tmp/mydir` on all `webservers` if it doesn't already exist.
6. Yum Module
The yum module is used to manage packages on RedHat-based systems (like CentOS, RHEL).
Examples:
Install a package:
bash
ansible webservers -m yum -a "name=httpd state=present"
This installs the `httpd` package (Apache web server) on all web servers.
Remove a package:
bash
ansible webservers -m yum -a "name=httpd state=absent"
This removes the `httpd` package from all web servers.
7. User Module
The user module is used to create, modify, or delete user accounts on remote systems.
Examples:
Create a user:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/11
bash
ansible webservers -m user -a "name=alice state=present"
This command creates a user named `alice` on all web servers.
Delete a user:
bash
ansible webservers -m user -a "name=alice state=absent"
This removes the user `alice` from all web servers.
8. Group Module
The group module allows you to manage user groups (add/remove users to/from groups).
Examples:
Create a group:
bash
ansible webservers -m group -a "name=admins state=present"
This creates the `admins` group on all web servers.
Add a user to a group:
bash
ansible webservers -m user -a "name=alice groups=admins"
This adds the user `alice` to the `admins` group on all web servers.
9. Service Module
The service module allows you to manage system services (start, stop, restart, etc.).
Examples:
Start a service:
bash
ansible webservers -m service -a "name=httpd state=started"
This starts the `httpd` (Apache) service on all web servers.
Stop a service:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/11
bash
ansible webservers -m service -a "name=httpd state=stopped"
This stops the `httpd` service on all web servers.
10. Setup Module
The setup module is used to gather system facts (like OS version, hardware details) from remote
systems, which can be useful for troubleshooting or conditional logic.
Example: Gathering system facts
bash
ansible all -m setup
This command collects and displays system information for all machines.
11. Cron Module
The cron module manages cron jobs, which are used to schedule recurring tasks on remote systems.
Example: Scheduling a backup job
bash
ansible webservers -m cron -a "name='backup' minute='0' hour='2' job='/usr/bin/backup.sh'"
This schedules the `backup.sh` script to run daily at 2 AM on all web servers.
12. Get_url Module
The get_url module is used to download files from an HTTP, HTTPS, or FTP server.
Example: Downloading a file
bash
ansible webservers -m get_url -a "url=http://example.com/file.tar.gz dest=/tmp/file.tar.gz"
This downloads the file from the given URL and saves it to `/tmp/file.tar.gz` on all web servers.
13. Git Module
The git module is used to manage Git repositories on remote systems.
Example: Cloning a repository
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/11
bash
ansible webservers -m git -a "repo=https://github.com/example/repo.git dest=/opt/repo"
This clones the Git repository from the specified URL into the `/opt/repo` directory on all web servers.
14. Wait_for Module
The wait_for module is used to pause the execution of a task until a specific condition is met, like
waiting for a port to be open.
Example: Waiting for a port to open
bash
ansible webservers -m wait_for -a "host=localhost port=80 state=started"
This command will wait until port 80 on the `localhost` is open before continuing.
15. Lineinfile Module
The lineinfile module is used to add, modify, or remove lines in a file.
Example: Adding a line to `/etc/hosts`
bash
ansible webservers -m lineinfile -a "path=/etc/hosts line='192.168.1.10 myserver'"
This adds the line `192.168.1.10 myserver` to the `/etc/hosts` file on all web servers.
I hope this breakdown helps you better understand the different Ansible modules and how to use them!
Feel free to ask for more details or examples if needed.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/11