ansible安裝及使用1
????????????? 6臺(tái)主機(jī),需要配置主機(jī)名、IP地址、YUM。關(guān)閉SELINUX和防火墻
????????????? control節(jié)點(diǎn)要求:
–??????????? 配置名稱解析,能夠通過(guò)名字訪問(wèn)所有節(jié)點(diǎn)
–??????????? 配置可以通過(guò)ssh到所有節(jié)點(diǎn)免密登陸
–??????????? 拷貝/linux-soft/2/ansible_soft.tar.gz到control,并解壓安裝
# 配置名稱解析
[root@control ~]# echo -e "192.168.4.253\tcontrol" >> /etc/hosts
[root@control ~]# for i in {1..5}
> do
> echo -e "192.168.4.1$i\tnode$i" >> /etc/hosts
> done
[root@control ~]# tail -6 /etc/hosts
192.168.4.253 control
192.168.4.11? node1
192.168.4.12? node2
192.168.4.13? node3
192.168.4.14? node4
192.168.4.15? node5
# 配置免密登陸
[root@control ~]# ssh-keygen?? # 三個(gè)問(wèn)題都直接回車,使用默認(rèn)值
[root@control ~]# for i in node{1..5}?? # 回答yes和密碼
> do
> ssh-copy-id $i
> done
# 裝包
[root@zzgrhel8 ~]# scp /linux-soft/2/ansible_soft.tar.gz 192.168.4.253:/root
[root@control ~]# yum install -y tar
[root@control ~]# tar xf ansible_soft.tar.gz
[root@control ~]# cd ansible_soft/
[root@control ansible_soft]# yum install -y *.rpm
配置ansible管理環(huán)境
????????????? 因?yàn)橐芾淼倪h(yuǎn)程主機(jī)可能不一樣。所以具有相同管理方式的配置放到一個(gè)目錄下。
# 創(chuàng)建ansible工作目錄,目錄名自己定義,不是固定的。
[root@control ~]# mkdir ansible
[root@control ~]# cd ansible
# 創(chuàng)建配置文件。默認(rèn)的配置文件是/etc/ansible/ansible.cfg,但是一般不使用它,而是在工作目錄下創(chuàng)建自己的配置文件
[root@control ansible]# vim ansible.cfg??? # 文件名必須是ansible.cfg
[defaults]
inventory = hosts??? # 管理的主機(jī),配置在當(dāng)前目錄的hosts文件中,hosts名是自定義的。=號(hào)兩邊空格可有可無(wú)。
# 創(chuàng)建主機(jī)清單文件。寫在[]里的是組名,[]下面的是組內(nèi)的主機(jī)名
[root@control ansible]# vim hosts
[test]
node1
[proxy]
node2
[webserver]
node[3:4]???? # node3和node4的簡(jiǎn)化寫法,表示從3到4
[database]
node5
# cluster是組名,自定義的;:children是固定寫法,表示下面的組名是cluster的子組。
[cluster:children]
webserver
database
# 查看被管理的所有的主機(jī)。注意,一定在工作目錄下執(zhí)行命令。
[root@control ansible]# ansible all --list-hosts
? hosts (5):
??? node1
??? node2
??? node3
??? node4
??? node5
# 查看webserver組中所有的主機(jī)
[root@control ansible]# ansible webserver --list-hosts
? hosts (2):
??? node3
??? node4
ansible管理
????????????? ansible進(jìn)行遠(yuǎn)程管理的兩個(gè)方法:
–??????????? adhoc臨時(shí)命令。就是在命令行上執(zhí)行管理命令。
–??????????? playbook劇本。把管理任務(wù)用特定格式寫到文件中。
????????????? 無(wú)論哪種方式,都是通過(guò)模塊加參數(shù)進(jìn)行管理。
adhoc臨時(shí)命令
????????????? 語(yǔ)法:
ansible 主機(jī)或組列表 -m 模塊 -a "參數(shù)"??? # -a是可選的
????????????? 測(cè)試到遠(yuǎn)程主機(jī)的連通性
[root@control ansible]# ansible all -m ping
ansible模塊
????????????? 模塊基本信息查看
# 列出ansible的所有模塊數(shù)量
[root@control ansible]# ansible-doc -l | wc -l
2834
# 列出ansible的所有模塊
[root@control ansible]# ansible-doc -l
# 查看與yum相關(guān)的模塊
[root@control ansible]# ansible-doc -l | grep yum
# 查看yum模塊的使用說(shuō)明,主要查看下方的EXAMPLE示例
[root@control ansible]# ansible-doc yum
????????????? 學(xué)習(xí)模塊,主要知道實(shí)現(xiàn)某種功能,需要哪個(gè)模塊。
????????????? 模塊的使用方式都一樣。主要是查看該模塊有哪些參數(shù)。
command模塊
????????????? ansible默認(rèn)模塊,用于在遠(yuǎn)程主機(jī)上執(zhí)行任意命令
????????????? command不支持shell特性,如管道、重定向。
# 在所有被管主機(jī)上創(chuàng)建目錄/tmp/demo
[root@control ansible]# ansible all -a "mkdir /tmp/demo"
# 查看node1的ip地址
[root@control ansible]# ansible node1 -a "ip a s"
[root@control ansible]# ansible node1 -a "ip a s | head"?? # 報(bào)錯(cuò)
shell模塊
????????????? 與command模塊類似,但是支持shell特性,如管道、重定向。
# 查看node1的ip地址,只顯示前10行
[root@control ansible]# ansible node1 -m shell -a "ip a s | head"
script模塊
????????????? 用于在遠(yuǎn)程主機(jī)上執(zhí)行腳本
# 在控制端創(chuàng)建腳本即可
[root@control ansible]# vim test.sh
#!/bin/bash
yum install -y httpd
systemctl start httpd
# 在test組的主機(jī)上執(zhí)行腳本
[root@control ansible]# ansible test -m script -a "test.sh"
file模塊
????????????? 可以創(chuàng)建文件、目錄、鏈接等,還可以修改權(quán)限、屬性等
????????????? 常用的選項(xiàng):
–??????????? path:指定文件路徑
–??????????? owner:設(shè)置文件所有者
–??????????? group:設(shè)置文件所屬組
–??????????? state:狀態(tài)。touch表示創(chuàng)建文件,directory表示創(chuàng)建目錄,link表示創(chuàng)建軟鏈接,absent表示刪除
–??????????? mode:設(shè)置權(quán)限
–??????????? src:source的簡(jiǎn)寫,源
–??????????? dest:destination的簡(jiǎn)寫,目標(biāo)
# 查看使用幫助
[root@control ansible]# ansible-doc file
... ...
EXAMPLES:
- name: Change file ownership, group and permissions? # 忽略
? file:?????????????????????????? # 模塊名。以下是它的各種參數(shù)
??? path: /etc/foo.conf?????????? # 要修改的文件的路徑
??? owner: foo??????????????????? # 文件所有者
??? group: foo??????????????????? # 文件的所有組
??? mode: '0644'????????????????? # 權(quán)限
... ...
# 根據(jù)上面的example,-m file -a的內(nèi)容就是doc中把各參數(shù)的冒號(hào)換成=號(hào)
# 在test主機(jī)上創(chuàng)建/tmp/file.txt
[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=touch"?? # touch是指如果文件不存在,則創(chuàng)建
# 在test主機(jī)上創(chuàng)建/tmp/demo目錄
[root@control ansible]# ansible test -m file -a "path=/tmp/demo state=directory"
# 將test主機(jī)上/tmp/file.txt的屬主改為sshd,屬組改為adm,權(quán)限改為0777
[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt owner=sshd group=adm mode='0777'"
[root@control ansible]# ansible test -a "ls -l /tmp/file.txt"
# 刪除test主機(jī)上/tmp/file.txt
[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=absent"??? # absent英文缺席的、不存在的
# 刪除test主機(jī)上/tmp/demo
[root@control ansible]# ansible test -m file -a "path=/tmp/demo state=absent"
# 在test主機(jī)上創(chuàng)建/etc/hosts的軟鏈接,目標(biāo)是/tmp/hosts.txt
[root@control ansible]# ansible test -m file -a "src=/etc/hosts dest=/tmp/hosts.txt state=link"
copy模塊
????????????? 用于將文件從控制端拷貝到被控端
????????????? 常用選項(xiàng):
–??????????? src:源??刂贫说奈募窂?/p>
–??????????? dest:目標(biāo)。被控制端的文件路徑
–??????????? content:內(nèi)容。需要寫到文件中的內(nèi)容
[root@control ansible]# echo "AAA" > a3.txt
# 將a3.txt拷貝到test主機(jī)的/root/
[root@control ansible]# ansible test -m copy -a "src=a3.txt dest=/root/"
# 在目標(biāo)主機(jī)上創(chuàng)建/tmp/mytest.txt,內(nèi)容是Hello World
[root@control ansible]# ansible test -m copy -a "content='Hello World' dest=/tmp/mytest.txt"
fetch模塊
????????????? 與copy模塊相反,copy是上傳,fetch是下載
????????????? 常用選項(xiàng):
–??????????? src:源。被控制端的文件路徑
–??????????? dest:目標(biāo)。控制端的文件路徑
# 將test主機(jī)上的/etc/hostname下載到本地用戶的家目錄下
[root@control ansible]# ansible test -m fetch -a "src=/etc/hostname dest=~/"
[root@control ansible]# ls ~/node1/etc/ ??# node1是test組中的主機(jī)
hostname
lineinfile模塊
????????????? 用于確保存目標(biāo)文件中有某一行內(nèi)容
????????????? 常用選項(xiàng):
–??????????? path:待修改的文件路徑
–??????????? line:寫入文件的一行內(nèi)容
–??????????? regexp:正則表達(dá)式,用于查找文件中的內(nèi)容
# test組中的主機(jī),/etc/issue中一定要有一行Hello World。如果該行不存在,則默認(rèn)添加到文件結(jié)尾
[root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='Hello World'"
# test組中的主機(jī),把/etc/issue中有Hello的行,替換成chi le ma
[root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='chi le ma' regexp='Hello'"
replace模塊
????????????? lineinfile會(huì)替換一行,replace可以替換關(guān)鍵詞
????????????? 常用選項(xiàng):
–??????????? path:待修改的文件路徑
–??????????? replace:將正則表達(dá)式查到的內(nèi)容,替換成replace的內(nèi)容
–??????????? regexp:正則表達(dá)式,用于查找文件中的內(nèi)容
# 把test組中主機(jī)上/etc/issue文件中的chi,替換成he
[root@control ansible]# ansible test -m replace -a "path=/etc/issue regexp='chi' replace='he'"
文件操作綜合練習(xí)
????????????? 所有操作均對(duì)test組中的主機(jī)生效
????????????? 在目標(biāo)主機(jī)上創(chuàng)建/tmp/mydemo目錄,屬主和屬組都是adm,權(quán)限為0777
????????????? 將控制端的/etc/hosts文件上傳到目標(biāo)主機(jī)的/tmp/mydemo目錄中,屬主和屬組都是adm,權(quán)限為0600
????????????? 替換目標(biāo)主機(jī)/tmp/mydemo/hosts文件中的node5為server5
????????????? 將目標(biāo)主機(jī)/tmp/mydemo/hosts文件下載到控制端的當(dāng)前目錄
# 在目標(biāo)主機(jī)上創(chuàng)建/tmp/mydemo目錄,屬主和屬組都是adm,權(quán)限為0777
[root@control ansible]# ansible test -m file -a "path=/tmp/mydemo owner=adm group=adm mode='0777' state=directory"
# 將控制端的/etc/hosts文件上傳到目標(biāo)主機(jī)的/tmp/mydemo目錄中,屬主和屬組都是adm,權(quán)限為0600
[root@control ansible]# ansible test -m copy -a "src=/etc/hosts dest=/tmp/mydemo owner=adm group=adm mode='0600'"
# 替換目標(biāo)主機(jī)/tmp/mydemo/hosts文件中的node5為server5
[root@control ansible]# ansible test -m replace -a "path=/tmp/mydemo/hosts regexp='node5' replace='server5'"
# 將目標(biāo)主機(jī)/tmp/mydemo/hosts文件下載到控制端的當(dāng)前目錄。文件將會(huì)保存到控制端當(dāng)前目錄的node1/tmp/mydemo/
[root@control ansible]# ansible test -m fetch -a "src=/tmp/mydemo/hosts dest=."
user模塊
????????????? 實(shí)現(xiàn)linux用戶管理
????????????? 常用選項(xiàng):
–??????????? name:待創(chuàng)建的用戶名
–??????????? uid:用戶ID
–??????????? group:設(shè)置主組
–??????????? groups:設(shè)置附加組
–??????????? home:設(shè)置家目錄
–??????????? password:設(shè)置用戶密碼
–??????????? state:狀態(tài)。present表示創(chuàng)建,它是默認(rèn)選項(xiàng)。absent表示刪除
–??????????? remove:刪除家目錄、郵箱等。值為yes或true都可以。
# 在test組中的主機(jī)上,創(chuàng)建tom用戶
[root@control ansible]# ansible test -m user -a "name=tom"
# 在test組中的主機(jī)上,創(chuàng)建jerry用戶。設(shè)置其uid為1010,主組是adm,附加組是daemon和root,家目錄是/home/jerry
[root@control ansible]# ansible test -m user -a "name=jerry uid=1010 group=adm groups=daemon,root home=/home/jerry"
# 設(shè)置tom的密碼是123456
# {{}}是固定格式,表示執(zhí)行命令。password_hash是函數(shù),sha512是加密算法,則password_hash函數(shù)將會(huì)把123456通過(guò)sha512加密變成tom的密碼
[root@control ansible]# ansible test -m user -a "name=tom password={{'123456'|password_hash('sha512')}}"
# 刪除tom用戶,不刪除家目錄
[root@control ansible]# ansible test -m user -a "name=tom state=absent"
# 刪除jerry用戶,同時(shí)刪除家目錄
[root@control ansible]# ansible test -m user -a "name=jerry state=absent remove=yes"
group模塊
????????????? 創(chuàng)建、刪除組
????????????? 常用選項(xiàng):
–??????????? name:待創(chuàng)建的組名
–??????????? gid:組的ID號(hào)
–??????????? state:present表示創(chuàng)建,它是默認(rèn)選項(xiàng)。absent表示刪除
# 在test組中的主機(jī)上創(chuàng)建名為devops的組
[root@control ansible]# ansible test -m group -a "name=devops"
# 在test組中的主機(jī)上刪除名為devops的組
[root@control ansible]# ansible test -m group -a "name=devops state=absent"
?