Commonly Used Ansible Modules

Command module: Executes commands on remote hosts, default module, can ignore the -m option

Example: ansible webservers -m command -a ‘systemctl start httpd ‘

The command module does not support $ variables, <, >, |, ;, &, etc., while the shell module does.

chdir: Change to the directory of the managed host

creates: If a directory exists, the command will not run

Example: ansible webservers -a ‘chdir=/data/ ls’

Shell module: Similar to Command

Example: ansible all -m shell -a “sed -i ‘s/SELINUX=.*/SELINUX=disabled’ /etc/selinux/config”

Can set shell as the default module:

Modify the configuration file to make shell the default module: vim /etc/ansible/ansible.cfg module_name = shell

Script: Runs a script from the Ansible server on the remote host

Example: ansible websrvs -m script -a /data/test.sh

Copy: Copies files from the control node to the remote host

src: Source file

dest: Specify target path

mode: Set permissions

backup: Backup the source file

content: Instead of src, specify the content of the local file to generate the target host file

Example: ansible webservers -m copy -a “src=/root/test1.sh dest=/tmp/test2.sh owner=wang mode=600 backup=yes” If the target exists, it will overwrite by default, first backing up

Example: ansible webservers -m copy -a “content=’test content \ntest content1′ dest=/tmp/test.txt” Specify content to directly generate the target file

The difference between copy and template: copy only copies files, while template can be used to copy configuration files. The src filename format should be .j2. When variables are defined in the file, after copying to the remote host, the configuration file will recognize and replace the corresponding variables with their respective values.

Fetch: Copies files from the remote host to the Ansible host, opposite of copy, does not support copying directories

Example: ansible webservers -m fetch -a ‘src=/root/test.sh dest=/data/scripts’

File: Sets file attributes

path: The path of the file to be managed

recurse: Recursive, folders require recursion

src: Create hard links, for soft links, specify the source target, with state=link or state=hard to set soft or hard links

state: Status. absent deletes; touch creates a file; directory creates a folder

Example: ansible webservers -m file -a ‘path=/app/test.txt state=touch’

Example: ansible webservers -m file -a ‘path=/data/testdir state=directory’

Example: ansible webservers -m file -a ‘src=/data/testfile dest=/data/testfile-link state=link’

Example: ansible webservers -m file -a ‘path=/root/test.sh owner=wang mode=777’

unarchive: Unpack and decompress, there are two methods:

1. Transfer the compressed package from the Ansible host to the remote host and decompress it to a specific directory, set copy=yes.

2. Decompress a compressed package on the remote host to a specified path, set copy=no.

Common parameters:

copy: Defaults to yes. When copy=yes, the copied file is from the Ansible host to the remote host. If set to copy=no, it will look for the src source file on the remote host.

src: Source path, can be a path on the Ansible host or a path on the remote host. If it is a path on the remote host, set copy=no.

dest: Target path on the remote host

mode: Set permissions for the decompressed files

Example: ansible webservers -m unarchive -a ‘src=test.tgz dest=/opt/test’ Defaults copy is yes, decompresses the local test.tgz to the target host /opt/test directory

Example: ansible webservers -m unarchive -a ‘src=/tmp/test.tgz dest=/data copy=no mode=0777’ Decompresses the managed host’s test.tgz to /data directory and sets permissions to 777

Example: ansible webservers -m unarchive -a ‘src=https://example.com/example.zip dest=/data copy=no’ Can also use remote links

Archive: Package and compress

Example: ansible webservers -m archive -a ‘path=/etc/sysconfig dest=/data/sysconfig.tar.bz2 format=bz2 owner=wang mode=0777’ Packages the directory from the remote host

path: Specify path

dest: Specify target file

format: Specify packaging format

owner: Specify owner

mode: Set permissions

Hostname: Manage hostnames

Example: ansible webservers -m hostname -a ‘name=test.com’ Change the hostname of a group of hosts

Example: ansible 192.168.1.2 -m hostname -a ‘name=app2.com’ Change the hostname of a single host, equivalent to hostnamectl set-hostname app2.com

Cron: Scheduled tasks

Supported times: minute, hour, day, month, weekday

Example: ansible webservers -m cron -a “minute=*/5 job=’/shell/test.sh & >/dev/null’ name=testcron” Create a task

Example: ansible webservers -m cron -a ‘state=absent name=testcron’ Delete a task

Example: ansible webservers -m cron -a “minute=*/10 job=’/shell/test.sh &>/dev/null’ name=testcron disabled=yes” Comment out a task

Yum: Manage packages

Example: ansible webservers -m yum -a ‘list=httpd’ View program list, equivalent to yum list httpd

Example: ansible webservers -m yum -a ‘name=httpd state=present’ Install httpd, default is present

Example: ansible webservers -m yum -a ‘name=httpd state=absent’ Uninstall, delete

Service: Manage services

Example: ansible webservers -m service -a ‘name=httpd state=stopped’

Example: ansible webservers -m service -a ‘name=httpd state=started enabled=yes’

Example: ansible webservers -m service -a ‘name=httpd state=reloaded’

Example: ansible webservers -m service -a ‘name=httpd state=restarted’

User: Manage users

home: Specify home directory path

system: Specify system account

group: Specify group

remove: Clear user (along with home directory), deleting a user can also use shell module userdel -r username

shell: Specify shell type

Example: ansible webservers -m user -a ‘name=user1 comment=”test user” uid=2048 home=/app/user1 group=root’

Example: ansible wrsvs -m user -a ‘name=sysuser system=yes home=/app/sysuser’

Example: ansible wsvs -m user -a ‘name=user1 state=absent remove=yes’ Clear all user data

Example: ansible wsrv -m user -a ‘name=app uid=88 system=yes home=/app group=root shell=/sbin/nologin password=”$1$6aJwQhVY$ll2F45/o.zkpnRWQTIsaJ.”‘

Example: ansible wesr -m user -a ‘name=app state=absent’ Only delete user without deleting home directory

yum install expect

mkpasswd # Generate password

openssl passwd -l # Generate encrypted password

Group: Manage groups

Example: ansible webservers -m group -a ‘name=testgroup system=yes gid=99’

Example: ansible webservers -m group -a ‘name=testgroup state=absent’

lineinfile and replace modules, equivalent to sed functionality

Example: ansible all -m lineinfile -a “path=/etc/selinux/config regexp=”^SELINUX=” line=”SELINUX=enforcing””

Example: ansible all -m lineinfile -a “dest=/etc/fstab state=absent regexp=”^#”” Delete lines starting with # (remove comments)

Example: ansible all -m replace -a “path=/etc/fstab regexp=”^(UUID.*)” replace=”#\1″” Indicates to add # in front of all lines starting with UUID, \1 represents UUID.*

Example: ansible all -m replace -a “path=/etc/fstab regexp=”^#(.*)” replace=”\1″” Indicates to uncomment

setup: View some variables and values in the host, such as memory, CPU, disk, IP address, etc. If there are many hosts, it may affect execution speed, you can use gather_facts: no to prevent Ansible from collecting facts information

Example: ansible 192.168.1.2 -m setup -a “filter=ansible_processor_vcpus”

For example, CPU: “ansible_processor_vcpus”: 2

Leave a Comment