1. Ansible模塊
Ansible模塊特別多,下面主要介紹一些常用的模塊。
[root@CentOS7 ~]# ansible-doc -l | wc -l
1378
1.1 ping模塊
用於檢查指定節點機器是否還能連通。ping模塊並不是基於ICMP協議的。
[root@CentOS7 ~]# ansible all -m ping
192.168.146.111 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.146.112 | SUCCESS => {
"changed": false,
"ping": "pong"
}
1.2 command
默認的模塊。不會通過shell進行處理,因此如$、<、>、&、|、;等特殊符號不會被執行。
常用參數:
- chdir參數:在運行對應命令前,會先進入chdir參數指定的目錄
- creates參數:與removes相反。當指定的文件存在時,跳過這一步,不執行對應命令;當指定的文件不存在時,執行對應的命令。
- removes參數:與creates相反,當指定的文件不存在時,就不執行對應的命令;當指定的文件存在時,就執行對應的命令。
- free_form參數:需要執行的指令(沒有真正的參數爲free_form)
- stdin參數:將命令的stdin直接設置爲指定的值。
- warn參數:如果ansible配置文件中定義了命令警告,如果參數設置爲no/False,將不會警告此命令
例1:列出root用戶根目錄下的文件
[root@CentOS7 ~]# ansible all -m command -a 'chdir=/root ls'
192.168.146.112 | SUCCESS | rc=0 >>
anaconda-ks.cfg
a.txt
server.sh
192.168.146.111 | SUCCESS | rc=0 >>
0a7add1d9996.tar.bz2
anaconda-ks.cfg
a.txt
server.sh
1.3 shell模塊
與command類似,用shell執行命令,但shell模塊支持如$、<、>、&、|、;等特殊符號。
常用參數:
- chdir參數:在運行對應命令前,會先進入chdir參數指定的目錄
- exectualbe參數:切換shell來執行命令,需使用命令的絕對路徑
- creates參數:與removes相反。當指定的文件存在時,跳過這一步,不執行對應命令;當指定的文件不存在時,執行對應的命令。
- removes參數:與creates相反,當指定的文件不存在時,就不執行對應的命令;當指定的文件存在時,就執行對應的命令。
- free_form參數:需要執行的指令(沒有真正的參數爲free_form)
- stdin參數:將命令的stdin直接設置爲指定的值。
例1:輸出遠程主機的主機名
[root@CentOS7 ~]# ansible all -m shell -a 'echo $HOSTNAME'
192.168.146.112 | SUCCESS | rc=0 >>
Server2
192.168.146.111 | SUCCESS | rc=0 >>
Server1
1.4 file模塊
file模塊用於設定文件屬性和創建文件的符號鏈接
常用參數:
- state參數:state=directory:如果目錄不存在,則創建目錄;
state=file:即使文件不存在也不會被創建;
state=link:創建軟連接;
state=hard:創建硬鏈接;
state=touch:如果文件不存在,則會創建一個新的文件;如果文件文件或目錄已經存在,則更新其最後修改時間;
state=absent:刪除目錄、文件或刪除鏈接文件
- path參數:定義文件/目錄的路徑(必選項),也可用dest、name
- owner參數:定義文件/目錄的屬主
- group參數:定義文件/目錄的屬組
- mode參數:定義文件/目錄的權限
- recurse參數:遞歸設置文件的屬性,只對目錄有效
- src參數:要被鏈接的源文件路徑,只應用於state=link的情況
- dest參數:被鏈接到的路徑,只應用於state=link的情況
- force參數:需要在兩種情況下強制創建軟連接。一種是源文件不存在但之後會建立的情況下;另一種是目標鏈接已存在,需要先取消之前的軟連接,然後創建新的軟連接,有兩個選項:yes、no
例1:創建目錄:
[root@CentOS7 ~]# ansible all -m file -a 'path=/tmp/dir1 state=directory'
192.168.146.112 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/dir1",
"size": 6,
"state": "directory",
"uid": 0
}
192.168.146.111 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/dir1",
"size": 6,
"state": "directory",
"uid": 0
}
查看創建的目錄:
[root@CentOS7 ~]# ansible all -m shell -a 'ls -ld /tmp/dir1'
192.168.146.112 | SUCCESS | rc=0 >>
drwxr-xr-x 2 root root 6 8月 15 09:37 /tmp/dir1
192.168.146.111 | SUCCESS | rc=0 >>
drwxr-xr-x 2 root root 6 8月 15 09:37 /tmp/dir1
例2:創建文件:
[root@CentOS7 ~]# ansible all -m file -a 'path=/tmp/file state=touch'
192.168.146.112 | SUCCESS => {
"changed": true,
"dest": "/tmp/file",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
192.168.146.111 | SUCCESS => {
"changed": true,
"dest": "/tmp/file",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
查看創建的文件:
[root@CentOS7 ~]# ansible all -m shell -a 'ls -l /tmp/file '
192.168.146.112 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 0 8月 15 09:34 /tmp/file
192.168.146.111 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 0 8月 15 09:34 /tmp/file
例3:創建軟連接:
[root@CentOS7 ~]# ansible all -m file -a 'src=/etc/passwd dest=passwd.link state=link'
192.168.146.112 | SUCCESS => {
"changed": true,
"dest": "passwd.link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 11,
"src": "/etc/passwd",
"state": "link",
"uid": 0
}
192.168.146.111 | SUCCESS => {
"changed": true,
"dest": "passwd.link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 11,
"src": "/etc/passwd",
"state": "link",
"uid": 0
}
查看創建的軟鏈接
[root@CentOS7 ~]# ansible all -m shell -a 'ls -l passwd.link'
192.168.146.112 | SUCCESS | rc=0 >>
lrwxrwxrwx 1 root root 11 8月 15 09:40 passwd.link -> /etc/passwd
192.168.146.111 | SUCCESS | rc=0 >
lrwxrwxrwx 1 root root 11 8月 15 09:40 passwd.link -> /etc/passwd
刪除軟連接:
[root@CentOS7 ~]# ansible all -m file -a 'dest=passwd.link state=absent'
192.168.146.112 | SUCCESS => {
"changed": true,
"path": "passwd.link",
"state": "absent"
}
192.168.146.111 | SUCCESS => {
"changed": true,
"path": "passwd.link",
"state": "absent"
}
1.5 copy模塊
用來將本地文件複製到被控節點
常用參數:
- src參數:要複製到遠程住居的文件在本機的地址,可以是絕對路徑或者相對路徑。如果路徑是一個目錄,它將遞歸複製。在這種情況下,若使用“/”結尾,則只複製目錄裏的內容;如果沒有使用“/”來結尾,則包含目錄在內的整個內容全部複製,類似於rsync
- dest參數:將源文件複製到遠程主機的絕對路徑。若源文件是個目錄,那麼目標文件也是個目錄。
- mode參數:
- owner參數:
- directory_mode參數:遞歸設定目錄的權限,默認爲系統默認權限
- backup參數:在覆蓋之間將源文件備份,備份文件包含時間信息,有兩個選項:yes、no
- content參數:當不使用src指定拷貝的文件時,可以使用content直接指定文件內容,src與content兩個參數必有其一,否則會報錯。
- backup參數:當遠程主機的目標路徑中已經存在同名文件,並且與ansible主機中的文件內容不同時,是否對遠程主機的文件進行備份,可選值yes/no。當值爲yes時,會先備份遠程主機中的文件,然後再將ansible主機中的文件拷貝到遠程主機。
例1:複製文件到遠程主機
[root@CentOS7 ~]# ansible 192.168.146.112 -m copy -a 'src=/root/test.sh dest=/opt/'
192.168.146.112 | SUCCESS => {
"changed": true,
"checksum": "5810f53a8dd7e48378290a6328ddf0ef8a822199",
"dest": "/opt/test.sh",
"gid": 0,
"group": "root",
"md5sum": "664d0430ee33458602e580520841a2d4",
"mode": "0644",
"owner": "root",
"size": 11,
"src": "/root/.ansible/tmp/ansible-tmp-1565177469.6-64634982784036/source",
"state": "file",
"uid": 0
}
查看複製到遠程主機的文件:
[root@CentOS7 ~]# ansible 192.168.146.112 -m shell -a 'ls -l /opt/test.sh'
192.168.146.112 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 11 8月 8 03:31 /opt/test.sh
1.6 script模塊
可以幫助我們在遠程主機上執行ansible管理主機的腳本,也就是,腳本一直存在ansible管理主機本地,不需要手動拷貝到遠程主機後再執行。
常用參數:
- chdir參數:在運行對應命令前,會先進入chdir參數指定的目錄
- decrypt參數:
- creates參數:與removes相反。當指定的文件存在時,跳過這一步,不執行對應命令;當指定的文件不存在時,執行對應的命令。
- removes參數:與creates相反,當指定的文件不存在時,就不執行對應的命令;當指定的文件存在時,就執行對應的命令。
- free_form參數:需要執行的指令(沒有真正的參數爲free_form)
例1:在遠程主機上執行主控端的腳本
#先在主控端創建腳本文件
[root@CentOS7 ~]# echo "echo $HOSTNAME" > /root/test.sh
[root@CentOS7 ~]# ansible all -m script -a 'creates=/root/test.sh /root/test.sh'
192.168.146.112 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.146.112 closed.\r\n",
"stdout": "CentOS7\r\n",
"stdout_lines": [
"CentOS7"
]
}
192.168.146.111 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.146.111 closed.\r\n",
"stdout": "CentOS7\r\n",
"stdout_lines": [
"CentOS7"
]
}
1.7 fetch模塊
與copy模塊相反。用於拉取遠程主機的文件(只能是單個),並以主機IP地址或者主機名爲目錄,並保留了原來的目錄結構
常用參數:
- src參數:要拉取的遠程主機的文件的地址
- dest參數:主控端的文件的地址
例1:抓取遠程主機日誌文件到主控端
[root@CentOS7 ~]# ansible all -m fetch -a 'src=/var/log/messages dest=/root/log'
192.168.146.112 | SUCCESS => {
"changed": true,
"checksum": "15784c58d4b77d1dbad180b4bea6e8dce7d0b84b",
"dest": "/root/log/192.168.146.112/var/log/messages",
"md5sum": "43b8bd9a24386d359780d7764681b255",
"remote_checksum": "15784c58d4b77d1dbad180b4bea6e8dce7d0b84b",
"remote_md5sum": null
}
192.168.146.111 | SUCCESS => {
"changed": true,
"checksum": "dd608ee3b3db6a19da5fe10b066d32b511976945",
"dest": "/root/log/192.168.146.111/var/log/messages",
"md5sum": "93ef86cb5fe12d370c323685f10c2be5",
"remote_checksum": "dd608ee3b3db6a19da5fe10b066d32b511976945",
"remote_md5sum": null
}
查看拉取過來的文件:
[root@CentOS7 ~]# ls /root/log/
192.168.146.111 192.168.146.112
[root@CentOS7 ~]# tree /root/log
/root/log
├── 192.168.146.111
│ └── var
│ └── log
│ └── messages
├── 192.168.146.112
│ └── var
│ └── log
│ └── messages
1.8 hostname模塊
用來修改遠程主機的主機名
例1:修改遠程主機192.168.146.111的主機名
[root@CentOS7 ~]# ansible 192.168.146.111 -m hostname -a 'name=s1'
192.168.146.111 | SUCCESS => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "s1",
"ansible_hostname": "s1",
"ansible_nodename": "s1"
},
"changed": true,
"name": "s1"
}
查看192.168.146.111的主機名:
[root@CentOS7 ~]# ansible 192.168.146.111 -m shell -a 'cat /etc/hostname'
192.168.146.111 | SUCCESS | rc=0 >>
s1
1.9 yum模塊
可以用來安裝、升級、降級、刪除以及列出包組
常用參數:
- name參數:要進行操作的軟件包的名字,多個包中間用逗號隔開/
- state參數:安裝或卸載軟件,可選值:安裝(present, installed, latest);卸載:(absent, removed)。默認爲present
- list參數:
- config_file參數:yum的匹配文件
- disable_gpg_check參數:關閉gpg_check檢查,有yes/no兩個值
- disablerepo:不啓用某個源
- enablerepo:啓用某個源
- update_cache:相當於yum clean all,更新yum緩存,有yes/no兩個值
列出所有已經安裝的包:
[root@CentOS7 ~]# ansible all -m yum -a 'list=installed'
例1:安裝HTTP軟件:
[root@CentOS7 ~]# ansible all -m yum -a 'name=httpd state=present'
192.168.146.112 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-88.el7.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-88.el7.centos local 2.7 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.7 M\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-88.el7.centos.x86_64 1/1 \n Verifying : httpd-2.4.6-88.el7.centos.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-88.el7.centos \n\nComplete!\n"
]
}
192.168.146.111 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-88.el7.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-88.el7.centos base 2.7 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.7 M\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-88.el7.centos.x86_64 1/1 \n Verifying : httpd-2.4.6-88.el7.centos.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-88.el7.centos \n\nComplete!\n"
]
}
卸載HTTP軟件:
[root@CentOS7 ~]# ansible all -m yum -a 'name=httpd state=absent'
192.168.146.111 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"已加載插件:fastestmirror\n正在解決依賴關係\n--> 正在檢查事務\n---> 軟件包 httpd.x86_64.0.2.4.6-88.el7.centos 將被 刪除\n--> 解決依賴關係完成\n\n依賴關係解決\n\n================================================================================\n Package 架構 版本 源 大小\n================================================================================\n正在刪除:\n httpd x86_64 2.4.6-88.el7.centos @base 9.4 M\n\n事務概要\n================================================================================\n移除 1 軟件包\n\n安裝大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在刪除 : httpd-2.4.6-88.el7.centos.x86_64 1/1 \n 驗證中 : httpd-2.4.6-88.el7.centos.x86_64 1/1 \n\n刪除:\n httpd.x86_64 0:2.4.6-88.el7.centos \n\n完畢!\n"
]
}
192.168.146.112 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"已加載插件:fastestmirror\n正在解決依賴關係\n--> 正在檢查事務\n---> 軟件包 httpd.x86_64.0.2.4.6-88.el7.centos 將被 刪除\n--> 解決依賴關係完成\n\n依賴關係解決\n\n================================================================================\n Package 架構 版本 源 大小\n================================================================================\n正在刪除:\n httpd x86_64 2.4.6-88.el7.centos @local 9.4 M\n\n事務概要\n================================================================================\n移除 1 軟件包\n\n安裝大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在刪除 : httpd-2.4.6-88.el7.centos.x86_64 1/1 \n 驗證中 : httpd-2.4.6-88.el7.centos.x86_64 1/1 \n\n刪除:\n httpd.x86_64 0:2.4.6-88.el7.centos \n\n完畢!\n"
]
}
例2:安裝從互聯網上下載的一個獨立的rpm包:
#得先將rpm包使用copy模塊複製到遠程主機
[root@CentOS7 ~]# ansible all -m copy -a 'src=/root/baidunetdisk_linux_2.0.2.rpm dest=/root/baidunetdisk_linux_2.0.2.rpm'
192.168.146.112 | SUCCESS => {
"changed": true,
"checksum": "d7eaedbb86606c028c9debc50f51e3d69f29b0b9",
"dest": "/root/baidunetdisk_linux_2.0.2.rpm",
"gid": 0,
"group": "root",
"md5sum": "4f6cd066474fbdd2fc65fcc94dab5342",
"mode": "0644",
"owner": "root",
"size": 68673662,
"src": "/root/.ansible/tmp/ansible-tmp-1565836231.89-226498765830831/source",
"state": "file",
"uid": 0
}
192.168.146.111 | SUCCESS => {
"changed": true,
"checksum": "d7eaedbb86606c028c9debc50f51e3d69f29b0b9",
"dest": "/root/baidunetdisk_linux_2.0.2.rpm",
"gid": 0,
"group": "root",
"md5sum": "4f6cd066474fbdd2fc65fcc94dab5342",
"mode": "0644",
"owner": "root",
"size": 68673662,
"src": "/root/.ansible/tmp/ansible-tmp-1565836231.83-15040832689769/source",
"state": "file",
"uid": 0
}
#執行ansible命令安裝
[root@CentOS7 ~]# ansible all -m yum -a 'name=/root/baidunetdisk_linux_2.0.2.rpm state=present'
192.168.146.111 | SUCCESS => {
"changed": true,
"msg": "Non-fatal POSTIN scriptlet failure in rpm package baidunetdisk-2.0.2-1.x86_64\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nExamining /root/baidunetdisk_linux_2.0.2.rpm: baidunetdisk-2.0.2-1.x86_64\nMarking /root/baidunetdisk_linux_2.0.2.rpm to be installed\nResolving Dependencies\n--> Running transaction check\n---> Package baidunetdisk.x86_64 0:2.0.2-1 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n baidunetdisk x86_64 2.0.2-1 /baidunetdisk_linux_2.0.2 299 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal size: 299 M\nInstalled size: 299 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : baidunetdisk-2.0.2-1.x86_64 1/1 \n/var/tmp/rpm-tmp.8HglLi: line 1: update-desktop-database: command not found\nwarning: %post(baidunetdisk-2.0.2-1.x86_64) scriptlet failed, exit status 127\n Verifying : baidunetdisk-2.0.2-1.x86_64 1/1 \n\nInstalled:\n baidunetdisk.x86_64 0:2.0.2-1 \n\nComplete!\n"
]
}
192.168.146.112 | SUCCESS => {
"changed": true,
"msg": "Non-fatal POSTIN scriptlet failure in rpm package baidunetdisk-2.0.2-1.x86_64\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nExamining /root/baidunetdisk_linux_2.0.2.rpm: baidunetdisk-2.0.2-1.x86_64\nMarking /root/baidunetdisk_linux_2.0.2.rpm to be installed\nResolving Dependencies\n--> Running transaction check\n---> Package baidunetdisk.x86_64 0:2.0.2-1 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n baidunetdisk x86_64 2.0.2-1 /baidunetdisk_linux_2.0.2 299 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal size: 299 M\nInstalled size: 299 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : baidunetdisk-2.0.2-1.x86_64 1/1 \n/var/tmp/rpm-tmp.baoocj: line 1: update-desktop-database: command not found\nwarning: %post(baidunetdisk-2.0.2-1.x86_64) scriptlet failed, exit status 127\n Verifying : baidunetdisk-2.0.2-1.x86_64 1/1 \n\nInstalled:\n baidunetdisk.x86_64 0:2.0.2-1 \n\nComplete!\n"
]
}
1.10 service模塊
可以幫助我們管理遠程主機上的服務。
常用參數:
- name參數:指定需要操作的服務名稱(必選項)。
- state參數:指定服務的狀態,此參數的可用值有started/stopped/restarted/reload
- enabled參數:指定是否將服務設置爲開機啓動項,此參數的可選值爲yes/no(或true/false)。
- runlevel參數:指定init的運行級別。
- sleep參數:如果執行了restarted,則在stop和start之間沉睡幾秒種
- arguments參數:給命令行提供一些選項
- pattern參數:定義一個模式,如果通過status指令來查看服務的狀態時,沒有響應,就會通過ps指令在進程種根據該模式進行查找,如果匹配到,則認爲該服務依然在運行
例1:啓動http軟件並設置爲開機自啓動
[root@CentOS7 ~]# ansible 192.168.146.112 -m service -a 'name=httpd state=started enabled=true'
192.168.146.112 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestamp": "四 2019-08-15 10:40:42 CST",
"ActiveEnterTimestampMonotonic": "4278536579",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "active",
"After": "system.slice basic.target network.target tmp.mount remote-fs.target -.mount nss-lookup.target systemd-journald.socket",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "yes",
"AssertTimestamp": "四 2019-08-15 10:40:42 CST",
"AssertTimestampMonotonic": "4278328826",
"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": "四 2019-08-15 10:40:42 CST",
"ConditionTimestampMonotonic": "4278328826",
"Conflicts": "shutdown.target",
"ControlGroup": "/system.slice/httpd.service",
"ControlPID": "0",
"DefaultDependencies": "yes",
"Delegate": "no",
"Description": "The Apache HTTP Server",
"DevicePolicy": "auto",
"Documentation": "man:httpd(8) man:apachectl(8)",
"EnvironmentFile": "/etc/sysconfig/httpd (ignore_errors=no)",
"ExecMainCode": "0",
"ExecMainExitTimestampMonotonic": "0",
"ExecMainPID": "9196",
"ExecMainStartTimestamp": "四 2019-08-15 10:40:42 CST",
"ExecMainStartTimestampMonotonic": "4278329588",
"ExecMainStatus": "0",
"ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[四 2019-08-15 10:40:42 CST] ; stop_time=[n/a] ; pid=9196 ; code=(null) ; status=0/0 }",
"ExecStop": "{ path=/bin/kill ; argv[]=/bin/kill -WINCH ${MAINPID} ; 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/httpd.service",
"GuessMainPID": "yes",
"IOScheduling": "0",
"Id": "httpd.service",
"IgnoreOnIsolate": "no",
"IgnoreOnSnapshot": "no",
"IgnoreSIGPIPE": "yes",
"InactiveEnterTimestampMonotonic": "0",
"InactiveExitTimestamp": "四 2019-08-15 10:40:42 CST",
"InactiveExitTimestampMonotonic": "4278329638",
"JobTimeoutAction": "none",
"JobTimeoutUSec": "0",
"KillMode": "control-group",
"KillSignal": "18",
"LimitAS": "18446744073709551615",
"LimitCORE": "18446744073709551615",
"LimitCPU": "18446744073709551615",
"LimitDATA": "18446744073709551615",
"LimitFSIZE": "18446744073709551615",
"LimitLOCKS": "18446744073709551615",
"LimitMEMLOCK": "65536",
"LimitMSGQUEUE": "819200",
"LimitNICE": "0",
"LimitNOFILE": "4096",
"LimitNPROC": "3795",
"LimitRSS": "18446744073709551615",
"LimitRTPRIO": "0",
"LimitRTTIME": "18446744073709551615",
"LimitSIGPENDING": "3795",
"LimitSTACK": "18446744073709551615",
"LoadState": "loaded",
"MainPID": "9196",
"MemoryAccounting": "no",
"MemoryCurrent": "18446744073709551615",
"MemoryLimit": "18446744073709551615",
"MountFlags": "0",
"Names": "httpd.service",
"NeedDaemonReload": "no",
"Nice": "0",
"NoNewPrivileges": "no",
"NonBlocking": "no",
"NotifyAccess": "main",
"OOMScoreAdjust": "0",
"OnFailureJobMode": "replace",
"PermissionsStartOnly": "no",
"PrivateDevices": "no",
"PrivateNetwork": "no",
"PrivateTmp": "yes",
"ProtectHome": "no",
"ProtectSystem": "no",
"RefuseManualStart": "no",
"RefuseManualStop": "no",
"RemainAfterExit": "no",
"Requires": "basic.target -.mount",
"RequiresMountsFor": "/var/tmp",
"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",
"StatusText": "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec",
"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": "notify",
"UMask": "0022",
"UnitFilePreset": "disabled",
"UnitFileState": "disabled",
"Wants": "system.slice",
"WatchdogTimestamp": "四 2019-08-15 10:40:42 CST",
"WatchdogTimestampMonotonic": "4278536539",
"WatchdogUSec": "0"
}
}
查看http服務是否開機自啓動
[root@CentOS7 ~]# ansible all -m shell -a 'systemctl is-enabled httpd'
192.168.146.112 | SUCCESS | rc=0 >>
enabled
1.11 cron模塊
用來定時任務
常用參數:
- backup參數:
- cron_file參數:替換客戶端該用戶的任務計劃的文件
- name參數:計劃任務名稱
- state參數:指定任務計劃,值有present/absent
- month參數:月(1-12, * , */2)
- weekday參數:周(0-6或1-7, *)
- day參數:日(1-31, * ,*/2)
- hour參數:小時(0-23, * ,*/2)
- minute參數:分鐘(0-59, * ,*/2)
- job參數: 要執行的命令
- disabled參數:禁用/啓用某個定時任務,有true/false(或yes/no)兩個選項
- env參數:
例1:計劃定時任務爲每分鐘告警
[root@CentOS7 ~]# ansible all -m cron -a 'minute=* weekday=* job="/usr/bin/wall Warning" name=warning'
192.168.146.111 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"warning"
]
}
192.168.146.112 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"warning"
]
}
查看定時任務:
[root@CentOS7 ~]# ansible all -m shell -a 'crontab -l'
192.168.146.112 | SUCCESS | rc=0 >>
#Ansible: warning
* * * * * /usr/bin/wall Warning
192.168.146.111 | SUCCESS | rc=0 >>
#Ansible: warning
* * * * * /usr/bin/wall Warning
取消定時任務:
[root@CentOS7 ~]# ansible all -m cron -a 'disabled=true job="/usr/bin/wall Warning" name=warning'
192.168.146.112 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"warning"
]
}
192.168.146.111 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"warning"
]
}
查看定時任務是否被取消:
[root@CentOS7 ~]# ansible all -m shell -a 'crontab -l'
192.168.146.112 | SUCCESS | rc=0 >>
#Ansible: warning
#* * * * * /usr/bin/wall Warning
192.168.146.111 | SUCCESS | rc=0 >>
#Ansible: warning
#* * * * * /usr/bin/wall Warning
1.12 user模塊
用於管理用戶
常用參數
- comment參數:用於指定用戶的註釋信息
- name參數:用於指定要操作的用戶名稱
- password參數:用於指定用戶的密碼(這個密碼不能是明文的密碼,而是一個對明文加密後的字符串)
- home參數:用於指定用戶家目錄
- create_home參數:用於指定是否創建用戶家目錄
- expires參數:用戶指定用戶的過期時間
- uid參數:用於指定用戶的UID號
- group參數:用於指定用戶所在的主組
- groups參數:用於指定用戶所在的附加組
- state參數:用於指定用戶是否存在於遠程主機中,值有present/absent,默認present
- remove參數:是否刪除用戶家目錄。當 state=absent 並且 remove=yes 時,纔會刪除用戶家目錄
- system參數:用於指定是否爲系統賬戶
- shell參數:用於指定用戶的默認shell
例1:創建名爲test,uid爲2222的用戶
[root@CentOS7 ~]# ansible 192.168.146.112 -m user -a 'name=test shell=/bin/bash home=/home/test uid=2222 comment="for test"'
192.168.146.112 | SUCCESS => {
"changed": true,
"comment": "for test",
"createhome": true,
"group": 2222,
"home": "/home/test",
"name": "test",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2222
}
查看用戶是否被創建:
[root@CentOS7 ~]# ansible 192.168.146.112 -m shell -a 'getent passwd test'
192.168.146.112 | SUCCESS | rc=0 >>
test:x:2222:2222:for test:/home/test:/bin/bash
查看用戶家目錄是否被創建:
[root@CentOS7 ~]# ansible 192.168.146.112 -m shell -a 'ls -ld /home/test'
192.168.146.112 | SUCCESS | rc=0 >>
drwx------ 2 test test 62 8月 15 10:57 /home/test
刪除test用戶及其家目錄:
[root@CentOS7 ~]# ansible 192.168.146.112 -m user -a 'name=test state=absent remove=yes'
192.168.146.112 | SUCCESS => {
"changed": true,
"force": false,
"name": "test",
"remove": true,
"state": "absent"
}
1.13 group模塊
用於管理用戶組
常用參數
- gid參數:用於指定用戶組的GID號
- name參數:用於指定要操作的用戶組名稱
- state參數:用於指定用戶組是否存在於遠程主機中,值有present/absent,默認present
- system參數:用於指定是否爲系統用戶組
例1:創建名爲test,gid爲2222的用戶組
[root@CentOS7 ~]# ansible 192.168.146.112 -m group -a 'name=test gid=2222 state=present'192.168.146.112 | SUCCESS => {
"changed": true,
"gid": 2222,
"name": "test",
"state": "present",
"system": false
}
查看用戶組是否被創建:
[root@CentOS7 ~]# ansible 192.168.146.112 -m shell -a 'getent group test'
192.168.146.112 | SUCCESS | rc=0 >>
test:x:2222: