1. ping module
The ping module is used to check whether the control node and the controlled node are unblocked. pong means on
[root@localhost .ssh]# ansible all -m ping 192.168.65.128 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
2. command, raw, shell module
Command, raw and shell are commands executed on the remote host. They all support the execution of scripts on the controlled host on the controlled host. However, command is the default module of the system and cannot use pipeline symbol and redirection. Raw and shell support pipelining and redirection. In daily life, try to use as few raw and shell modules as possible, and use the default command module. None of them is idempotent
Idempotency: (if you execute one or more times, the result is the same. If you have done this step, go to the next step directly)
2.1 command module
//Create a file named wjm1 in the / home / directory
[root@localhost ansible]# ansible all -a"touch /home/wjm1" 192.168.65.128 | CHANGED | rc=0 >> [root@wjm ~]# ll /home/ | grep wjm1 -rw-r--r-- 1 root root 0 7 June 18-15:17 wjm1
//View all files in the / home / directory
[root@localhost ansible]# ansible all -a "ls /home/" 192.168.65.128 | CHANGED | rc=0 >> 333 mjw mmm roo wjm wjm1
//command does not support pipeline characters and redirection
[root@localhost ansible]# ansible all -a 'ps -ef|grep vsftpd' 192.168.65.128 | FAILED | rc=1 >> error: unsupported SysV option Usage: ps [options] Try 'ps --help <simple|list|output|threads|misc|all>' or 'ps --help <s|l|o|t|m|a>' for additional help text. For more details see ps(1).non-zero return code
2.2 raw module
Execute script file
[root@localhost ansible]# ansible all -m raw -a"/home/www.sh" 192.168.65.128 | CHANGED | rc=0 >> hello world Shared connection to 192.168.65.128 closed.
Supports pipeline characters and redirection
[root@localhost ansible]# ansible all -m raw -a "ls /home | grep www.sh" 192.168.65.128 | CHANGED | rc=0 >> www.sh Shared connection to 192.168.65.128 closed. [root@localhost ansible]# ansible all -m raw -a"echo 'wjm' > /home/123" 192.168.65.128 | CHANGED | rc=0 >> Shared connection to 192.168.65.128 closed. [root@wjm home]# cat 123 wjm
2.3 shell module
The shell module is used to execute the script on the controlled machine on the controlled machine, or directly execute the command on the controlled machine. The shell module also supports pipeline and redirection.
Execute the script file on the controlled host
[root@localhost ansible]# ansible all -m shell -a"/home/www.sh" 192.168.65.128 | CHANGED | rc=0 >> hello world
Supports pipeline characters and redirection
[root@localhost ansible]# ansible all -m shell -a"echo 'wjm123' > /home/456" 192.168.65.128 | CHANGED | rc=0 >>
3. script module
The script module is used to execute the script on the master on the controlled machine
[root@localhost ansible]# vim script.sh [root@localhost ansible]# chmod +x script.sh [root@localhost ansible]# ll Total consumption 32 -rw-r--r-- 1 root root 20025 7 October 17:58 ansible.cfg -rw-r--r-- 1 root root 1016 6 August 23:12 hosts -rw-r--r-- 1 root root 18 7 June 18-14:41 inventory drwxr-xr-x 2 root root 6 7 November 16:32 mulu drwxr-xr-x 2 root root 6 6 August 23:12 roles -rwxr-xr-x 1 root root 19 7 June 18-16:13 script.sh [root@localhost ansible]# ansible all -m script -a"/etc/ansible/script.sh" / / execute the script of the control node on the controlled host 192.168.65.128 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.65.128 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.65.128 closed." ], "stdout": "file system Capacity used available used% Mount point\r\n/dev/mapper/centos-root 50G 4.6G 45G 10% /\r\ndevtmpfs 2.0G 0 2.0G 0% /dev\r\ntmpfs 2.0G 0 2.0G 0% /dev/shm\r\ntmpfs 2.0G 13M 2.0G 1% /run\r\ntmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup\r\n/dev/sda1 1014M 170M 845M 17% /boot\r\n/dev/mapper/centos-home 24G 35M 24G 1% /home\r\ntmpfs 407M 12K 407M 1% /run/user/42\r\ntmpfs 407M 0 407M 0% /run/user/0\r\n", "stdout_lines": [ "file system Capacity used available used% Mount point", "/dev/mapper/centos-root 50G 4.6G 45G 10% /", "devtmpfs 2.0G 0 2.0G 0% /dev", "tmpfs 2.0G 0 2.0G 0% /dev/shm", "tmpfs 2.0G 13M 2.0G 1% /run", "tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup", "/dev/sda1 1014M 170M 845M 17% /boot", "/dev/mapper/centos-home 24G 35M 24G 1% /home", "tmpfs 407M 12K 407M 1% /run/user/42", "tmpfs 407M 0 407M 0% /run/user/0" ] } You are /var/spool/mail/root New messages in
4. template module
The template module is used to generate a template and transfer it to a remote host.
Generate a template from the file of the control node and transfer it to the controlled node
[root@localhost ansible]# ansible all -m template -a"src=/etc/ansible/inventory dest=/home/inventory" 192.168.65.128 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "1230368e8fcfc3e8b0d533b776f0022b43ed08f1", "dest": "/home/inventory", "gid": 0, "group": "root", "md5sum": "e8a6a86641a7059d0edf37bf8370a7d1", "mode": "0644", "owner": "root", "size": 18, "src": "/root/.ansible/tmp/ansible-tmp-1626596417.78-124639-223938999577496/source", "state": "file", "uid": 0 } [root@wjm home]# ls /home/ | grep inventory inventory
5. yum module
The yum module is used to use the yum management software on the specified node machine. It supports two main parameters
name: Package name to manage state: What to do latest: Install software //Install the latest version of software installed: Install software //Fait accompli, so that it has become an installed state present: Install software //Direct installation removed: Uninstall software //remove absent: Uninstall software //Absent, both mean uninstall
Install FTP (Text Transfer Protocol) service
[root@localhost ansible]# ansible all -m yum -a"name=vsftpd state=present" 192.168.65.128 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "vsftpd" ] }, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.cqu.edu.cn\n * extras: mirrors.bfsu.edu.cn\n * updates: mirrors.cqu.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package vsftpd.x86_64 0:3.0.2-29.el7_9 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n vsftpd x86_64 3.0.2-29.el7_9 updates 173 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 173 k\nInstalled size: 353 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : vsftpd-3.0.2-29.el7_9.x86_64 1/1 \n Verifying : vsftpd-3.0.2-29.el7_9.x86_64 1/1 \n\nInstalled:\n vsftpd.x86_64 0:3.0.2-29.el7_9 \n\nComplete!\n" ] }
6. Add, delete and modify user and group modules
Add user:
[root@localhost ansible]# ansible localhost -m user -a"name=mok state=present" / / if you add create after it_ Home = no means no home directory is created localhost | CHANGED => { "changed": true, "comment": "", "create_home": true, "group": 1002, "home": "/home/mok", "name": "mok", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1002 }
Delete user:
[root@localhost ansible]# ansible localhost -m user -a"name=mok state=absent" / / if you want to delete the home directory when deleting the directory, you can use remove=yes to delete the user's home directory localhost | CHANGED => { "changed": true, "force": false, "name": "mok", "remove": false, "state": "absent" } You are /var/spool/mail/root New messages in [root@localhost ansible]# id mok id: mok: no such user //There is no such user
Modify user:
[root@localhost ansible]# ansible localhost -m user -a"name=mok uid=1003" / / modify the user's UID localhost | CHANGED => { "append": false, "changed": true, "comment": "", "group": 1002, "home": "/home/mok", "move_home": false, "name": "mok", "shell": "/bin/bash", "state": "present", "uid": 1003 } [root@localhost ansible]# id mok uid=1003(mok) gid=1002(mok) group=1002(mok)
Add group:
[root@localhost ansible]# ansible localhost -m group -a"name=M state=present" localhost | CHANGED => { "changed": true, "gid": 1006, "name": "M", "state": "present", "system": false }
Modify group gid
[root@localhost ansible]# ansible localhost -m group -a"name=M gid=1007" localhost | CHANGED => { "changed": true, "gid": 1007, "name": "M", "state": "present", "system": false }
delete group
[root@localhost ansible]# ansible localhost -m group -a"name=M state=absent" localhost | CHANGED => { "changed": true, "name": "M", "state": "absent" }
7. copy and touch, mkdir modules
4.2 copy, touch and mkdir modules
copy:
[root@localhost ansible]# Ansible localhost - M copy - a "SRC = / etc / ansible / WJM dest = / home" / / SRC: source address dest: destination address localhost | CHANGED => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/home/WJM", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1626406101.86-64577-213203522178257/source", "state": "file", "uid": 0 } [root@localhost ansible]# cd /home/ | ls ansible.cfg hosts inventory roles WJM
touch:
[root@localhost ansible]# Ansible localhost - M command - a "touch WJM" / / command: command module [WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. localhost | CHANGED | rc=0 >> [root@localhost ansible]# ll Total consumption 24 -rw-r--r-- 1 root root 20027 7 September 16:35 ansible.cfg -rw-r--r-- 1 root root 1016 6 August 23:12 hosts -rw-r--r-- 1 root root 0 7 September 16:35 inventory drwxr-xr-x 2 root root 6 6 August 23:12 roles -rw-r--r-- 1 root root 0 7 November 16:15 WJM
mkdir:
[root@localhost ansible]# ansible localhost -m command -a"mkdir mulu" [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. localhost | CHANGED | rc=0 >> [root@localhost ansible]# ls ansible.cfg hosts inventory mulu roles WJM
8. file module
The file module is used to set file permissions and other attributes
To change the permissions of a file, use mode
[root@localhost ansible]# Ansible all - m file - a "path = / home / WJM mode = 666" / / the location of the path file and the permissions of the mode file 192.168.65.128 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1001, "group": "wjm", "mode": "0666", "owner": "wjm", "path": "/home/wjm", "size": 144, "state": "directory", "uid": 1001 } [root@wjm home]# ll | grep wjm Total consumption 1 drw-rw-rw-. 5 wjm wjm 144 7 June 18-15:15 wjm
To modify the owner of a file, use owner
[root@localhost ansible]# ansible all -m file -a"path=/home/wjm owner=root" 192.168.65.128 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1001, "group": "wjm", "mode": "0666", "owner": "root", "path": "/home/wjm", "size": 144, "state": "directory", "uid": 0 } [root@wjm home]# ll | grep wjm Total consumption 1 drw-rw-rw-. 5 root wjm 144 7 June 18-15:15 wjm
To modify the group of a file, you can use group
[root@localhost ansible]# ansible all -m file -a"path=/home/wjm group=root" 192.168.65.128 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0666", "owner": "root", "path": "/home/wjm", "size": 144, "state": "directory", "uid": 0 } [root@wjm home]# ll | grep wjm Total consumption 1 drw-rw-rw-. 5 root root 144 7 June 18-15:15 wjm
9. blockinfile module
The blockinfile module can help us insert "a text" into the specified file. This text is marked. In other words, we mark this text so that we can find this text through the "mark" in future operations, and then modify or delete it. This description is not particularly easy to understand, Combined with the following small examples, you can understand it immediately.
Add two lines after the test file on the controlled host
[root@localhost ansible]# ansible all -m blockinfile -a"path=/home/test block='10 11 12 13\n14 15 16 17 '" / /' \ n ': line feed 192.168.65.128 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "msg": "Block inserted" } [root@wjm home]# cat test 123 456 789 # BEGIN ANSIBLE MANAGED BLOCK / / start tag 10 11 12 13 14 15 16 17 # END ANSIBLE MANAGED BLOCK / / end tag