天天看點

Ansible 學習之常用子產品

子產品相關指令

(1)列出 ansible 支援的子產品

(2)檢視該子產品的幫助資訊

以下實驗均是在 IP 位址為: 192.168.91.128 的虛拟機上操作的。

/etc/ansible/hosts 檔案中配置如下:

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
[Client]


# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com
           

常用子產品示例

ping 子產品

檢查指定節點機器能否連通,如果主機線上,則回複pong。

例如:

[root@xdhuxc ~]# ansible Client -m ping
 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@xdhuxc ~]# 
           
遠端指令子產品(command、script、shell)

command 作為 ansible 的預設子產品,可以運作遠端權限範圍所有的 shell 指令,不支援管道符。

例如:

執行結果為:

[root@xdhuxc ~]# ansible Client -m command -a "free -h"
.91.130 | SUCCESS | rc= >>
              total        used        free      shared  buff/cache   available
Mem:           M        M        M        M        M        M
Swap:          G          B        G
           

script 的功能是在遠端主機執行主要端存儲的 shell 腳本檔案,相當于 scp + shell 組合。

例如:

xdhuxc.sh 腳本的内容為:

[[email protected] ~]# pwd
/root
[[email protected] ~]# ls
xdhuxc.sh
[[email protected] ~]# cat xdhuxc.sh 
#!/usr/bin/env bash
echo "Hello Shell."
echo $1
echo $2 >> /root/abc.sh
           

執行結果為:

[root@xdhuxc ~]# ansible Client -m script -a "/root/xdhuxc.sh string buffer"
 | SUCCESS => {
    "changed": true, 
    "rc": , 
    "stderr": "Shared connection to 192.168.91.130 closed.\r\n", 
    "stdout": "Hello Shell.\r\nstring\r\n", 
    "stdout_lines": [
        "Hello Shell.", 
        "string"
    ]
}
           

在 192.168.91.130上,在 root 目錄下,會産生 abc.sh 檔案。

[root@xdhuxc ~]# pwd
/root
[root@xdhuxc ~]# ls
abc.sh  anaconda-ks.cfg  ~None
[root@xdhuxc ~]# cat abc.sh 
buffer
[root@xdhuxc ~]# 
           

~None 目錄下

[root@xdhuxc ~None]# pwd
/root/~None
[root@xdhuxc ~None]# tree -a
.
└── .ansible
    └── tmp

 directories,  files
           

shell 的功能是執行遠端主機上的 shell 腳本檔案,支援管道符。

例如:

在 root 目錄下建立 xdhuxc.sh 檔案,

[[email protected] ~]# pwd
/root
[[email protected] ~]# ll
total 
-rw-r--r--   root root     Jun  : abc.sh
-rw-------.  root root  Apr   : anaconda-ks.cfg
drwx------   root root    Jun  : ~None
-rw-r--r--   root root    Jun  : xdhuxc.sh   # xdhuxc.sh 沒有執行權限。
[[email protected] ~]# cat xdhuxc.sh 
#!/usr/bin/env bash
echo -n "Hello World!"
           

在 xdhuxc.sh 沒有被賦予執行權限時,執行 ansible 指令。

[root@xdhuxc ~]# ansible Client -m shell -a "/root/xdhuxc.sh"
. | FAILED | rc= >>
/bin/sh: /root/xdhuxc.sh: Permission deniednon-zero return code
           

很明顯,xdhuxc.sh 沒有可執行權限導緻的,修改該指令,重新執行,指令執行成功。

[[email protected] ~]# ansible Client -m shell -a "chmod +x /root/xdhuxc.sh ; /root/xdhuxc.sh"
 [WARNING]: Consider using the file module with mode rather than running chmod.  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.

 | SUCCESS | rc= >>
Hello World!
           
copy 子產品

copy 子產品用于實作主要端向目标機器複制檔案,類似于 scp 功能。

例如:

執行結果如下:

[root@xdhuxc ~]# pwd
/root
[root@xdhuxc ~]# ls
xdhuxc.sh
[root@xdhuxc ~]# ansible Client -m copy -a "src=/root/xdhuxc.sh dest=/tmp/ owner=root group=root mode=0755"
 | SUCCESS => {
    "changed": true, 
    "checksum": "406eb4b32e5fe321308cfd40365f765685d64d58", 
    "dest": "/tmp/xdhuxc.sh", 
    "gid": , 
    "group": "root", 
    "md5sum": "1abf4cb799e8858ab96bb7035837f248", 
    "mode": "0755", 
    "owner": "root", 
    "size": , 
    "src": "~None/.ansible/tmp/ansible-tmp-1528728245.0-152168028566440/source", 
    "state": "file", 
    "uid": 
}
           

在 192.168.91.130 機器上

[root@xdhuxc ~None]# pwd
/root/~None
[root@xdhuxc ~None]# tree -a
.
└── .ansible
    └── tmp
        └── ansible-tmp--

 directories,  files
[root@xdhuxc ~None]# ls /tmp
xdhuxc.sh
           
stat 子產品

stat 子產品用于擷取遠端檔案狀态資訊,atime/ctime/mtime/md5/uid/gid等資訊。

例如:

執行結果如下:

[root@xdhuxc ~]# ansible Client -m stat -a "path=/etc/resolv.conf"
 | SUCCESS => {
    "changed": false, 
    "stat": {
        "atime": , 
        "attr_flags": "", 
        "attributes": [], 
        "block_size": , 
        "blocks": , 
        "charset": "us-ascii", 
        "checksum": "dfebfbbbb5ea787130ffb53eaab925c743f50a56", 
        "ctime": , 
        "dev": , 
        "device_type": , 
        "executable": false, 
        "exists": true, 
        "gid": , 
        "gr_name": "root", 
        "inode": , 
        "isblk": false, 
        "ischr": false, 
        "isdir": false, 
        "isfifo": false, 
        "isgid": false, 
        "islnk": false, 
        "isreg": true, 
        "issock": false, 
        "isuid": false, 
        "mimetype": "text/plain", 
        "mode": "0644", 
        "mtime": , 
        "nlink": , 
        "path": "/etc/resolv.conf", 
        "pw_name": "root", 
        "readable": true, 
        "rgrp": true, 
        "roth": true, 
        "rusr": true, 
        "size": , 
        "uid": , 
        "version": "1617439728", 
        "wgrp": false, 
        "woth": false, 
        "writeable": true, 
        "wusr": true, 
        "xgrp": false, 
        "xoth": false, 
        "xusr": false
    }
}
           

顯示了 /etc/sysctl.conf 檔案的所有狀态資訊。

get_url 子產品

get_url 子產品可以實作從遠端主機下載下傳指定 URL 到本地,支援 sha256sum 檔案教驗。

例如:

執行指令的結果為:

[root@xdhuxc ~]# ansible Client -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"
 | SUCCESS => {
    "changed": true, 
    "checksum_dest": "69676fdd06a44063439b46a7df1e326eeae98ed4", 
    "checksum_src": "063ec2b31e5fa4d889076b948994ed4934d8ad62", 
    "dest": "/tmp/index.html", 
    "gid": , 
    "group": "root", 
    "md5sum": "b4715e7fb51d60a983e29f3dc6d7a51a", 
    "mode": "0440", 
    "msg": "OK (unknown bytes)", 
    "owner": "root", 
    "size": , 
    "src": "/tmp/tmpXgrOKe", 
    "state": "file", 
    "status_code": , 
    "uid": , 
    "url": "http://www.baidu.com"
}
[root@xdhuxc ~]# ansible Client -m command -a "ls /tmp"
 | SUCCESS | rc= >>
ansible_EsBCHn
index.html                              # 下載下傳下來的檔案                         
           
yum 子產品

yum 子產品用于實作軟體包管理。

例如:

指令執行的結果為:

[[email protected] ~]# ansible Client -m yum -a "name=curl state=latest"
192.168.91.130 | SUCCESS => {
    "changed": true, 
    "msg": "Repository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nRepository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nRepository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nRepository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nRepository epel is listed more than once in the configuration\nRepository epel-debuginfo is listed more than once in the configuration\nRepository epel-source is listed more than once in the configuration\nhttps://mirrors.aliyun.com/centos/7.4.1708/virt/x86_64/kubernetes19/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found\nTrying other mirror.\nTo address this issue please refer to the below knowledge base article \n\nhttps://access.redhat.com/articles/1320623\n\nIf above article doesn't help to resolve this issue please create a bug on https://bugs.centos.org/\n\n", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * epel: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package curl.x86_64 0:7.29.0-42.el7 will be updated\n---> Package curl.x86_64 0:7.29.0-46.el7 will be an update\n--> Processing Dependency: libcurl = 7.29.0-46.el7 for package: curl-7.29.0-46.el7.x86_64\n--> Running transaction check\n---> Package libcurl.x86_64 0:7.29.0-42.el7 will be updated\n---> Package libcurl.x86_64 0:7.29.0-46.el7 will be an update\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package          Arch            Version                   Repository     Size\n================================================================================\nUpdating:\n curl             x86_64          7.29.0-46.el7             base          268 k\nUpdating for dependencies:\n libcurl          x86_64          7.29.0-46.el7             base          220 k\n\nTransaction Summary\n================================================================================\nUpgrade  1 Package (+1 Dependent package)\n\nTotal download size: 488 k\nDownloading packages:\nDelta RPMs disabled because /usr/bin/applydeltarpm not installed.\n--------------------------------------------------------------------------------\nTotal                                              960 kB/s | 488 kB  00:00     \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Updating   : libcurl-7.29.0-46.el7.x86_64                                 1/4 \n  Updating   : curl-7.29.0-46.el7.x86_64                                    2/4 \n  Cleanup    : curl-7.29.0-42.el7.x86_64                                    3/4 \n  Cleanup    : libcurl-7.29.0-42.el7.x86_64                                 4/4 \n  Verifying  : curl-7.29.0-46.el7.x86_64                                    1/4 \n  Verifying  : libcurl-7.29.0-46.el7.x86_64                                 2/4 \n  Verifying  : libcurl-7.29.0-42.el7.x86_64                                 3/4 \n  Verifying  : curl-7.29.0-42.el7.x86_64                                    4/4 \n\nUpdated:\n  curl.x86_64 0:7.29.0-46.el7                                                   \n\nDependency Updated:\n  libcurl.x86_64 0:7.29.0-46.el7                                                \n\nComplete!\n"
    ]
}
[[email protected] ~]# ansible Client -m yum -a "name=curl state=latest"
192.168.91.130 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "All packages providing curl are up to date", 
        ""
    ]
}
           

從 msg 和 results 輸出的資訊可以看到,這就是一個 yum 安裝軟體包的過程。

cron 子產品

cron 子產品用于配置遠端主機 crontab

例如:

運作效果:

[root@xdhuxc ~]# ansible Client -m cron -a "name='check' minute='1' job='ntpdate asia.pool.ntp.org'"
 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "check"
    ]
}
[root@xdhuxc ~]# ansible Client -m command -a "crontab -l"
 | SUCCESS | rc= >>
#Ansible: check
 * * * * ntpdate asia.pool.ntp.org

[root@xdhuxc ~]# 
           
service 子產品

service 子產品可以用來管理遠端主機的系統服務。

例如:

ansible Client -m service -a "name=haproxy state=stopped"
ansible Client -m service -a "name=haproxy state=restarted"
ansible Client -m service -a "name=haproxy state=reloaded"
           

指令執行的結果為:

[[email protected] ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon -- :: CST; 2s ago
 Main PID:  (haproxy-systemd)
   Memory: M
   CGroup: /system.slice/haproxy.service
           ├─ /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
           ├─ /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
           └─ /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

Jun  :: xdhuxc systemd[]: Started HAProxy Load Balancer.
Jun  :: xdhuxc systemd[]: Starting HAProxy Load Balancer...
Jun  :: xdhuxc haproxy-systemd-wrapper[]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
[[email protected] ~]# ansible Client -m service -a "name=haproxy state=stopped"
 | SUCCESS => {
    "changed": true, 
    "name": "haproxy", 
    "state": "stopped", 
    "status": {
        "ActiveEnterTimestamp": "Mon 2018-06-11 23:17:21 CST", 
        "ActiveEnterTimestampMonotonic": "4444399885", 
        "ActiveExitTimestampMonotonic": "0", 
        "ActiveState": "active", 
        "After": "basic.target system.slice network.target systemd-journald.socket syslog.target", 
        "AllowIsolate": "no", 
        "AmbientCapabilities": "0", 
        "AssertResult": "yes", 
        "AssertTimestamp": "Mon 2018-06-11 23:17:21 CST", 
        "AssertTimestampMonotonic": "4444398724", 
        "Before": "shutdown.target", 
        "BlockIOAccounting": "no", 
        "BlockIOWeight": "18446744073709551615", 
        "CPUAccounting": "no", 
        "CPUQuotaPerSecUSec": "infinity", 
        "CPUSchedulingPolicy": "0", 
        "CPUSchedulingPriority": "0", 
        "CPUSchedulingResetOnFork": "no", 
        "CPUShares": "18446744073709551615", 
        "CanIsolate": "no", 
        "CanReload": "yes", 
        "CanStart": "yes", 
        "CanStop": "yes", 
        "CapabilityBoundingSet": "18446744073709551615", 
        "ConditionResult": "yes", 
        "ConditionTimestamp": "Mon 2018-06-11 23:17:21 CST", 
        "ConditionTimestampMonotonic": "4444398724", 
        "Conflicts": "shutdown.target", 
        "ControlGroup": "/system.slice/haproxy.service", 
        "ControlPID": "0", 
        "DefaultDependencies": "yes", 
        "Delegate": "no", 
        "Description": "HAProxy Load Balancer", 
        "DevicePolicy": "auto", 
        "EnvironmentFile": "/etc/sysconfig/haproxy (ignore_errors=no)", 
        "ExecMainCode": "0", 
        "ExecMainExitTimestampMonotonic": "0", 
        "ExecMainPID": "2135", 
        "ExecMainStartTimestamp": "Mon 2018-06-11 23:17:21 CST", 
        "ExecMainStartTimestampMonotonic": "4444399843", 
        "ExecMainStatus": "0", 
        "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -USR2 $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", 
        "ExecStart": "{ path=/usr/sbin/haproxy-systemd-wrapper ; argv[]=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS ; ignore_errors=no ; start_time=[Mon 2018-06-11 23:17:21 CST] ; stop_time=[n/a] ; pid=2135 ; code=(null) ; status=0/0 }", 
        "FailureAction": "none", 
        "FileDescriptorStoreMax": "0", 
        "FragmentPath": "/usr/lib/systemd/system/haproxy.service", 
        "GuessMainPID": "yes", 
        "IOScheduling": "0", 
        "Id": "haproxy.service", 
        "IgnoreOnIsolate": "no", 
        "IgnoreOnSnapshot": "no", 
        "IgnoreSIGPIPE": "yes", 
        "InactiveEnterTimestampMonotonic": "0", 
        "InactiveExitTimestamp": "Mon 2018-06-11 23:17:21 CST", 
        "InactiveExitTimestampMonotonic": "4444399885", 
        "JobTimeoutAction": "none", 
        "JobTimeoutUSec": "0", 
        "KillMode": "mixed", 
        "KillSignal": "15", 
        "LimitAS": "18446744073709551615", 
        "LimitCORE": "18446744073709551615", 
        "LimitCPU": "18446744073709551615", 
        "LimitDATA": "18446744073709551615", 
        "LimitFSIZE": "18446744073709551615", 
        "LimitLOCKS": "18446744073709551615", 
        "LimitMEMLOCK": "65536", 
        "LimitMSGQUEUE": "819200", 
        "LimitNICE": "0", 
        "LimitNOFILE": "4096", 
        "LimitNPROC": "3818", 
        "LimitRSS": "18446744073709551615", 
        "LimitRTPRIO": "0", 
        "LimitRTTIME": "18446744073709551615", 
        "LimitSIGPENDING": "3818", 
        "LimitSTACK": "18446744073709551615", 
        "LoadState": "loaded", 
        "MainPID": "2135", 
        "MemoryAccounting": "no", 
        "MemoryCurrent": "2060288", 
        "MemoryLimit": "18446744073709551615", 
        "MountFlags": "0", 
        "Names": "haproxy.service", 
        "NeedDaemonReload": "no", 
        "Nice": "0", 
        "NoNewPrivileges": "no", 
        "NonBlocking": "no", 
        "NotifyAccess": "none", 
        "OOMScoreAdjust": "0", 
        "OnFailureJobMode": "replace", 
        "PermissionsStartOnly": "no", 
        "PrivateDevices": "no", 
        "PrivateNetwork": "no", 
        "PrivateTmp": "no", 
        "ProtectHome": "no", 
        "ProtectSystem": "no", 
        "RefuseManualStart": "no", 
        "RefuseManualStop": "no", 
        "RemainAfterExit": "no", 
        "Requires": "basic.target", 
        "Restart": "no", 
        "RestartUSec": "100ms", 
        "Result": "success", 
        "RootDirectoryStartOnly": "no", 
        "RuntimeDirectoryMode": "0755", 
        "SameProcessGroup": "no", 
        "SecureBits": "0", 
        "SendSIGHUP": "no", 
        "SendSIGKILL": "yes", 
        "Slice": "system.slice", 
        "StandardError": "inherit", 
        "StandardInput": "null", 
        "StandardOutput": "journal", 
        "StartLimitAction": "none", 
        "StartLimitBurst": "5", 
        "StartLimitInterval": "10000000", 
        "StartupBlockIOWeight": "18446744073709551615", 
        "StartupCPUShares": "18446744073709551615", 
        "StatusErrno": "0", 
        "StopWhenUnneeded": "no", 
        "SubState": "running", 
        "SyslogLevelPrefix": "yes", 
        "SyslogPriority": "30", 
        "SystemCallErrorNumber": "0", 
        "TTYReset": "no", 
        "TTYVHangup": "no", 
        "TTYVTDisallocate": "no", 
        "TasksAccounting": "no", 
        "TasksCurrent": "18446744073709551615", 
        "TasksMax": "18446744073709551615", 
        "TimeoutStartUSec": "1min 30s", 
        "TimeoutStopUSec": "1min 30s", 
        "TimerSlackNSec": "50000", 
        "Transient": "no", 
        "Type": "simple", 
        "UMask": "0022", 
        "UnitFilePreset": "disabled", 
        "UnitFileState": "disabled", 
        "Wants": "system.slice", 
        "WatchdogTimestamp": "Mon 2018-06-11 23:17:21 CST", 
        "WatchdogTimestampMonotonic": "4444399872", 
        "WatchdogUSec": "0"
    }
}
[[email protected] ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Jun  :: xdhuxc systemd[]: Started HAProxy Load Balancer.
Jun  :: xdhuxc systemd[]: Starting HAProxy Load Balancer...
Jun  :: xdhuxc haproxy-systemd-wrapper[]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Jun  :: xdhuxc systemd[]: Stopping HAProxy Load Balancer...
Jun  :: xdhuxc haproxy-systemd-wrapper[]: haproxy-systemd-wrapper: SIGTERM -> 
Jun  :: xdhuxc systemd[]: Stopped HAProxy Load Balancer.
Jun  :: xdhuxc haproxy-systemd-wrapper[]: haproxy-systemd-wrapper: exit, haproxy RC=
[[email protected] ~]# ansible Client -m service -a "name=haproxy state=restarted"
 | SUCCESS => {
    "changed": true, 
    "name": "haproxy", 
    "state": "started", 
    "status": {
        "ActiveEnterTimestampMonotonic": "0", 
        "ActiveExitTimestampMonotonic": "0", 
        "ActiveState": "inactive", 
        "After": "network.target syslog.target systemd-journald.socket basic.target system.slice", 
        "AllowIsolate": "no", 
        "AmbientCapabilities": "0", 
        "AssertResult": "no", 
        "AssertTimestampMonotonic": "0", 
        "Before": "shutdown.target", 
        "BlockIOAccounting": "no", 
        "BlockIOWeight": "18446744073709551615", 
        "CPUAccounting": "no", 
        "CPUQuotaPerSecUSec": "infinity", 
        "CPUSchedulingPolicy": "0", 
        "CPUSchedulingPriority": "0", 
        "CPUSchedulingResetOnFork": "no", 
        "CPUShares": "18446744073709551615", 
        "CanIsolate": "no", 
        "CanReload": "yes", 
        "CanStart": "yes", 
        "CanStop": "yes", 
        "CapabilityBoundingSet": "18446744073709551615", 
        "ConditionResult": "no", 
        "ConditionTimestampMonotonic": "0", 
        "Conflicts": "shutdown.target", 
        "ControlPID": "0", 
        "DefaultDependencies": "yes", 
        "Delegate": "no", 
        "Description": "HAProxy Load Balancer", 
        "DevicePolicy": "auto", 
        "EnvironmentFile": "/etc/sysconfig/haproxy (ignore_errors=no)", 
        "ExecMainCode": "0", 
        "ExecMainExitTimestampMonotonic": "0", 
        "ExecMainPID": "0", 
        "ExecMainStartTimestampMonotonic": "0", 
        "ExecMainStatus": "0", 
        "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -USR2 $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", 
        "ExecStart": "{ path=/usr/sbin/haproxy-systemd-wrapper ; argv[]=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", 
        "FailureAction": "none", 
        "FileDescriptorStoreMax": "0", 
        "FragmentPath": "/usr/lib/systemd/system/haproxy.service", 
        "GuessMainPID": "yes", 
        "IOScheduling": "0", 
        "Id": "haproxy.service", 
        "IgnoreOnIsolate": "no", 
        "IgnoreOnSnapshot": "no", 
        "IgnoreSIGPIPE": "yes", 
        "InactiveEnterTimestampMonotonic": "0", 
        "InactiveExitTimestampMonotonic": "0", 
        "JobTimeoutAction": "none", 
        "JobTimeoutUSec": "0", 
        "KillMode": "mixed", 
        "KillSignal": "15", 
        "LimitAS": "18446744073709551615", 
        "LimitCORE": "18446744073709551615", 
        "LimitCPU": "18446744073709551615", 
        "LimitDATA": "18446744073709551615", 
        "LimitFSIZE": "18446744073709551615", 
        "LimitLOCKS": "18446744073709551615", 
        "LimitMEMLOCK": "65536", 
        "LimitMSGQUEUE": "819200", 
        "LimitNICE": "0", 
        "LimitNOFILE": "4096", 
        "LimitNPROC": "3818", 
        "LimitRSS": "18446744073709551615", 
        "LimitRTPRIO": "0", 
        "LimitRTTIME": "18446744073709551615", 
        "LimitSIGPENDING": "3818", 
        "LimitSTACK": "18446744073709551615", 
        "LoadState": "loaded", 
        "MainPID": "0", 
        "MemoryAccounting": "no", 
        "MemoryCurrent": "18446744073709551615", 
        "MemoryLimit": "18446744073709551615", 
        "MountFlags": "0", 
        "Names": "haproxy.service", 
        "NeedDaemonReload": "no", 
        "Nice": "0", 
        "NoNewPrivileges": "no", 
        "NonBlocking": "no", 
        "NotifyAccess": "none", 
        "OOMScoreAdjust": "0", 
        "OnFailureJobMode": "replace", 
        "PermissionsStartOnly": "no", 
        "PrivateDevices": "no", 
        "PrivateNetwork": "no", 
        "PrivateTmp": "no", 
        "ProtectHome": "no", 
        "ProtectSystem": "no", 
        "RefuseManualStart": "no", 
        "RefuseManualStop": "no", 
        "RemainAfterExit": "no", 
        "Requires": "basic.target", 
        "Restart": "no", 
        "RestartUSec": "100ms", 
        "Result": "success", 
        "RootDirectoryStartOnly": "no", 
        "RuntimeDirectoryMode": "0755", 
        "SameProcessGroup": "no", 
        "SecureBits": "0", 
        "SendSIGHUP": "no", 
        "SendSIGKILL": "yes", 
        "Slice": "system.slice", 
        "StandardError": "inherit", 
        "StandardInput": "null", 
        "StandardOutput": "journal", 
        "StartLimitAction": "none", 
        "StartLimitBurst": "5", 
        "StartLimitInterval": "10000000", 
        "StartupBlockIOWeight": "18446744073709551615", 
        "StartupCPUShares": "18446744073709551615", 
        "StatusErrno": "0", 
        "StopWhenUnneeded": "no", 
        "SubState": "dead", 
        "SyslogLevelPrefix": "yes", 
        "SyslogPriority": "30", 
        "SystemCallErrorNumber": "0", 
        "TTYReset": "no", 
        "TTYVHangup": "no", 
        "TTYVTDisallocate": "no", 
        "TasksAccounting": "no", 
        "TasksCurrent": "18446744073709551615", 
        "TasksMax": "18446744073709551615", 
        "TimeoutStartUSec": "1min 30s", 
        "TimeoutStopUSec": "1min 30s", 
        "TimerSlackNSec": "50000", 
        "Transient": "no", 
        "Type": "simple", 
        "UMask": "0022", 
        "UnitFilePreset": "disabled", 
        "UnitFileState": "disabled", 
        "Wants": "system.slice", 
        "WatchdogTimestampMonotonic": "0", 
        "WatchdogUSec": "0"
    }
}
[[email protected] ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon -- :: CST; 25s ago
 Main PID:  (haproxy-systemd)
   Memory: M
   CGroup: /system.slice/haproxy.service
           ├─ /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
           ├─ /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
           └─ /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

Jun  :: xdhuxc systemd[]: Started HAProxy Load Balancer.
Jun  :: xdhuxc systemd[]: Starting HAProxy Load Balancer...
Jun  :: xdhuxc haproxy-systemd-wrapper[]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
           
user 子產品

user 子產品用來進行遠端主機使用者的管理。

例如:

ansible Client -m user -a "name=wang comment='user wang'"     # 建立使用者 wang
ansible Client -m user -a "name=wang state=absent remove=yes" # 删除使用者 wang
           

執行指令結果如下:

[root@xdhuxc ~]# cat /etc/passwd | grep wang
[root@xdhuxc ~]# ansible Client -m user -a "name=wang comment='user wang'"     # 建立使用者 wang
 | SUCCESS => {
    "changed": true, 
    "comment": "user wang", 
    "create_home": true, 
    "group": , 
    "home": "/home/wang", 
    "name": "wang", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 
}
[root@xdhuxc ~]# cat /etc/passwd | grep wang
wang:x:::user wang:/home/wang:/bin/bash
[root@xdhuxc ~]# ansible Client -m user -a "name=wang state=absent remove=yes"  # 删除使用者 wang
 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "wang", 
    "remove": true, 
    "state": "absent"
}
[root@xdhuxc ~]# cat /etc/passwd | grep wang
[root@xdhuxc ~]# 
           

繼續閱讀