Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common modules of ansible-

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Common modules of ansible-

According to the official classification, the modules are classified into cloud module, command module, database module, file module, asset module, message module, monitoring module, network module, notification module, package management module, source code control module, system module, unit module, web facility module, windows module.

User: configuring user

Group: configuring user group

Cron: configuring scheduled tasks

Copy: copying files to a remote host

File: for profile properties

Yum: used to install software packages

Service: used to manage servic

Shell: used to execute commands with "|" pipe symbols, etc.

Scripts: execute script files on the control side on the remote host

Setup: view basic information of remote hosts

Filesystem: creating file systems on block Devic

Mount: configure mount point

Synchronize: synchronizing files with rsync

Get_url: this module is mainly used to download files from http, ftp, and https servers (similar to wget)

Package: install, upgrade, and delete packages using the os package manager

Stat: gets the file status information of the remote host.

Unarchive: used to extract files

Command: execute commands on remote host

Raw: similar to shell module, supporting pipes

Ping: used to detect whether the remote host is alive

1) ping module

Test whether the host is connected or not, the usage is very simple

[root@test-1ansible] # ansible test- m ping [DEPRECATIONWARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is ageneric framework. See become_user. , use become instead. This feature will beremoved inversion 2.8. Deprecation warnings can be disabled by settingdeprecation_warnings=False in ansible.cfg.192.168.3.174 | SUCCESS = > {"changed": false, "ping": "pong"} 192.168.3.175 | SUCCESS = > {"changed": false, "ping": "pong"}

2) file- module

The file module is mainly used for file manipulation on remote hosts, and the file module contains the following options:

Force: you need to force the creation of a soft connection in two cases.

One is when the source file does not exist but will be created later

The other is that the target soft link already exists. You need to cancel the previous soft link, and then create a new one. There are two options: yes | no

Group: define the subordinate group of the file / directory

Owner: define the owner of the file / directory

Mode: define permissions for files / directories

Path: required, define the path to the file / directory

Recurse: recursively sets the properties of a file, valid only for directories

Src: the path of the source file to be linked, only in the case of state=link

Dest: the path to be linked to, only in the case of state=link

State: defining file statu

Directory: if the directory does not exist, create the directory

File: even if the file does not exist, it will not be created

Link: creating soft links

Hard: creating hard links

Touch: if the file does not exist, a new file is created, and if the file or directory already exists, its last modification time is updated

Absent: delete directories, files, or unlink files

Case 1-create a soft connection / etc/fstab to / tmp/fstab with file

[root@test-1ansible] # ansible test- m file-a "src=/etc/fstab dest=/tmp/fstab state=link" [DEPRECATIONWARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is ageneric framework. See become_user. , use become instead. This feature will beremoved inversion 2.8. Deprecation warnings can be disabled by settingdeprecation_warnings=False in ansible.cfg.192.168.3.175 | CHANGED = > {"changed": true, "dest": "/ tmp/fstab", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10 "src": "/ etc/fstab", "state": "link", "uid": 0} 192.168.3.174 | CHANGED = > {"changed": true, "dest": "/ tmp/fstab", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0" "size": 10, "src": "/ etc/fstab", "state": "link", "uid": 0}

Execution result

[root@test-2tmp] # lltotal 8lrwxrwxrwx.1 root root 10 Nov 19 02:44 fstab- > / etc/fstab-rwx-.1 root root 836 Oct 24 09:40 ks-script-6aY4Ugdrwx-.3 root root 17 Oct 25 23:39systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10-rw-r--r--.1 root root 8 Oct 25 22:26 test.txtdrwx-.2 root root 6 Oct 24 09:44 vmware-root- Rw-.1 root root 0 Oct 24 09:36 yum.log

Case 2-We need to create a file file on the remote server / tmp/

[root@test-1ansible] # ansible test- m file-a 'path=/tmp/file state=touch' [DEPRECATIONWARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is ageneric framework. See become_user. , use become instead. This feature will beremoved inversion 2.8. Deprecation warnings can be disabled by settingdeprecation_warnings=False in ansible.cfg.192.168.3.174 | CHANGED = > {"changed": true, "dest": "/ tmp/file", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file" "uid": 0

Execution result:

[root@test-2 tmp] # lltotal 8 Murray RW Murray. 1 root root 0 Nov 19 03:12 filelrwxrwxrwx. 1 root root 10 Nov 19 02:44 fstab- > / etc/fstab-rwx-. 1 root root 836 Oct 24 09:40 ks-script-6aY4Ugdrwx-. 3 root root 17 Oct 25 23:39 systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10-rw-r--r--. 1 root root 8 Oct 25 22:26 test.txtdrwx-. 2 root root 6 Oct 24 09:44 vmware-root-rw-. 1 root root 0 Oct 24 09:36 yum.log

Case 3-requires file permissions to modify file under the remote test group

[root@test-1ansible] # ansible test- m file-a "path=/tmp/file mode=755" [DEPRECATIONWARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is ageneric framework. See become_user. , use become instead. This feature will beremoved inversion 2.8. Deprecation warnings can be disabled by settingdeprecation_warnings=False in ansible.cfg.192.168.3.174 | CHANGED = > {"changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/ tmp/file", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file" "uid": 0}

Execution result:

[root@test-1ansible] # ansible test- m command-a'ls-lh / tmp/' [DEPRECATIONWARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is ageneric framework. See become_user. , use become instead. This feature will beremoved inversion 2.8. Deprecation warnings can be disabled by settingdeprecation_warnings=False in ansible.cfg.192.168.3.174 | CHANGED | rc=0 > > total 8.0Kdrwx-.2 root root 80 Nov 19 03:20ansible_command_payload_8myj39-rwxr-xr-x.1 root root 0 Nov 19 03:12 filelrwxrwxrwx.1 root root 10 Nov 19 02:44 fstab- > / etc/fstab-rwx-.1 root root 836 Oct 24 09:40 ks-script-6aY4Ugdrwx-.3 root root 17 Oct 25 23:39systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10-rw-r--r--.1 root root 8 Oct 25 22:26 test.txtdrwx-.2 root root 6 Oct 24 09:44 vmware-root-rw-.1 root root 0 Oct 24 09:36 yum.log

3) copy module

Backup: back up the original file before overwriting. The backup file contains time information. There are two options: yes | no

Content: used instead of "src", you can directly set the value of the specified file

Dest: required. The absolute path of the remote host to which the source file is to be copied, and if the source file is a directory, the path must also be a directory

Directory_mode: recursively sets the permissions of the directory, which defaults to the system default permissions

Force: if the target host contains the file, but the content is different, if it is set to yes, it is forced to be overwritten, and if it is no, it is copied only if the file does not exist at the target host's destination location. Default is yes

Others: the options in all file modules can be used here

Src: the local address of the file to be copied to the remote host, either an absolute path or a relative path. If the path is a directory, it will be copied recursively. In this case, if the path ends with "/", only the contents of the directory are copied, and if it does not end with "/", the entire content, including the directory, is copied, similar to rsync.

Validate: The validation command to run before copying into place. The path to the file to validate is passed in via'% s' which must be present as in the visudo example bel

Case 1-directory from locally copied files to ansibel destination

[root@test-1 ansible] # touch / tmp/aa # aa file created by local test [root@test-1 ansible] # ansible test- m copy-a "src=/tmp/aa dest=/tmp/aa" [DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will beremoved in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.192.168.3.174 | CHANGED = > {"changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/ tmp/aa", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root" "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 0, "src": "/ root/.ansible/tmp/ansible-tmp-1542616228.03-86258002572076/source", "state": "file", "uid": 0}

Execution result:

[root@test-2 tmp] # lltotal 8 Murray RW Murray. 1 root root 0 Nov 19 03:30 aa-rwxr-xr-x. 1 root root 0 Nov 19 03:12 filelrwxrwxrwx. 1 root root 10 Nov 19 02:44 fstab- > / etc/fstab-rwx-. 1 root root 836 Oct 24 09:40 ks-script-6aY4Ugdrwx-. 3 root root 17 Oct 25 23:39 systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10-rw-r--r--. 1 root root 8 Oct 25 22:26 test.txtdrwx-. 2 root root 6 Oct 24 09:44 vmware-root-rw-. 1 root root 0 Oct 24 09:36 yum.log

3.2) case 2-ansible uses backup for backup

[root@test-1ansible] # vim / tmp/aa [root@test-1ansible] # ansible test- m copy-a "src=/tmp/aa dest=/tmp/aabackup=yes" [DEPRECATIONWARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is ageneric framework. See become_user. , use become instead. This feature will beremoved inversion 2.8. | Deprecation warnings can be disabled by settingdeprecation_warnings=False in ansible.cfg.192.168.3.174 | CHANGED = > {"backup_file": "/ tmp/aa.3042.2018-11-19303 backup_file 37VR 06"," changed ": true," checksum ":" 5730dd3a58d64a39a7fc704c3c5570d70303d9db "," dest ":" / tmp/aa "," gid ": 0," group ":" root "," md5sum ":" 96fdb0b7ddbb489f8636769965584623 "," mode ":" 0644 " "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 35, "src": "/ root/.ansible/tmp/ansible-tmp-1542616624.56-136259009428901/source", "state": "file", "uid": 0

Execution result

[root@test-2 tmp] # lltotal 16 RW Murray. 1 root root 35 Nov 19 03:37 aa-rw-r--r--. 1 root root 56 Nov 19 03:36 aa.3042.2018-11-1900 03 aa.3042.2018 37 aa.3042.2018 06 ~ # this is the-rwxr-xr-x used by ansible. 1 root root 0 Nov 19 03:12 filelrwxrwxrwx. 1 root root 10 Nov 19 02:44 fstab- > / etc/fstab-rwx-. 1 root root 836 Oct 24 09:40 ks-script-6aY4Ugdrwx-. 3 root root 17 Oct 25 23:39 systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10-rw-r--r--. 1 root root 8 Oct 25 22:26 test.txtdrwx-. 2 root root 6 Oct 24 09:44 vmware-root-rw-. 1 root root 0 Oct 24 09:36 yum.log

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report