1. 主机联通性测试
可以使用ping模块来进行主机连通性测试:
root@ansible-server:~/liusu# ansible Client -m ping
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
2. command 模块
这个模块可以直接在远程主机上执行命令,并将结果返回本主机:
root@ansible-server:~/liusu# ansible Client -m command -a 'ss -ntl'
node1 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
3. shell 模块
shell 模块可以再远程主机上调用shell解释器执行命令,支持管道等:
root@ansible-server:~/liusu# ansible Client -m shell -a 'cat /etc/passwd |grep root'
node1 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
4. copy 模块
用于复制文件到远程主机,同时支持给定内容生成文件和修改权限等:
root@ansible-server:~/liusu# ansible Client -m copy -a 'src=/root/liusu/hello dest=/data/hello'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/data/hello",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1616407813.69-8951-278263912343377/source",
"state": "file",
"uid": 0
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m copy -a 'content="I am keer\n" dest=/data/name mode=666'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"checksum": "0421570938940ea784f9d8598dab87f07685b968",
"dest": "/data/name",
"gid": 0,
"group": "root",
"md5sum": "497fa8386590a5fc89090725b07f175c",
"mode": "0666",
"owner": "root",
"size": 10,
"src": "/root/.ansible/tmp/ansible-tmp-1616407854.47-8989-109167042354371/source",
"state": "file",
"uid": 0
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'ls -l /data/'
node1 | CHANGED | rc=0 >>
total 8
-rw-r--r-- 1 root root 0 3月 22 09:26 aaa.jpg
drwxr-xr-x 2 root root 4096 3月 22 09:23 app
lrwxrwxrwx 1 root root 7 3月 22 09:26 bbb.jpg -> aaa.jpg
-rw-r--r-- 1 root root 0 3月 22 18:10 hello
-rw-rw-rw- 1 root root 10 3月 22 18:10 name
5. file 模块
用于设置文件的属性,包括创建、删除文件和创建链接文件:
root@ansible-server:~/liusu# ansible Client -m copy -a 'src=/root/liusu/hello dest=/data/hello'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/data/hello",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1616407813.69-8951-278263912343377/source",
"state": "file",
"uid": 0
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m copy -a 'content="I am keer\n" dest=/data/name mode=666'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"checksum": "0421570938940ea784f9d8598dab87f07685b968",
"dest": "/data/name",
"gid": 0,
"group": "root",
"md5sum": "497fa8386590a5fc89090725b07f175c",
"mode": "0666",
"owner": "root",
"size": 10,
"src": "/root/.ansible/tmp/ansible-tmp-1616407854.47-8989-109167042354371/source",
"state": "file",
"uid": 0
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'ls -l /data/'
node1 | CHANGED | rc=0 >>
total 8
-rw-r--r-- 1 root root 0 3月 22 09:26 aaa.jpg
drwxr-xr-x 2 root root 4096 3月 22 09:23 app
lrwxrwxrwx 1 root root 7 3月 22 09:26 bbb.jpg -> aaa.jpg
-rw-r--r-- 1 root root 0 3月 22 18:10 hello
-rw-rw-rw- 1 root root 10 3月 22 18:10 name
root@ansible-server:~/liusu# clear
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m file -a 'path=/data/app state=directory'
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/data/app",
"size": 4096,
"state": "directory",
"uid": 0
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'ls -l /data'
node1 | CHANGED | rc=0 >>
total 8
-rw-r--r-- 1 root root 0 3月 22 09:26 aaa.jpg
drwxr-xr-x 2 root root 4096 3月 22 09:23 app
lrwxrwxrwx 1 root root 7 3月 22 09:26 bbb.jpg -> aaa.jpg
-rw-r--r-- 1 root root 0 3月 22 18:10 hello
-rw-rw-rw- 1 root root 10 3月 22 18:10 name
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m file -a 'path=/data/ccc.jpg src=aaa.jpg state=link'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"dest": "/data/ccc.jpg",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 7,
"src": "aaa.jpg",
"state": "link",
"uid": 0
}
root@ansible-server:~/liusu# ansible Client -m shell -a 'ls -l /data'
node1 | CHANGED | rc=0 >>
total 8
-rw-r--r-- 1 root root 0 3月 22 09:26 aaa.jpg
drwxr-xr-x 2 root root 4096 3月 22 09:23 app
lrwxrwxrwx 1 root root 7 3月 22 09:26 bbb.jpg -> aaa.jpg
lrwxrwxrwx 1 root root 7 3月 22 18:15 ccc.jpg -> aaa.jpg
-rw-r--r-- 1 root root 0 3月 22 18:10 hello
-rw-rw-rw- 1 root root 10 3月 22 18:10 name
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m file -a 'path=/data/hello state=absent'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"path": "/data/hello",
"state": "absent"
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'ls -l /data'
node1 | CHANGED | rc=0 >>
total 8
-rw-r--r-- 1 root root 0 3月 22 09:26 aaa.jpg
drwxr-xr-x 2 root root 4096 3月 22 09:23 app
lrwxrwxrwx 1 root root 7 3月 22 09:26 bbb.jpg -> aaa.jpg
lrwxrwxrwx 1 root root 7 3月 22 18:15 ccc.jpg -> aaa.jpg
-rw-rw-rw- 1 root root 10 3月 22 18:10 name
6. fetch 模块
用来从远程某主机获取(复制)文件到本地:
root@ansible-server:~/liusu# ansible Client -m fetch -a 'src=/data/aaa.jpg dest=/data'
node1 | CHANGED => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/data/node1/data/aaa.jpg",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"remote_md5sum": null
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ls /data
node1
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# cd /data/node1/data/
root@ansible-server:/data/node1/data# pwd
/data/node1/data
root@ansible-server:/data/node1/data# ls
aaa.jpg
7. cron 模块
该模块适用于管理cron计划任务:
root@ansible-server:~/liusu# ansible Client -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/bin/ntpdate 192.168.100.0 & > /dev/null"'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": [
"ntp update every 5 min"
]
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'crontab -l'
node1 | CHANGED | rc=0 >>
#Ansible: ntp update every 5 min
*/5 * * * * /bin/ntpdate 192.168.100.0 & > /dev/null
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m cron -a 'name="ntp update every 5 min" job="/bin/ntpdate 192.168.100.0 & > /dev/null" state=absent'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": []
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'crontab -l'
node1 | CHANGED | rc=0 >>
8. apt 模块
用于软件安装:
root@ansible-server:~/liusu# ansible Client -m apt -a 'name=nginx state=present'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"cache_update_time": 1616356928,
"cache_updated": false,
"changed": true,
"stderr": "",
"stderr_lines": [],
"stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following NEW packages will be installed:\n nginx\n0 upgraded, 1 newly installed, 0 to remove and 117 not upgraded.\nNeed to get 0 B/3596 B of archives.\nAfter this operation, 44.0 kB of additional disk space will be used.\nSelecting previously unselected package nginx.\r\n(Reading database ... \r(Reading database ... 5%\r(Reading database ... 10%\r(Reading database ... 15%\r(Reading database ... 20%\r(Reading database ... 25%\r(Reading database ... 30%\r(Reading database ... 35%\r(Reading database ... 40%\r(Reading database ... 45%\r(Reading database ... 50%\r(Reading database ... 55%\r(Reading database ... 60%\r(Reading database ... 65%\r(Reading database ... 70%\r(Reading database ... 75%\r(Reading database ... 80%\r(Reading database ... 85%\r(Reading database ... 90%\r(Reading database ... 95%\r(Reading database ... 100%\r(Reading database ... 168222 files and directories currently installed.)\r\nPreparing to unpack .../nginx_1.14.0-0ubuntu1.7_all.deb ...\r\nUnpacking nginx (1.14.0-0ubuntu1.7) ...\r\nSetting up nginx (1.14.0-0ubuntu1.7) ...\r\n",
"stdout_lines": [
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"The following NEW packages will be installed:",
" nginx",
"0 upgraded, 1 newly installed, 0 to remove and 117 not upgraded.",
"Need to get 0 B/3596 B of archives.",
"After this operation, 44.0 kB of additional disk space will be used.",
"Selecting previously unselected package nginx.",
"(Reading database ... ",
"(Reading database ... 5%",
"(Reading database ... 10%",
"(Reading database ... 15%",
"(Reading database ... 20%",
"(Reading database ... 25%",
"(Reading database ... 30%",
"(Reading database ... 35%",
"(Reading database ... 40%",
"(Reading database ... 45%",
"(Reading database ... 50%",
"(Reading database ... 55%",
"(Reading database ... 60%",
"(Reading database ... 65%",
"(Reading database ... 70%",
"(Reading database ... 75%",
"(Reading database ... 80%",
"(Reading database ... 85%",
"(Reading database ... 90%",
"(Reading database ... 95%",
"(Reading database ... 100%",
"(Reading database ... 168222 files and directories currently installed.)",
"Preparing to unpack .../nginx_1.14.0-0ubuntu1.7_all.deb ...",
"Unpacking nginx (1.14.0-0ubuntu1.7) ...",
"Setting up nginx (1.14.0-0ubuntu1.7) ..."
]
}
9. service 模块
用于服务程序的管理:
root@ansible-server:~/liusu# ansible Client -m service -a 'name=nginx state=started enabled=true'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"enabled": true,
"name": "nginx",
"state": "started",
"status": {
"ActiveEnterTimestamp": "Mon 2021-03-22 09:39:37 CST",
"ActiveEnterTimestampMonotonic": "142777512908",
"ActiveExitTimestamp": "Mon 2021-03-22 09:40:44 CST",
"ActiveExitTimestampMonotonic": "142844354134",
"ActiveState": "inactive",
"After": "network.target basic.target sysinit.target systemd-journald.socket system.slice",
"AllowIsolate": "no",
"AmbientCapabilities": "",
"AssertResult": "yes",
"AssertTimestamp": "Mon 2021-03-22 09:39:37 CST",
"AssertTimestampMonotonic": "142777470480",
"Before": "multi-user.target shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "[not set]",
"CPUAccounting": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "[not set]",
"CPUUsageNSec": "[not set]",
"CPUWeight": "[not set]",
"CacheDirectoryMode": "0755",
"CanIsolate": "no",
"CanReload": "yes",
"CanStart": "yes",
"CanStop": "yes",
"CapabilityBoundingSet": "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap cap_mac_override cap_mac_admin cap_syslog cap_wake_alarm cap_block_suspend",
"CollectMode": "inactive",
"ConditionResult": "yes",
"ConditionTimestamp": "Mon 2021-03-22 09:39:37 CST",
"ConditionTimestampMonotonic": "142777470480",
"ConfigurationDirectoryMode": "0755",
"Conflicts": "shutdown.target",
"ControlPID": "0",
"DefaultDependencies": "yes",
"Delegate": "no",
"Description": "A high performance web server and a reverse proxy server",
"DevicePolicy": "auto",
"Documentation": "man:nginx(8)",
"DynamicUser": "no",
"ExecMainCode": "1",
"ExecMainExitTimestamp": "Mon 2021-03-22 09:40:44 CST",
"ExecMainExitTimestampMonotonic": "142844360751",
"ExecMainPID": "18374",
"ExecMainStartTimestamp": "Mon 2021-03-22 09:39:37 CST",
"ExecMainStartTimestampMonotonic": "142777512889",
"ExecMainStatus": "0",
"ExecReload": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -g daemon on; master_process on; -s reload ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStart": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -g daemon on; master_process on; ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStartPre": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -t -q -g daemon on; master_process on; ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStop": "{ path=/sbin/start-stop-daemon ; argv[]=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid ; ignore_errors=yes ; start_time=[Mon 2021-03-22 09:40:44 CST] ; stop_time=[Mon 2021-03-22 09:40:44 CST] ; pid=18896 ; code=exited ; status=0 }",
"FailureAction": "none",
"FileDescriptorStoreMax": "0",
"FragmentPath": "/lib/systemd/system/nginx.service",
"GID": "[not set]",
"GuessMainPID": "yes",
"IOAccounting": "no",
"IOSchedulingClass": "0",
"IOSchedulingPriority": "0",
"IOWeight": "[not set]",
"IPAccounting": "no",
"IPEgressBytes": "18446744073709551615",
"IPEgressPackets": "18446744073709551615",
"IPIngressBytes": "18446744073709551615",
"IPIngressPackets": "18446744073709551615",
"Id": "nginx.service",
"IgnoreOnIsolate": "no",
"IgnoreSIGPIPE": "yes",
"InactiveEnterTimestamp": "Mon 2021-03-22 09:40:44 CST",
"InactiveEnterTimestampMonotonic": "142844376947",
"InactiveExitTimestamp": "Mon 2021-03-22 09:39:37 CST",
"InactiveExitTimestampMonotonic": "142777471184",
"InvocationID": "ad68a26de02746e1a69eda34a1f55e78",
"JobRunningTimeoutUSec": "infinity",
"JobTimeoutAction": "none",
"JobTimeoutUSec": "infinity",
"KeyringMode": "private",
"KillMode": "mixed",
"KillSignal": "15",
"LimitAS": "infinity",
"LimitASSoft": "infinity",
"LimitCORE": "infinity",
"LimitCORESoft": "0",
"LimitCPU": "infinity",
"LimitCPUSoft": "infinity",
"LimitDATA": "infinity",
"LimitDATASoft": "infinity",
"LimitFSIZE": "infinity",
"LimitFSIZESoft": "infinity",
"LimitLOCKS": "infinity",
"LimitLOCKSSoft": "infinity",
"LimitMEMLOCK": "16777216",
"LimitMEMLOCKSoft": "16777216",
"LimitMSGQUEUE": "819200",
"LimitMSGQUEUESoft": "819200",
"LimitNICE": "0",
"LimitNICESoft": "0",
"LimitNOFILE": "4096",
"LimitNOFILESoft": "1024",
"LimitNPROC": "15553",
"LimitNPROCSoft": "15553",
"LimitRSS": "infinity",
"LimitRSSSoft": "infinity",
"LimitRTPRIO": "0",
"LimitRTPRIOSoft": "0",
"LimitRTTIME": "infinity",
"LimitRTTIMESoft": "infinity",
"LimitSIGPENDING": "15553",
"LimitSIGPENDINGSoft": "15553",
"LimitSTACK": "infinity",
"LimitSTACKSoft": "8388608",
"LoadState": "loaded",
"LockPersonality": "no",
"LogLevelMax": "-1",
"LogsDirectoryMode": "0755",
"MainPID": "0",
"MemoryAccounting": "no",
"MemoryCurrent": "[not set]",
"MemoryDenyWriteExecute": "no",
"MemoryHigh": "infinity",
"MemoryLimit": "infinity",
"MemoryLow": "0",
"MemoryMax": "infinity",
"MemorySwapMax": "infinity",
"MountAPIVFS": "no",
"MountFlags": "",
"NFileDescriptorStore": "0",
"NRestarts": "0",
"Names": "nginx.service",
"NeedDaemonReload": "no",
"Nice": "0",
"NoNewPrivileges": "no",
"NonBlocking": "no",
"NotifyAccess": "none",
"OOMScoreAdjust": "0",
"OnFailureJobMode": "replace",
"PIDFile": "/run/nginx.pid",
"PermissionsStartOnly": "no",
"Perpetual": "no",
"PrivateDevices": "no",
"PrivateNetwork": "no",
"PrivateTmp": "no",
"PrivateUsers": "no",
"ProtectControlGroups": "no",
"ProtectHome": "no",
"ProtectKernelModules": "no",
"ProtectKernelTunables": "no",
"ProtectSystem": "no",
"RefuseManualStart": "no",
"RefuseManualStop": "no",
"RemainAfterExit": "no",
"RemoveIPC": "no",
"Requires": "system.slice sysinit.target",
"Restart": "no",
"RestartUSec": "100ms",
"RestrictNamespaces": "no",
"RestrictRealtime": "no",
"RestrictSUIDSGID": "no",
"Result": "success",
"RootDirectoryStartOnly": "no",
"RuntimeDirectoryMode": "0755",
"RuntimeDirectoryPreserve": "no",
"RuntimeMaxUSec": "infinity",
"SameProcessGroup": "no",
"SecureBits": "0",
"SendSIGHUP": "no",
"SendSIGKILL": "yes",
"Slice": "system.slice",
"StandardError": "inherit",
"StandardInput": "null",
"StandardInputData": "",
"StandardOutput": "journal",
"StartLimitAction": "none",
"StartLimitBurst": "5",
"StartLimitIntervalUSec": "10s",
"StartupBlockIOWeight": "[not set]",
"StartupCPUShares": "[not set]",
"StartupCPUWeight": "[not set]",
"StartupIOWeight": "[not set]",
"StateChangeTimestamp": "Mon 2021-03-22 09:40:44 CST",
"StateChangeTimestampMonotonic": "142844376947",
"StateDirectoryMode": "0755",
"StatusErrno": "0",
"StopWhenUnneeded": "no",
"SubState": "dead",
"SuccessAction": "none",
"SyslogFacility": "3",
"SyslogLevel": "6",
"SyslogLevelPrefix": "yes",
"SyslogPriority": "30",
"SystemCallErrorNumber": "0",
"TTYReset": "no",
"TTYVHangup": "no",
"TTYVTDisallocate": "no",
"TasksAccounting": "yes",
"TasksCurrent": "[not set]",
"TasksMax": "4665",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "5s",
"TimerSlackNSec": "50000",
"Transient": "no",
"Type": "forking",
"UID": "[not set]",
"UMask": "0022",
"UnitFilePreset": "enabled",
"UnitFileState": "enabled",
"UtmpMode": "init",
"WantedBy": "multi-user.target",
"WatchdogTimestampMonotonic": "0",
"WatchdogUSec": "0"
}
}
查看端口是否打开:
root@ansible-server:~/liusu# ansible Client -m shell -a 'ss -ntl'
node1 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
关闭服务:
root@ansible-server:~/liusu# ansible Client -m service -a 'name=nginx state=stopped'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "nginx",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Mon 2021-03-22 18:42:27 CST",
"ActiveEnterTimestampMonotonic": "175347622573",
"ActiveExitTimestamp": "Mon 2021-03-22 09:40:44 CST",
"ActiveExitTimestampMonotonic": "142844354134",
"ActiveState": "active",
"After": "network.target basic.target sysinit.target systemd-journald.socket system.slice",
"AllowIsolate": "no",
"AmbientCapabilities": "",
"AssertResult": "yes",
"AssertTimestamp": "Mon 2021-03-22 18:42:27 CST",
"AssertTimestampMonotonic": "175347601844",
"Before": "multi-user.target shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "[not set]",
"CPUAccounting": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "[not set]",
"CPUUsageNSec": "[not set]",
"CPUWeight": "[not set]",
"CacheDirectoryMode": "0755",
"CanIsolate": "no",
"CanReload": "yes",
"CanStart": "yes",
"CanStop": "yes",
"CapabilityBoundingSet": "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap cap_mac_override cap_mac_admin cap_syslog cap_wake_alarm cap_block_suspend",
"CollectMode": "inactive",
"ConditionResult": "yes",
"ConditionTimestamp": "Mon 2021-03-22 18:42:27 CST",
"ConditionTimestampMonotonic": "175347601844",
"ConfigurationDirectoryMode": "0755",
"Conflicts": "shutdown.target",
"ControlGroup": "/system.slice/nginx.service",
"ControlPID": "0",
"DefaultDependencies": "yes",
"Delegate": "no",
"Description": "A high performance web server and a reverse proxy server",
"DevicePolicy": "auto",
"Documentation": "man:nginx(8)",
"DynamicUser": "no",
"ExecMainCode": "0",
"ExecMainExitTimestampMonotonic": "0",
"ExecMainPID": "22759",
"ExecMainStartTimestamp": "Mon 2021-03-22 18:42:27 CST",
"ExecMainStartTimestampMonotonic": "175347622554",
"ExecMainStatus": "0",
"ExecReload": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -g daemon on; master_process on; -s reload ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStart": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -g daemon on; master_process on; ; ignore_errors=no ; start_time=[Mon 2021-03-22 18:42:27 CST] ; stop_time=[Mon 2021-03-22 18:42:27 CST] ; pid=22758 ; code=exited ; status=0 }",
"ExecStartPre": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -t -q -g daemon on; master_process on; ; ignore_errors=no ; start_time=[Mon 2021-03-22 18:42:27 CST] ; stop_time=[Mon 2021-03-22 18:42:27 CST] ; pid=22757 ; code=exited ; status=0 }",
"ExecStop": "{ path=/sbin/start-stop-daemon ; argv[]=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid ; ignore_errors=yes ; start_time=[Mon 2021-03-22 09:40:44 CST] ; stop_time=[Mon 2021-03-22 09:40:44 CST] ; pid=18896 ; code=exited ; status=0 }",
"FailureAction": "none",
"FileDescriptorStoreMax": "0",
"FragmentPath": "/lib/systemd/system/nginx.service",
"GID": "[not set]",
"GuessMainPID": "yes",
"IOAccounting": "no",
"IOSchedulingClass": "0",
"IOSchedulingPriority": "0",
"IOWeight": "[not set]",
"IPAccounting": "no",
"IPEgressBytes": "18446744073709551615",
"IPEgressPackets": "18446744073709551615",
"IPIngressBytes": "18446744073709551615",
"IPIngressPackets": "18446744073709551615",
"Id": "nginx.service",
"IgnoreOnIsolate": "no",
"IgnoreSIGPIPE": "yes",
"InactiveEnterTimestamp": "Mon 2021-03-22 09:40:44 CST",
"InactiveEnterTimestampMonotonic": "142844376947",
"InactiveExitTimestamp": "Mon 2021-03-22 18:42:27 CST",
"InactiveExitTimestampMonotonic": "175347602530",
"InvocationID": "6e3770c5fdf64e2a887f7fd8c52b061b",
"JobRunningTimeoutUSec": "infinity",
"JobTimeoutAction": "none",
"JobTimeoutUSec": "infinity",
"KeyringMode": "private",
"KillMode": "mixed",
"KillSignal": "15",
"LimitAS": "infinity",
"LimitASSoft": "infinity",
"LimitCORE": "infinity",
"LimitCORESoft": "0",
"LimitCPU": "infinity",
"LimitCPUSoft": "infinity",
"LimitDATA": "infinity",
"LimitDATASoft": "infinity",
"LimitFSIZE": "infinity",
"LimitFSIZESoft": "infinity",
"LimitLOCKS": "infinity",
"LimitLOCKSSoft": "infinity",
"LimitMEMLOCK": "16777216",
"LimitMEMLOCKSoft": "16777216",
"LimitMSGQUEUE": "819200",
"LimitMSGQUEUESoft": "819200",
"LimitNICE": "0",
"LimitNICESoft": "0",
"LimitNOFILE": "4096",
"LimitNOFILESoft": "1024",
"LimitNPROC": "15553",
"LimitNPROCSoft": "15553",
"LimitRSS": "infinity",
"LimitRSSSoft": "infinity",
"LimitRTPRIO": "0",
"LimitRTPRIOSoft": "0",
"LimitRTTIME": "infinity",
"LimitRTTIMESoft": "infinity",
"LimitSIGPENDING": "15553",
"LimitSIGPENDINGSoft": "15553",
"LimitSTACK": "infinity",
"LimitSTACKSoft": "8388608",
"LoadState": "loaded",
"LockPersonality": "no",
"LogLevelMax": "-1",
"LogsDirectoryMode": "0755",
"MainPID": "22759",
"MemoryAccounting": "no",
"MemoryCurrent": "[not set]",
"MemoryDenyWriteExecute": "no",
"MemoryHigh": "infinity",
"MemoryLimit": "infinity",
"MemoryLow": "0",
"MemoryMax": "infinity",
"MemorySwapMax": "infinity",
"MountAPIVFS": "no",
"MountFlags": "",
"NFileDescriptorStore": "0",
"NRestarts": "0",
"Names": "nginx.service",
"NeedDaemonReload": "no",
"Nice": "0",
"NoNewPrivileges": "no",
"NonBlocking": "no",
"NotifyAccess": "none",
"OOMScoreAdjust": "0",
"OnFailureJobMode": "replace",
"PIDFile": "/run/nginx.pid",
"PermissionsStartOnly": "no",
"Perpetual": "no",
"PrivateDevices": "no",
"PrivateNetwork": "no",
"PrivateTmp": "no",
"PrivateUsers": "no",
"ProtectControlGroups": "no",
"ProtectHome": "no",
"ProtectKernelModules": "no",
"ProtectKernelTunables": "no",
"ProtectSystem": "no",
"RefuseManualStart": "no",
"RefuseManualStop": "no",
"RemainAfterExit": "no",
"RemoveIPC": "no",
"Requires": "system.slice sysinit.target",
"Restart": "no",
"RestartUSec": "100ms",
"RestrictNamespaces": "no",
"RestrictRealtime": "no",
"RestrictSUIDSGID": "no",
"Result": "success",
"RootDirectoryStartOnly": "no",
"RuntimeDirectoryMode": "0755",
"RuntimeDirectoryPreserve": "no",
"RuntimeMaxUSec": "infinity",
"SameProcessGroup": "no",
"SecureBits": "0",
"SendSIGHUP": "no",
"SendSIGKILL": "yes",
"Slice": "system.slice",
"StandardError": "inherit",
"StandardInput": "null",
"StandardInputData": "",
"StandardOutput": "journal",
"StartLimitAction": "none",
"StartLimitBurst": "5",
"StartLimitIntervalUSec": "10s",
"StartupBlockIOWeight": "[not set]",
"StartupCPUShares": "[not set]",
"StartupCPUWeight": "[not set]",
"StartupIOWeight": "[not set]",
"StateChangeTimestamp": "Mon 2021-03-22 18:42:27 CST",
"StateChangeTimestampMonotonic": "175347622573",
"StateDirectoryMode": "0755",
"StatusErrno": "0",
"StopWhenUnneeded": "no",
"SubState": "running",
"SuccessAction": "none",
"SyslogFacility": "3",
"SyslogLevel": "6",
"SyslogLevelPrefix": "yes",
"SyslogPriority": "30",
"SystemCallErrorNumber": "0",
"TTYReset": "no",
"TTYVHangup": "no",
"TTYVTDisallocate": "no",
"TasksAccounting": "yes",
"TasksCurrent": "5",
"TasksMax": "4665",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "5s",
"TimerSlackNSec": "50000",
"Transient": "no",
"Type": "forking",
"UID": "[not set]",
"UMask": "0022",
"UnitFilePreset": "enabled",
"UnitFileState": "enabled",
"UtmpMode": "init",
"WantedBy": "multi-user.target",
"WatchdogTimestamp": "Mon 2021-03-22 18:42:27 CST",
"WatchdogTimestampMonotonic": "175347622572",
"WatchdogUSec": "0"
}
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'ss -ntl'
node1 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
10. user 模块
用来管理用户账号,添加和删除一个用户:
root@ansible-server:~/liusu# ansible Client -m user -a 'name=moyasu uid=88888'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/moyasu",
"name": "moyasu",
"shell": "/bin/sh",
"state": "present",
"system": false,
"uid": 88888
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'cat /etc/passwd |grep moyasu'
node1 | CHANGED | rc=0 >>
moyasu:x:88888:1001::/home/moyasu:/bin/sh
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m user -a 'name=moyasu state=absent'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"force": false,
"name": "moyasu",
"remove": false,
"state": "absent"
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'cat /etc/passwd |grep moyasu'
node1 | FAILED | rc=1 >>
non-zero return code
11. group 模块
用于添加或删除组:
root@ansible-server:~/liusu# ansible Client -m group -a 'name=hello gid=99999'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 99999,
"name": "hello",
"state": "present",
"system": false
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'cat /etc/group |grep 99999'
node1 | CHANGED | rc=0 >>
hello:x:99999:
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m group -a 'name=hello state=absent'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "hello",
"state": "absent"
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'cat /etc/group |grep 99999'
node1 | FAILED | rc=1 >>
non-zero return code
12. script 模块
用于将本机的脚本在被管理的机器上执行:
root@ansible-server:~/liusu# cat /tmp/df.sh
#!/bin/bash
date >> /tmp/disk_total.log
df -lh >> /tmp/disk_total.log
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# chmod +x /tmp/df.sh
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m script -a '/tmp/df.sh'
node1 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.100.28 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.100.28 closed."
],
"stdout": "",
"stdout_lines": []
}
root@ansible-server:~/liusu#
root@ansible-server:~/liusu#
root@ansible-server:~/liusu# ansible Client -m shell -a 'cat /tmp/disk_total.log'
node1 | CHANGED | rc=0 >>
2021年 03月 22日 星期一 18:59:44 CST
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 394M 1.7M 392M 1% /run
/dev/sda1 9.8G 6.2G 3.2G 66% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/loop2 15M 15M 0 100% /snap/gnome-characters/399
/dev/loop1 4.3M 4.3M 0 100% /snap/gnome-calculator/544
/dev/loop0 1.0M 1.0M 0 100% /snap/gnome-logs/81
/dev/loop3 161M 161M 0 100% /snap/gnome-3-28-1804/116
/dev/loop4 90M 90M 0 100% /snap/core/8268
/dev/loop5 3.8M 3.8M 0 100% /snap/gnome-system-monitor/127
/dev/loop6 45M 45M 0 100% /snap/gtk-common-themes/1440
/dev/loop7 55M 55M 0 100% /snap/core18/1668
tmpfs 394M 52K 394M 1% /run/user/1000
tmpfs 394M 0 394M 0% /run/user/0
tmpfs 394M 20K 394M 1% /run/user/121
/dev/loop8 56M 56M 0 100% /snap/core18/1988
/dev/loop9 100M 100M 0 100% /snap/core/10859
/dev/loop10 384K 384K 0 100% /snap/gnome-characters/570
/dev/loop11 2.5M 2.5M 0 100% /snap/gnome-calculator/884
/dev/loop12 2.3M 2.3M 0 100% /snap/gnome-system-monitor/157
/dev/loop13 640K 640K 0 100% /snap/gnome-logs/103
/dev/loop14 65M 65M 0 100% /snap/gtk-common-themes/1514
/dev/loop15 163M 163M 0 100% /snap/gnome-3-28-1804/145
/dev/loop16 219M 219M 0 100% /snap/gnome-3-34-1804/66
13. setup 模块
该模块主要用于收集信息,是通过调用facts组件来实现的:
root@ansible-server:~/liusu# ansible Client -m setup -a 'filter="*mem*"'
node1 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 413,
"ansible_memory_mb": {
"nocache": {
"free": 2677,
"used": 1258
},
"real": {
"free": 413,
"total": 3935,
"used": 3522
},
"swap": {
"cached": 0,
"free": 472,
"total": 472,
"used": 0
}
},
"ansible_memtotal_mb": 3935,
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}