Command module:
The 1:command module executes the command on the remote node:
The command module is followed by the commands to be executed, and the arguments to the command are separated by spaces. The specified command executes on the selected node. The command is not executed through the shell, so environment variables such as $home and some operators (<,>,|,&) cannot be used. The shell module can be used.
1 chdir switch to the specified directory before running the command.
[Email protected] ansible]# ansible testhosts-m command-a "ls-l chdir=/tmp" 127.0.0.1 | Success | Rc=0 >>total 4drwx------2 root root 4096 Apr 5 21:27 pymp-ebjbzl192.168.1.112 | Success | Rc=0 >>total 4drwxr-xr-x 2 root root 4096 Apr 2 02:04 rsync/First switch to the/tmp directory before executing the command!
2 creates you can specify either a file name (directory name) or a regular pattern to match a series of file names. If the specified file exists, the specified command is not executed.
[Email protected] ansible]# ansible 192.168.1.112-m command-a "ls-l/tmp creates=/tmp/test" 192.168.1.112 | Success | Rc=0 >>total 4drwxr-xr-x 2 root root 4096 Apr 2 02:04 rsync/tmp/test does not exist, so execute command [[email protected] ansible]# ansible 192.168.1.112-m command-a "LS-L/tmp creates=/tmp/rsync" 192.168.1.112 | Success | Rc=0 >>skipped, Since/tmp/rsync exists/tmp/rsync file exists, so do not execute command
3 removes you can specify either a file name (or a directory name) or a regular pattern to match a series of filenames. If the specified file does not exist, the command is not run. (Pay attention to the contrast with creates)
[Email protected] ~]# ansible 192.168.1.112-m command-a "ls-l/tmp removes=/tmp/test" 192.168.1.112 | Success | Rc=0 >>skipped, Since/tmp/test does not exist[[email protected] ~]# ansible 192.168.1.112-m command-a "LS-L/tmp Removes=/tmp/rsync "192.168.1.112 | Success | Rc=0 >>total 4drwxr-xr-x 2 root root 4096 Apr 2 02:04 rsync[[email protected] ~]#/note vs. creates
The 2.script module runs local scripts on the remote machine.
The-a option of the script module is directly followed by the absolute path of a local script, and the parameters of the script are separated by spaces. The module first uploads the specified script to the remote node and executes the script in the shell environment of the remote node.
[Email protected] ~]# ansible 192.168.1.112-m script-a "/root/test.sh" 192.168.1.112 | Success >> {"Changed": True, "RC": 0, "stderr": "", "stdout": "ok\n"}/Note that this is a local script that executes remotely, so that when executed, the script file Must have executable permissions/
3.shell module, executing commands on remote nodes
The parameters of the Shell module are command names, and the parameters of the command itself are separated by a space. Commands are executed on the remote node as the command module does, but the shell module then executes the command through the shell Environment (/bin/bash), which can also execute a shell script, but the script must exist on the remote node.
The ChDir, creates, and removes parameters are the same as the command module's parameters.
[[email protected] ~]# ansible 192.168.1.112 -m shell -a " echo $HOME "192.168.1.112 | success | rc=0 >>/rootshell Execute script file [[email protected] ~]# ansible 192.168.1.112 -m command -a " echo $ HOME "192.168.1.112 | success | rc=0 >>/root[[email protected] ~]# ansible 192.168.1.112 -m shell -a "/bin/bash /root/test1.sh" 192.168.1.112 | FAILED | rc=127 >>/bin/bash: /root/test1.sh: No such file or directory[[email protected] ~]# vim test1.sh / Create a script file on the remote node [[email protected] ~]# cat test1.sh #!/bin/bashecho ok[[email Protected] ~]# [[email protected] ~]# ansible 192.168.1.112 -m shell -a "/bin/bash /root/test1.sh "192.168.1.112 | success | rc=0 >>ok/sehll Execute script file, the script file must exist on the remote node
Look at a simple playbook file:
[[email protected] ansible]# vim test.yml ---- hosts: 192.168.1.112 remote_user: root tasks: - name: lianxi module 1# file on the remote shell: /bin/bash /root/test1.sh The /playbook script is suffixed with. Yml! The following is the result of execution [[email protected] ~]# cd /etc/ansible[[email protected] ansible]# ansible-playbook test.yml play [192.168.1.112] ******************************************* gathering facts *************************************************************** ok: [192.168.1.112]task: [lianxi module 1] ************************************ changed: [192.168.1.112]play recap *********************************** 192.168.1.112 : ok=2 changed=1 unreachable=0 failed=0
4. FILE-related modules:
Copy the local file to the remote path
The copy module copies the local files to the remote path. The fetch module copies the remote files locally.
Options for copy:
Dest the required parameter to specify an absolute path on the remote node for the destination file. If SRC is a directory, then the parameter must also be a directory.
The absolute path of the SRC local file, or the relative path. If it is a path will be copied recursively, the path is the end of the word, only copy the contents of the directory, if not in a few words will copy the target
The contents of the recording itself and the inside. Like Rsync.
Backup optional parameter, create a backup file for the source file, add a timestamp information to the backup file. The value is: yes/no, default is No.
[[email protected] ~]# ansible 192.168.1.112 -m copy -a "Src=/root/test dest=/root/ backup=yes" 192.168.1.112 | success >> { "changed": true, " Checksum ": " 4e1243bd22c66e76c2ba9eddc1f91394e57f9f83 ", " dest ": "/root/ Test ", " GID ": 0, " group ": " root ", "md5sum": "d8e8fca2dc0f896fd7cb4cb0031ba249", "mode" : "0644", "owner": "root", "size": 5, "src": "/root/.ansible/tmp/ansible-tmp-1459869115.84-179543827108657/source ", " state ": " file ", " UID ": 0}
The
Content optional parameter, which is written directly to the destination file when the parameter is used instead of SRC.
[[email protected] ~]# ansible 192.168.1.112 -m copy -a "content= ' test test ' dest=/root/test ' 192.168.1.112 | success >> { "changed": true, "checksum": " ABEDC47A5EDE3FAB13390898C5160EC9AFBB6EC3 ", " dest ": "/root/test ", "GID": 0, "group": "root", "md5sum": "4f4acc5d8c71f5fbf04dace00b5360c8", "mode": "0644", "owner": "root", "size": 9, "src": "/root/.ansible/tmp/ansible-tmp-1459869735.21-149102614709149/source", "state": "file", "UID": 0} take a look at:[[email Protected] ~]# cat testtest test[[email protected] ~]#
Directory_mode optional parameter, when recursive replication, set permissions for the created directory, and if not specified, use the system's default permissions. This parameter affects only the newly created directory and does not affect the directory that already exists.
[Email protected] ~]# ansible webservers-m copy-a "src=/root/test/dest=/root/directory_mode=0777"
Force, which defaults to Yes and replaces remote files when the remote file is inconsistent with the local file content. Files are transferred only when the remote destination file does not exist.
The group to which the groups file or directory belongs.
[Email protected] ~]# ansible webservers-m copy-a "Src=/root/test/testgroup dest=/root/test1/testgroup group= Liuzhenwei "
Mode file or directory permissions, such as 0644.
[Email protected] ~]# ansible webservers-m copy-a "Src=/root/test/testgroup dest=/root/test1/testgroup mode=0755"
Take a look at the return value of the copy module, the meaning of each representative:
SRC source file path to be copied to the remote node
Backup_file the backup file path on the remote node, Backup=yes
Owner ID of UID file
Dest the absolute path of the destination file on the remote node,/root/file.txt
Checksum checksum value
md5sum MD5 Checksum value
State status, such as file
The owning group ID of the GID file
Permissions for the mode file
Owner File Owners
Group files belong to groups
Size File sizes
This article is from "Custom" blog, declined reprint!
Ansible Module Introduction