目錄
一、Ansible概述
二、Ansible特點(diǎn)
三、Ansible應(yīng)用
1、使用者
2、Ansible工具集合
3、作用對(duì)象
四、Ansible的搭建
1、實(shí)驗(yàn)環(huán)境
2、環(huán)境準(zhǔn)備
Ansible:
3、創(chuàng)建ssh免密交互登錄
client端環(huán)境準(zhǔn)備
五、Ansible配置
六、Ansible命令
1、ansible
實(shí)驗(yàn)案例:
1.檢查所有主機(jī)是否存活
2.列出Rich組中所有主機(jī)列表
3.批量顯示Rich組中的磁盤(pán)使用情況
2、Ansible-doc
1.列出支持的模塊
2.查詢ping模塊的說(shuō)明信息
3、ansible-playbook
4、ansible-console?
七、Ansible模塊
1.command模塊
實(shí)驗(yàn)案例
2.shell模塊
實(shí)驗(yàn)案例
1、重定向
2、測(cè)試管道符
3.raw模塊
4.copy模塊
5.hostname模塊
6.yum模塊
實(shí)驗(yàn)案例
1.?client端yum安裝bind
7.service模塊
實(shí)驗(yàn)案例
1.啟動(dòng)httpd服務(wù)并設(shè)置為開(kāi)啟自啟動(dòng)
8.User模塊
實(shí)驗(yàn)案例
1.?創(chuàng)建用戶
2.?刪除用戶及家目錄
9.script模塊
一、Ansible概述
???????Ansible是最近非常火的一款開(kāi)源運(yùn)維自動(dòng)化工具,通過(guò)Ansible可以實(shí)現(xiàn)運(yùn)維自動(dòng)化,提高運(yùn)維工程師的工作效率,減少人為失誤,Ansible可以通過(guò)本身集成的非常豐富的模塊實(shí)現(xiàn)各種管理任務(wù),其自帶模塊數(shù)量已超過(guò)上千個(gè),更為重要的是,它的操作非常簡(jiǎn)單,即使新手也比較容易上手,Ansible提供的功能卻非常豐富,在運(yùn)維領(lǐng)域,幾乎可以實(shí)現(xiàn)任何事情。目前屬于RedHat公司產(chǎn)品,官方地址:Ansible is Simple IT Automation。
二、Ansible特點(diǎn)
Ansible自2012年發(fā)布以來(lái),很快在全球流行,其特點(diǎn)表現(xiàn)如下:
- Ansible基于python開(kāi)發(fā),運(yùn)維工程師對(duì)其二次開(kāi)發(fā)相對(duì)容易
- Ansible擁有豐富的內(nèi)置模塊,基本可以滿足一切要求
- 管理模式非常簡(jiǎn)單,一條命令可以影響上千臺(tái)機(jī)器
- 無(wú)客戶端模式設(shè)計(jì),底層基于SSH通信
- Ansible發(fā)布后也陸續(xù)被AWS,Google,Cloud Platfrom,Microsoft?Azure,Cisco,HP,VMware,Twitter等大公司接納并投入使用
三、Ansible應(yīng)用
????????Ansible沒(méi)有客戶端,也不需要在被管理主機(jī)添加任何代理程序,通過(guò)SSH完成底層通信,而SSH在Linux的發(fā)型版本中默認(rèn)已經(jīng)安裝并啟用,而在Windows系統(tǒng)下則依賴于PowerShell,Ansible要求管理端必須是Linux系統(tǒng),在管理節(jié)點(diǎn)通過(guò)應(yīng)用模塊將指令發(fā)送到被管理主機(jī)上,并在執(zhí)行完畢后自動(dòng)刪除產(chǎn)生的臨時(shí)文件,根據(jù)Ansible使用過(guò)程中不同角色,可將其分為三個(gè)部分。
- 使用者如何使用Ansible實(shí)現(xiàn)自動(dòng)化運(yùn)維?
- Ansible的工具集,Ansible可以實(shí)現(xiàn)的功能?
- 作用對(duì)象,Ansible可以影響哪些主機(jī)?
1、使用者
?
如圖所示:Ansible使用者可以采用多種方式和Ansible交互,圖中我們展示了四種方式
- CMDB:CMDB系統(tǒng)存儲(chǔ)和管理著企業(yè)IT架構(gòu)中的各種配置信息,是構(gòu)建ITL項(xiàng)目核心工具,運(yùn)維人員可以組合CMDB和Ansible,通過(guò)CMDB直接下發(fā)指令調(diào)用Ansible工具完成操作者所希望達(dá)到的目標(biāo)。
- PUBLIC/PRIVATE方式,Ansible除了豐富的內(nèi)置模塊外,同時(shí)提供豐富的API語(yǔ)言接口,如PHP,Pythone,PERL等多種流行語(yǔ)言,基于PUBLIC/PRIVATE,Ansible以API調(diào)用的方式運(yùn)行。
- Ad-Hoc命令集,Users直接通過(guò)Ad-Hoc命令集調(diào)用Ansible工具來(lái)完成工作。
- Playbooks:Users預(yù)先編寫(xiě)好Ansible?Playbooks,通過(guò)執(zhí)行Playbooks中預(yù)先編排好的任務(wù)集按序執(zhí)行命令。
2、Ansible工具集合
? ? ? ??
Ansible工具集合了inventory,Moudles,Plugins和API。其中,inventory用來(lái)管理設(shè)備列表,可以通過(guò)分組(不同的業(yè)務(wù))實(shí)現(xiàn),對(duì)組的調(diào)用直接影響組內(nèi)所有的主機(jī);Moudles是各種執(zhí)行模塊,幾乎所有的管理任務(wù)都是通過(guò)模塊來(lái)執(zhí)行的;Plugins提供了各種附加功能;API為編程人員提供了一個(gè)調(diào)用接口,可以做Ansible的二次開(kāi)發(fā)具體表現(xiàn)如下:
- Ansible?Playbook:任務(wù)腳本,編排定義Ansible任務(wù)集的配置文件,由Ansible按序依次執(zhí)行,通常是JSON格式的YML/YAML文件;
- inventory:Ansible管理主機(jī)清單
- Moudle:Ansible執(zhí)行命令功能模塊,多數(shù)為內(nèi)置的核心模塊也可以用戶自定義;
- Plugins:模塊功能的補(bǔ)充,如連接類(lèi)型插件,循環(huán)插件,變量插件,過(guò)濾插件等,該功能不常用。
- API:提供第三方程序調(diào)用的應(yīng)用程序編程接口;
Ansible:該部分圖中表示的,組合inventory,API,Moudles,Plugins可以理解為Ansible
命令工具其核心執(zhí)行工具。
3、作用對(duì)象
? ? ? Ansible的作用對(duì)象不僅僅是Linux和非Linux操作系統(tǒng)的主機(jī),也可以作用于各類(lèi)PUBLIC/PRIVATE,商業(yè)和非商業(yè)設(shè)備的網(wǎng)絡(luò)設(shè)施。
????????使用者使用Ansible或Ansible-playbooks時(shí),在服務(wù)器終端輸入Ansible的Ad-Hoc命令集或playbooks后,Ansible會(huì)遵循預(yù)先定義安排的規(guī)則將Playbooks逐步拆解為Play,再將Play組織成Ansible可以識(shí)別的任務(wù),隨后調(diào)用任務(wù)涉及的所有模板和插件,根據(jù)inventory中自定義的主機(jī)列表通過(guò)SSH將任務(wù)集以臨時(shí)文件或命令的形式傳輸給遠(yuǎn)程客戶端執(zhí)行并返回執(zhí)行結(jié)果,如果是臨時(shí)文件則執(zhí)行完畢后自動(dòng)刪除。
四、Ansible的搭建
????????接下來(lái)我們來(lái)學(xué)習(xí)Ansible的安裝和部署。Ansible的安裝部署非常簡(jiǎn)單,以RPM安裝為例,配置好阿里云的yum源直接安裝就可以了,Ansible的管理端只能是Linux,如Redhat,Debian,CentOS。下面介紹在CentOS7.x上安裝部署Ansible。
1、實(shí)驗(yàn)環(huán)境
操作系統(tǒng) |
IP地址 |
角色 |
主機(jī)名 |
CPU核心 |
CentOS7.5 |
192.168.200.111 |
Ansible |
ansible |
1 |
CentOS7.5 |
192.168.200.112 |
Client |
client1 |
1 |
CentOS7.5 |
192.168.200.113 |
Client |
client2 |
2 |
2、環(huán)境準(zhǔn)備
Ansible:
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# hostname ansible
[root@localhost ~]# bash
YUM方式安裝asible
[root@ansible ~]# rpm -ivh epel-release-latest-7.noarch.rpm
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# ls
backup CentOS-aliyun.repo CentOS-Media.repo epel.repo epel-testing.repo
[root@ansible yum.repos.d]# mv CentOS-aliyun.repo backup/
[root@ansible yum.repos.d]# mv backup/CentOS-Base.repo ./
[root@ansible yum.repos.d]# ls
backup CentOS-Base.repo CentOS-Media.repo epel.repo epel-testing.repo
[root@ansible yum.repos.d]# yum -y install ansible
[root@ansible yum.repos.d]# ansible --version #測(cè)試可以顯示信息表示安裝成功
ansible 2.9.10
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
3、創(chuàng)建ssh免密交互登錄
????????Ansible是通過(guò)SSH協(xié)議對(duì)設(shè)備進(jìn)行管理,而SSH服務(wù)包含兩種認(rèn)證方式,一種是通過(guò)密碼認(rèn)證,另一種是通過(guò)密鑰對(duì)認(rèn)證,密碼方式必須和系統(tǒng)進(jìn)行交互,而密鑰對(duì)是免交互登錄,如果希望通過(guò)Ansible自動(dòng)管理設(shè)備應(yīng)該配置為免交互登錄被管理設(shè)備。
[root@ansible ~]# ssh-keygen -t rsa #生成SSH密鑰對(duì)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
#設(shè)置密鑰對(duì)存放位置默認(rèn)即可
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #設(shè)置密鑰對(duì)的保護(hù)密碼 回車(chē)表示設(shè)置為空
Enter same passphrase again: #再次輸入密碼確認(rèn),上面沒(méi)有下面也不用
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hUqzTeV/+I7jdbIsQBL9IIyQ+Wt/FCTJoHQ4qGZnHkk root@ansible
The key's randomart image is:
+---[RSA 2048]----+
| .o=o= o. |
| .E=o. B++ |
| .. ooo o=oo |
|.o = ..*..o... |
|o + . o.So .o . |
| . o o o |
| . . . . + .|
| . . o= + |
| . .oo+ |
+----[SHA256]-----+
client端環(huán)境準(zhǔn)備
client1
[root@localhost ~]# hostname client1
[root@localhost ~]# bash
[root@client1 ~]# iptables -F
[root@client1 ~]# setenforce 0
[root@client1 ~]# systemctl stop firewalld
client2
[root@localhost ~]# hostname client2
[root@localhost ~]# bash
[root@client2 ~]# iptables -F
[root@client2 ~]# setenforce 0
[root@client2 ~]# systemctl stop firewalld
ansible端發(fā)送公鑰給client
[root@ansible ~]# ssh-copy-id root@192.168.2.222
[root@ansible ~]# ssh-copy-id root@192.168.2.223
[root@ansible ~]# vim /etc/hosts #三臺(tái)機(jī)子都操作
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.222 client1
192.168.2.223 client2
192.168.2.221 ansible
五、Ansible配置
????????inventory是Ansible管理主機(jī)信息的配置文件,相當(dāng)于系統(tǒng)的Hosts文件功能,默認(rèn)存放在/etc/ansible/hosts。在hosts文件中通過(guò)分組來(lái)組織設(shè)備,Ansible通過(guò)inventory來(lái)定義主機(jī)和分組,通過(guò)ansible命令中是用選項(xiàng)-i或者--inventory-file指定inventory。
[root@ansible ~]# cp /etc/ansible/hosts{,.bak} #備份配置文件
[root@ansible ~]# vim /etc/ansible/hosts
[Rich]
192.168.2.222
192.168.2.223
[root@ansible ~]# ansible -i /etc/ansible/hosts Rich -m ping
192.168.2.223 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.2.222 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
????????配置完成后可以針對(duì)hosts定義服務(wù)組件進(jìn)行遠(yuǎn)程操作,也可以針對(duì)組中的指定的某一個(gè)或多個(gè)主機(jī)操作,下面接收如何針對(duì)特定的服務(wù)器操作
1.針對(duì)Rich組中的192.168.2.222主機(jī)操作,通--limit參數(shù)限定主機(jī)的變更
[root@ansible ~]# ansible Rich -m command -a "head -5 /etc/passwd" --limit 192.168.2.222
192.168.2.222 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
2.只對(duì)192.168.2.0網(wǎng)段主機(jī)進(jìn)行操作。通過(guò)通配符限定主機(jī)變更
[root@ansible ~]# ansible 192.168.2.* -m command -a "head -5 /etc/passwd"
192.168.2.223 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
192.168.2.222 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
六、Ansible命令
????????Ansible的維護(hù)命令大多以ansible開(kāi)頭,在終端輸入ansible后連續(xù)按兩次tab鍵,會(huì)補(bǔ)全所有以ansible字母開(kāi)頭的命令,下面介紹Ansible的一些常用命令。
[root@ansible ~]# ansible #連續(xù)按兩次tab鍵
ansible ansible-console ansible-doc-2.7 ansible-playbook ansible-pull-2.7
ansible-2 ansible-console-2 ansible-galaxy ansible-playbook-2 ansible-test
ansible-2.7 ansible-console-2.7 ansible-galaxy-2 ansible-playbook-2.7 ansible-vault
ansible-config ansible-doc ansible-galaxy-2.7 ansible-pull ansible-vault-2
ansible-connection ansible-doc-2 ansible-inventory ansible-pull-2 ansible-vault-2.7
1、ansible
Ansible是生產(chǎn)環(huán)境中使用非常頻繁的命令之一,主要在以下場(chǎng)景應(yīng)用
- 非固化需求
- 臨時(shí)一次性操作
- 二次開(kāi)發(fā)接口調(diào)用
非固化需求是指臨時(shí)性的維護(hù),如查看Rich服務(wù)器組的磁盤(pán)使用情況,復(fù)制一個(gè)文件
到其他機(jī)器等,類(lèi)似這些沒(méi)有規(guī)律的,臨時(shí)需要做的任務(wù),我們稱為非固化需求,臨時(shí)一次性操作
?
語(yǔ)法:ansible?<host-pattern> [options]
可用選項(xiàng)如下
- -v(--verbose):輸出詳細(xì)的執(zhí)行過(guò)程信息,可以得到執(zhí)行過(guò)程所有信息
- -i?PATH(inventory=PATH):指定inventory信息,默認(rèn)為/etc/ansible/hosts
- -f?NUM(--forks=NUM):并發(fā)線程數(shù),默認(rèn)為5個(gè)線程
- --private-key=PRIVATE_KEY_FILE:指定密鑰文件
- -m?NAME,--moudle-name=NAME:指定執(zhí)行時(shí)使用的模塊
- -M DIRECTORY(--module-path=DIRECTORY):指定模塊存放路徑,默認(rèn)為/usr/share/ansible,也可以通過(guò)ANSIBLE_LIBRARY設(shè)定默認(rèn)目錄
- -a?ARGUMENTS(--args=ARGUMENTS):指定模塊參數(shù)
- -u?USERNAME(--user=USERNAME):指定遠(yuǎn)程主機(jī)USERNAME運(yùn)行命令
- -l?subset(--limit=SUBSET):限定運(yùn)行主機(jī)
- --list-hosts:列出符合條件的主機(jī)列表,不執(zhí)行任何命令
實(shí)驗(yàn)案例:
1.檢查所有主機(jī)是否存活
[root@ansible ~]# ansible all -f 5 -m ping
192.168.2.222 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.2.223 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2.列出Rich組中所有主機(jī)列表
[root@ansible ~]# ansible Rich --list-host
hosts (2):
192.168.2.222
192.168.2.223
[root@ansible ~]# ansible Rich --list
hosts (2):
192.168.2.222
192.168.2.223
3.批量顯示Rich組中的磁盤(pán)使用情況
[root@ansible ~]# ansible Rich -m command -a "df -Th"
192.168.200.112 | CHANGED | rc=0 >>
文件系統(tǒng) 類(lèi)型 容量 已用 可用 已用% 掛載點(diǎn)
/dev/mapper/centos-root xfs 50G 4.5G 46G 9% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 12M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sr0 iso9660 4.3G 4.3G 0 100% /media/cdrom
/dev/sda1 xfs 1014M 166M 849M 17% /boot
/dev/mapper/centos-home xfs 146G 37M 146G 1% /home
tmpfs tmpfs 394M 0 394M 0% /run/user/0
192.168.200.113 | CHANGED | rc=0 >>
文件系統(tǒng) 類(lèi)型 容量 已用 可用 已用% 掛載點(diǎn)
dev/mapper/centos-root xfs 50G 4.5G 46G 9% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 12M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sr0 iso9660 4.3G 4.3G 0 100% /media/cdrom
/dev/sda1 xfs 1014M 166M 849M 17% /boot
/dev/mapper/centos-home xfs 146G 37M 146G 1% /home
tmpfs tmpfs 394M 0 394M 0% /run
- Rich需要提前在/etc/ansible/hosts文件中定義組
- Ansible的返回結(jié)果非常友好,一般會(huì)用三種顏色執(zhí)行結(jié)果:紅色,綠色和橘黃色,其中紅色表示執(zhí)行過(guò)程中有異常,橘黃色表示命令執(zhí)行后目標(biāo)有狀態(tài)變化,綠色表示執(zhí)行成功且沒(méi)有對(duì)目標(biāo)機(jī)器做修改
2、Ansible-doc
????????ansible-doc用來(lái)查詢ansible模塊文檔的說(shuō)明,類(lèi)似于man命令,針對(duì)每個(gè)模塊都有詳細(xì)的用法說(shuō)明及應(yīng)用案例介紹
[root@ansible ~]# ansible-doc -l #查看總幫助
[root@ansible ~]# ansible-doc -s shell #查看shell模塊的幫助
[root@ansible ~]# ansible-doc -s raw
語(yǔ)法:ansible-doc?[options] [module……]
實(shí)驗(yàn)案例
1.列出支持的模塊
[root@ansible ~]# ansible-doc -l #敲完會(huì)卡一會(huì)
fortios_router_community_list Configure community lists in Fortinet's FortiOS ...
azure_rm_devtestlab_info Get Azure DevTest Lab facts
ecs_taskdefinition register a task definition in ecs
avi_alertscriptconfig Module for setup of AlertScriptConfig Avi RESTfu...
tower_receive Receive assets from Ansible Tower
…… #省略部分內(nèi)容
2.查詢ping模塊的說(shuō)明信息
[root@ansible ~]# ansible-doc ping #=ansible-doc -s ping
> PING (/usr/lib/python2.7/site-packages/ansible/modules/system/ping.py)
A trivial test module, this module always returns `pong' on successful contact. It
does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify
the ability to login and that a usable Python is configured. This is NOT ICMP ping,
this is just a trivial test module that requires Python on the remote-node. For
Windows targets, use the [win_ping] module instead. For Network targets, use the
[net_ping] module instead.
* This module is maintained by The Ansible Core Team
OPTIONS (= is mandatory):
- data
Data to return for the `ping' return value.
If this parameter is set to `crash', the module will cause an exception.
[Default: pong]
type: str
3、ansible-playbook
????????Ansible-playbook是日常應(yīng)用中使用頻率最高的命令,類(lèi)似于Linux系統(tǒng)中的sh或source命令,用來(lái)執(zhí)行系列任務(wù),其工作機(jī)制是,通過(guò)讀取編寫(xiě)好的playbook文件實(shí)現(xiàn)集中處理任務(wù),ansible-playbook命令后跟yml或者yaml格式的playbook文件,playbook文件存放了要執(zhí)行的任務(wù)代碼,命令使用方式如下
語(yǔ)法:ansible-playbook playbook.yml
ansible-playbook需要之前編譯好、建議寫(xiě)playbook.yml文件的絕對(duì)路徑
4、ansible-console?
????????ansible-console是ansible為用戶提供的一款交互式工具,類(lèi)似于Windows中的cmd以及Linux中的shell,用戶可以在ansible-console虛擬出來(lái)的終端上做像shell一樣使用ansible內(nèi)置的各種命令,這為習(xí)慣于使用shell交互方式的用戶提供了良好的體驗(yàn),在終端輸入ansible-console命令后顯示如下
[root@ansible ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
root@all (2)[f:5]$ cd Rich
root@Rich (2)[f:5]$ list
192.168.2.222
192.168.2.223
root@Rich (2)[f:5]$ exit
????????所有操作與shell類(lèi)似,而且支持Tab鍵補(bǔ)全,按快捷鍵Ctrl+D或Ctrl+C即可退出當(dāng)前的虛擬終端
七、Ansible模塊
1.command模塊
command模塊在遠(yuǎn)程主機(jī)執(zhí)行命令,但是不支持管道,重定向等shell的特征,常用參數(shù)如下(不支持管道,不建議使用)。
- chdir:在遠(yuǎn)程主機(jī)上運(yùn)行命令前要提前進(jìn)入的目錄
- creates:在命令運(yùn)行時(shí)創(chuàng)建一個(gè)文件,如果文件已經(jīng)存在,則不會(huì)創(chuàng)建任務(wù)
- removes:在命令運(yùn)行時(shí)移除一個(gè)文件,如果文件不存在,則不會(huì)執(zhí)行移除任務(wù)
- executable:指明運(yùn)行命令的shell程序
實(shí)驗(yàn)案例
1.在所有主機(jī)上運(yùn)行“l(fā)s./”命令,運(yùn)行前切換到/home目錄。操作如下。
準(zhǔn)備一下環(huán)境:在兩臺(tái)主機(jī)上分別創(chuàng)建Rich用戶否則/home下是空的查看不到效果
[root@ansible ~]# ansible Rich -m command -a "useradd Rich"
192.168.2.223 | CHANGED | rc=0 >>
192.168.2.222 | CHANGED | rc=0 >>
[root@ansible ~]# ansible Rich -m command -a "chdir=/home ls -l"
192.168.2.223 | CHANGED | rc=0 >>
總用量 4
drwx------ 3 Rich Rich 78 8月 13 18:57 Rich
drwx------. 15 test test 4096 6月 22 18:37 test
192.168.2.2222 | CHANGED | rc=0 >>
總用量 4
drwx------ 3 Rich Rich 78 8月 13 18:57 Rich
drwx------. 15 test test 4096 6月 22 18:37 test
[root@ansible ~]# ansible Rich -m command -a "uptime"
192.168.2.223 | CHANGED | rc=0 >>
18:58:34 up 3:15, 3 users, load average: 0.00, 0.01, 0.05
192.168.2.222 | CHANGED | rc=0 >>
18:58:34 up 3:15, 3 users, load average: 0.00, 0.01, 0.05
2.shell模塊
shell模塊在遠(yuǎn)程主機(jī)執(zhí)行命令,相當(dāng)于調(diào)用遠(yuǎn)程主機(jī)的shell進(jìn)程,然后在該shell下打開(kāi)一個(gè)子shell運(yùn)行命令,和command模塊的區(qū)別是它支持shell特征,如管道,重定向等。
實(shí)驗(yàn)案例
1、重定向
[root@ansible ~]# ansible Rich -m shell -a "echo 1111 > 1.txt"
192.168.2.223 | CHANGED | rc=0 >>
192.168.2.222 | CHANGED | rc=0 >>
[root@client1 ~]# ls
1.txt anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 視頻 圖片 文檔 下載 音樂(lè) 桌面
[root@client1 ~]# cat 1.txt
1111
2、測(cè)試管道符
過(guò)濾client端mac地址嚴(yán)格遵守文檔格式 要不會(huì)報(bào)錯(cuò)
[root@ansible ~]# ansible Rich -m shell -a 'ifconfig ens32 | awk "/ether/{print $2}"'
192.168.200.113 | CHANGED | rc=0 >>
ether 00:0c:29:5d:59:b8 txqueuelen 1000 (Ethernet)
92.168.200.112 | CHANGED | rc=0 >>
ether 00:0c:29:46:d7:f2 txqueuelen 1000 (Ethernet)
3.raw模塊
最原始的方式運(yùn)行命令(不依賴python,僅通過(guò)ssh實(shí)現(xiàn))
清除yum緩存
[root@ansible ~]# ansible Rich -m raw -a "yum clean all"
192.168.2.223 | CHANGED | rc=0 >>
已加載插件:fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
正在清理軟件源: c7-media
Cleaning up list of fastest mirrors
Other repos take up 116 M of disk space (use --verbose for details)
Shared connection to 192.168.200.113 closed.
192.168.2.222 | CHANGED | rc=0 >>
已加載插件:fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
正在清理軟件源: c7-media
Cleaning up list of fastest mirrors
Other repos take up 116 M of disk space (use --verbose for details)
Shared connection to 192.168.200.112 closed.
4.copy模塊
copy模塊用于復(fù)制指定主機(jī)文件到遠(yuǎn)程主機(jī)的指定位置,常見(jiàn)參數(shù)如下
- dest:指出復(fù)制文件的目標(biāo)目錄位置,使用絕對(duì)路徑。如果源是目錄,指目標(biāo)也要是目錄,如果目標(biāo)文件已經(jīng)存在會(huì)覆蓋原有內(nèi)容。
- src:指出源文件的路徑,可以使用相對(duì)路徑或絕對(duì)路徑,支持直接指定目錄,如果源是目錄則目標(biāo)也要是目錄
- mode:指出復(fù)制時(shí),目標(biāo)文件的權(quán)限 可選
- owner:指出復(fù)制時(shí),目標(biāo)文件的屬主 可選
- group:指出復(fù)制時(shí),目標(biāo)文件的屬組 可選
- content:指出復(fù)制到目標(biāo)主機(jī)上的內(nèi)容,不能與src一起使用,相當(dāng)于復(fù)制content指明的數(shù)據(jù)到目標(biāo)文件中
特別提示:
參數(shù):backup=yes===>意思是,如果目標(biāo)路徑下,有與我同名但不同內(nèi)容的文件時(shí),在覆蓋前,對(duì)目標(biāo)文件先進(jìn)行備份。
所有被管理端節(jié)點(diǎn)必須安裝libselinux-python包
[root@client1 ~]# rpm -q libselinux-python
libselinux-python-2.5-15.el7.x86_64
?
實(shí)驗(yàn)案例
將Rich組中主機(jī)的/etc/hosts文件拷貝到/tmp下 指定權(quán)限為777?更改屬主為Rich更改屬組為root
[root@ansible ~]# ansible Rich -m copy -a "src=/etc/hosts dest=/tmp mode=777 owner=Rich group=root"
192.168.2.222 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "54fb6627dbaa37721048e4549db3224d",
"mode": "0777",
"owner": "Rich",
"size": 158,
"src": "/root/.ansible/tmp/ansible-tmp-1597371126.82-11091-132452980530176/source",
"state": "file",
"uid": 1001
}
192.168.2.223 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "54fb6627dbaa37721048e4549db3224d",
"mode": "0777",
"owner": "Rich",
"size": 158,
"src": "/root/.ansible/tmp/ansible-tmp-1597371126.84-11093-220512889812301/source",
"state": "file",
"uid": 1001
}
[root@client1 ~]# ls -l /tmp/hosts
-rwxrwxrwx 1 Rich root 158 8月 14 10:12 /tmp/hosts
[root@client2 ~]# ls -l /tmp/hosts
-rwxrwxrwx 1 Rich root 158 8月 14 10:12 /tmp/hosts
5.hostname模塊
hostname模塊用于管理遠(yuǎn)程主機(jī)上的主機(jī)名,常用參數(shù)如下
1.?name:指明主機(jī)名
實(shí)驗(yàn)案例
更改client1(192.168.200.112)的主機(jī)名為Rich
[root@ansible ~]# ansible 192.168.2.222 -m hostname -a "name=Rich"
192.168.2.222 | CHANGED => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "Rich",
"ansible_hostname": "Rich",
"ansible_nodename": "Rich",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "Rich"
}
client1上查看
[root@client1 ~]# hostname
Rich
?注意:更改回來(lái),否則后面如果在測(cè)試主機(jī)名會(huì)報(bào)錯(cuò)
[root@ansible ~]# ansible 192.168.2.222 -m hostname -a "name=client1"
192.168.2.222 | CHANGED => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "client1",
"ansible_hostname": "client1",
"ansible_nodename": "client1",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "client1"
}
6.yum模塊
Yum模塊基于yum機(jī)制,對(duì)遠(yuǎn)程主機(jī)管理程序包,常用參數(shù)如下。
- name:程序包的名稱,可以帶上版本號(hào),如不指定版本號(hào)默認(rèn)安裝為最新版本
- state=present?|?latest?|?absent:指明對(duì)程序包執(zhí)行的操作,present表示安裝程序包,latest表示安裝最新版本的程序包,absent表示卸載程序包
- disablerepo:在用yum安裝時(shí)禁用某個(gè)倉(cāng)庫(kù)的ID
- enablerepo:在用yum安裝時(shí)啟用某個(gè)參考的ID
- conf_file:yum運(yùn)行時(shí)的配置文件而不是使用默認(rèn)的配置文件
- diable_gpg_check=yes?|?no:是否啟用完整性校驗(yàn)功能
實(shí)驗(yàn)案例
注意實(shí)驗(yàn)前要在client端配置yum倉(cāng)庫(kù)
管理員只是發(fā)送yum命令道被管理端,被管理端要存在可用的yum倉(cāng)庫(kù)才可以成功安裝
1.?client端yum安裝bind
[root@ansible ~]# ansible Rich -m yum -a "name=bind state=present"
192.168.2.223 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"bind"
]
},
"msg": "Non-fatal POSTTRANS scriptlet failure in rpm package 32:bind-9.9.4-72.el7.x86_64\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * c7-media: \nResol
ving Dependencies\n--> Running transaction check\n---> Package bind.x86_64 32:9.9.4-72.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n bind x86_64 32:9.9.4-72.el7 c7-media 1.8 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 1.8 M\nInstalled size: 4.5 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 32:bind-9.9.4-72.el7.x86_64 1/1 \nOSError: No such file or directory\nValueError: SELinux policy is not managed or store cannot be accessed.\nwarning: %posttrans(bind-32:9.9.4-72.el7.x86_64) scriptlet failed, exit status 1\n Verifying : 32:bind-9.9.4-72.el7.x86_64 1/1 \n\nInstalled:\n bind.x86_64 32:9.9.4-72.el7 \n\nComplete!\n" ]
}
192.168.2.222 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"bind"
]
},
"msg": "Non-fatal POSTTRANS scriptlet failure in rpm package 32:bind-9.9.4-72.el7.x86_64\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * c7-media: \nResol
ving Dependencies\n--> Running transaction check\n---> Package bind.x86_64 32:9.9.4-72.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n bind x86_64 32:9.9.4-72.el7 c7-media 1.8 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 1.8 M\nInstalled size: 4.5 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 32:bind-9.9.4-72.el7.x86_64 1/1 \nOSError: No such file or directory\nValueError: SELinux policy is not managed or store cannot be accessed.\nwarning: %posttrans(bind-32:9.9.4-72.el7.x86_64) scriptlet failed, exit status 1\n Verifying : 32:bind-9.9.4-72.el7.x86_64 1/1 \n\nInstalled:\n bind.x86_64 32:9.9.4-72.el7 \n\nComplete!\n" ]
}
[root@client1 ~]# rpm -q bind
bind-9.9.4-72.el7.x86_64
7.service模塊
Service模塊為用來(lái)管理遠(yuǎn)程主機(jī)上的服務(wù)的模塊,常見(jiàn)參數(shù)如下:
- name:被管理的服務(wù)名稱
- state=started?|?stopped?|?restarted:動(dòng)作包含啟動(dòng)關(guān)機(jī)或重啟
- enabled=yes?|?no:表示是否設(shè)置該服務(wù)開(kāi)機(jī)自啟動(dòng)
- runlevel:如果設(shè)定了enabled開(kāi)機(jī)自啟動(dòng),則要定義在哪些運(yùn)行目標(biāo)下自啟動(dòng)
實(shí)驗(yàn)案例
1.啟動(dòng)httpd服務(wù)并設(shè)置為開(kāi)啟自啟動(dòng)
client準(zhǔn)備操作 #如果沒(méi)有需要進(jìn)行安裝
[root@client1 ~]# rpm -q httpd
httpd-2.4.6-93.el7.centos.x86_64
[root@client2 ~]# rpm -q httpd
httpd-2.4.6-93.el7.centos.x86_64
[root@ansible ~]# ansible Rich -m service -a "name=httpd state=started enabled=yes"
192.168.200.112 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "nss-lookup.target -.mount remote-fs.target systemd-journald.socket tmp.mount network.target basic
.target system.slice",
…………………………………… #省略部分內(nèi)容
8.User模塊
User模塊用于管理遠(yuǎn)程主機(jī)上的用戶賬戶,常見(jiàn)參數(shù)如下:
- name:必選參數(shù) ?賬號(hào)名稱
- state=present?|?absent:創(chuàng)建賬號(hào)或者刪除賬號(hào),present表示創(chuàng)建,absent表示刪除
- system=yes?|?no:是否為系統(tǒng)賬號(hào)
- uid:用戶UID
- group:用戶的基本組
- groups:用戶的附加組
- shell:默認(rèn)使用的shell
- home:用戶的家目錄
- move_home=yes?|?no:如果設(shè)置的家目錄已經(jīng)存在,是否將已經(jīng)存在的家目錄進(jìn)行移動(dòng)
- password:用戶的密碼,建議使用加密后的字符串
- comment:用戶的注釋信息
- remove=yes?|?no:當(dāng)state=absent時(shí),是否刪除用戶的家目錄
實(shí)驗(yàn)案例
1.?創(chuàng)建用戶
[root@ansible ~]# ansible Rich -m user -a 'name=user1 system=yes uid=502 group=root groups=sshd shell=/sbin/nologin home=/home/user1 password=user1 comment="test user"' #ansible 命令字 對(duì)Rich組進(jìn)行操作 -m 指定模塊為user -a 信息 ‘用戶名為user1 是系統(tǒng)賬號(hào) uid是502 用戶的基本組是root 附加組是sshd shell是/sbin/nologin 家目錄是/home/user1 注釋信息是測(cè)試用戶’
[WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this
module to work properly.
192.168.2.223 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "test user",
"create_home": true,
"group": 0,
"groups": "sshd",
"home": "/home/********",
"name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 502
}
192.168.2.222 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "test user",
"create_home": true,
"group": 0,
"groups": "sshd",
"home": "/home/********",
"name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 502
}
[root@client1 ~]# tail -1 /etc/passwd
user1:x:502:0:test user:/home/user1:/sbin/nologin
[root@client2 ~]# tail -1 /etc/passwd
user1:x:502:0:test user:/home/user1:/sbin/nologin
2.?刪除用戶及家目錄
[root@ansible ~]# ansible Rich -m user -a 'name=user1 state=absent remove=yes'
192.168.2.223 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "user1",
"remove": true,
"state": "absent",
"stderr": "userdel: user1 郵件池 (/var/spool/mail/user1) 未找到\n",
"stderr_lines": [
"userdel: user1 郵件池 (/var/spool/mail/user1) 未找到"
]
}
192.168.2.222 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "user1",
"remove": true,
"state": "absent",
"stderr": "userdel: user1 郵件池 (/var/spool/mail/user1) 未找到\n",
"stderr_lines": [
"userdel: user1 郵件池 (/var/spool/mail/user1) 未找到"
]
}
[root@client1 ~]# tail -1 /etc/passwd
named:x:25:25:Named:/var/named:/bin/false
9.script模塊
script模塊能夠?qū)崿F(xiàn)遠(yuǎn)程服務(wù)器批量運(yùn)行本地的shell腳本文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-596362.html
所有被管理端需要掛載光盤(pán),并創(chuàng)建本地yum倉(cāng)庫(kù)文件文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-596362.html
[root@ansible ~]# vim /opt/file20.sh
#!/bin/bash
touch /tmp/file{1..20}.txt
[root@ansible ~]# ansible Rich -m script -a "/opt/file20.sh"
192.168.200.112 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.200.112 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.200.112 closed."
],
"stdout": "",
"stdout_lines": []
}
192.168.200.113 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.200.113 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.200.113 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@client1 ~]# ls /tmp/
file10.txt file17.txt file4.txt systemd-private-9e4d0685272b4012a7bf002b6e2de6e7-chronyd.service-iv8Fn6
file11.txt file18.txt file5.txt systemd-private-9e4d0685272b4012a7bf002b6e2de6e7-cups.service-btJJf2
file12.txt file19.txt file6.txt systemd-private-9e4d0685272b4012a7bf002b6e2de6e7-httpd.service-w14lQM
file13.txt file1.txt file7.txt vmware-root_8657-1722094600
file14.txt file20.txt file8.txt
file15.txt file2.txt file9.txt
file16.txt file3.txt hosts
到了這里,關(guān)于Ansible自動(dòng)化運(yùn)維工具的認(rèn)識(shí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!