Common Ansible Modules for Operations Automation Learning

Shell Module: Execute Any Command

The shell module allows you to execute shell commands on remote hosts. For example, to view the list of files in the current directory on a remote host, you can use:

ansible <host_server> -m shell -a "ls -l"

<host_server> is the host or host group you want to operate on, which can be a single hostname, IP address, or a host group name defined in the Ansible configuration file. This command will execute the ls -l command on the specified host and return the file list information.

Copy Module: File Copying

The copy module is used to copy files from the control node to the remote node. Suppose we have a local file /path/to/local/file.txt that we want to copy to the /destination/path/ directory on the remote host, we can use the following command:

ansible <host_server> -m copy -a "src=/path/to/local/file.txt dest=/destination/path/"

If you need to modify the permissions of the copied file, you can also add the mode parameter:

ansible <host_server> -m copy -a "src=/path/to/local/file.txt dest=/destination/path/ mode=0644"

Script Module: Run Local Scripts

The script module can run local script files on remote hosts. For example, if we have a local bash script /path/to/local/script.sh that we want to execute on the remote host:

ansible <host_server> -m script -a "/path/to/local/script.sh"

Ansible will transfer the script to the remote host and execute it, returning the execution result to the control node.

Ping Module: Check Host Reachability

The ping module is not a traditional ICMP ping, but is used to check if Ansible can establish a connection with the remote host. It is very simple to use:

ansible <host_server> -m ping

If the remote host is reachable and Ansible is configured correctly, it will return a result similar to pong, indicating a successful connection.

File Module: Manage Files and Directories

The file module can create, delete, or modify the properties of files or directories. For example, to create a directory /new/directory on the remote host and set the permissions to 0755:

ansible <host_server> -m file -a "path=/new/directory state=directory mode=0755"

To delete a file /path/to/file:

ansible <host_server> -m file -a "path=/path/to/file state=absent"

Fetch Module: Retrieve Files from Remote Hosts

The fetch module is the opposite of the copy module; it is used to download files from remote hosts to the control node. Suppose we want to download a file from the remote host’s /remote/path/file.txt to the local /local/destination/ directory:

ansible <host_server> -m fetch -a "src=/remote/path/file.txt dest=/local/destination/"

The downloaded file will be recreated locally according to the directory structure of the remote host to avoid filename conflicts.

Hostname Module: Set Hostname

The hostname module allows you to easily set the hostname of a remote host. For example, to set the hostname of the remote host to new_hostname:

ansible <host_server> -m hostname -a "name=new_hostname"

After setting, the hostname of the remote host will be updated accordingly.

Cron Module: Manage Scheduled Tasks

The cron module can be used to add, delete, or modify scheduled tasks on remote hosts. For example, to add a backup task that runs every day at 2 AM on the remote host:

ansible <host_server> -m cron -a "name='daily backup' minute=0 hour=2 job='/path/to/backup/script.sh'"

If you want to delete a scheduled task, simply specify the task’s name and set the state to absent:

ansible <host_server> -m cron -a "name='daily backup' state=absent"

User Module: Manage Users

The user module is used to create, delete, or modify users. To create a new user new_user and set its home directory and specified shell:

ansible <host_server> -m user -a "name=new_user home=/home/new_user shell=/bin/bash"

To delete a user:

ansible <host_server> -m user -a "name=new_user state=absent"

Service Module: Manage Services

The service module can control the start, stop, restart, and status check of services on remote hosts. For example, to start the httpd service on the remote host:

ansible <host_server> -m service -a "name=httpd state=started"

To check the status of the httpd service:

ansible <host_server> -m service -a "name=httpd state=status"

Group Module: Manage User Groups

The group module is used to create, delete, or modify user groups. To create a new user group new_group:

ansible <host_server> -m group -a "name=new_group"

To delete a user group:

ansible <host_server> -m group -a "name=new_group state=absent"

Common Ansible Modules for Operations Automation Learning

Leave a Comment