2.ansible中常用模块
2.ansible中常用模塊
test setting
ssh key crypt node{1…3} root,westos
id_rsa ----> ansible -----> lee,root
sudo node{1…3} westos ALL=(ALL) NOPASSWD: ALL
vim test.sh
#!/bin/bash
#!/bin/bash
echo “####################node1###################”
ssh westos@172.25.0.1 “KaTeX parse error: Expected 'EOF', got '#' at position 18: … echo "" echo "#?###############…"
echo “”
echo “##################node3################”
ssh westos@172.25.0.3 "$”
1.ansible實(shí)現(xiàn)管理的方式
Ad-Hoc 利用ansible命令直接完成管理,主要用于臨時(shí)命令使用場(chǎng)景
playbook ansible腳本,主要用于大型項(xiàng)目場(chǎng)景,需要前期的規(guī)劃
2.Ad-Hoc執(zhí)行方式中如何獲得幫助
ansible-doc 顯示模塊幫助的指令
格式
ansible-doc [參數(shù)] [模塊…]
常用參數(shù)
-l 列出可用模塊
-s 顯示指定模塊的playbook片段
3.ansible命令運(yùn)行方式及常用參數(shù)
格式:
ansible 清單 -m 模塊 -a 模塊參數(shù)
常用參數(shù)
--version 顯示版本 -m module 指定模塊,默認(rèn)為command模塊 -v 詳細(xì)過程 -vv -vvv更詳細(xì)過程 --list 顯示主機(jī)列表,也可以用--list-hosts -k 提示輸入ssh連接密碼,默認(rèn)key認(rèn)證 -C 預(yù)執(zhí)行檢測(cè) -T 執(zhí)行命令的超時(shí)時(shí)間,默認(rèn)10s -u 指定遠(yuǎn)程執(zhí)行的用戶 -b 執(zhí)行sudo切換身份操作 -become-user=USERNAME 指定sudo的用戶 -K 提示輸入sudo密碼4.ansible的基本顏色代表信
綠色 執(zhí)行成功但為對(duì)遠(yuǎn)程主機(jī)做任何改變
黃色 執(zhí)行成功并對(duì)遠(yuǎn)程主機(jī)做改變
紅色 執(zhí)行失敗
5.ansible中的常用模塊
1)command
功能: 在遠(yuǎn)程主機(jī)執(zhí)行命令,此模塊為默認(rèn)模塊
常用參數(shù)
chdir 執(zhí)行命令前先進(jìn)入到指定目錄 cmd 運(yùn)行命令指定 creates 如果文件存在將不運(yùn)行 removes 如果文件存在在將運(yùn)行 free_form 在遠(yuǎn)程主機(jī)中執(zhí)行的命令,此參數(shù)不需要加實(shí)例
ansible all -m command -a "useradd lee" -u root -k ansible all -m command -a "userdel -r lee" -u root -k ansible all -m command -a "chdir=/etc cat passwd " -u root -k ansible all -m command -a "chdir=/etc creates=/etc/passwd cat passwd " -u root -k ansible all -m command -a "chdir=/etc removes=/etc/passwd cat passwd " -u root -k注意
Linux中的很多通配符在command模塊中不支持
2)shell
功能:
和command功能類似
常用參數(shù)
實(shí)例
ansible all -m shell -a "executable=sh ps ax | grep $$ " -k
3)script
功能:
在ansible主機(jī)中寫好的腳本在受控主機(jī)中執(zhí)行
實(shí)例
vim /mnt/westos.sh
#!/bin/bash
echo $HOSTNAME
ansible all -m script -a “/mnt/westos.sh” -k
4)copy
功能
從ansible主機(jī)復(fù)制文件到受控主機(jī)
常用參數(shù)
src 源文件 dest 目的地文件 owner 指定目的地文件所有人 group 指定組 mode 指定目的地文件權(quán)限 backup=yes 當(dāng)受控主機(jī)中存在文件時(shí)備份原文件 content 指定文本內(nèi)容直接在受控主機(jī)中生成文件實(shí)例
ansible all -m copy -a “src=/root/westos dest=/mnt/westos owner=lee mode=777 backup=yes”
ansible all -m copy -a “content=‘hello westos\nhello linux\n’ dest=/mnt/westosfile1 owner=lee mode=600”
5)fetch
功能
從受控主機(jī)把文件復(fù)制到ansible主機(jī),但不支持目錄
常用參數(shù)
src 受控主機(jī)的源文件
dest 本機(jī)目錄
實(shí)例
ansible all -m fetch -a “src=/etc/hostname dest=/mnt” -k
6)file
功能
設(shè)置文件的屬性
常用參數(shù)
path 指定文件名稱 state 指定操作狀態(tài) touch 建立 absent 刪除 directory 遞歸 link 建立鏈接 hard mode 設(shè)定權(quán)限 owner 設(shè)定文件用戶 group 設(shè)定文件組 src 源文件 dest 目標(biāo)文件 recurse=yes example ansible all -m file -a 'path=/mnt/test.sh state=touch' ansible all -m file -a 'path=/mnt/test.sh state=absent' ansible all -m file -a 'path=/mnt/westos state=directory' ansible all -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes' ansible all -m file -a 'src=/mnt/file dest=/mnt/westos state=link' ansible all -m file -a 'src=/mnt/file dest=/mnt/westos1 state=hard' ansible all -m file -a 'path=/mnt/file state=touch owner=lee group=westos mode=777'7)unarchive
功能
解壓縮
常用參數(shù)
copy 默認(rèn)為yes 從ansible主機(jī)復(fù)制文件到受控主機(jī) 設(shè)定為no 從受控主機(jī)中尋找src源文件 remote_src 功能同copy且相反 設(shè)定為yes 表示包在受控主機(jī) 設(shè)定為no 表示包在ansible主機(jī) src 包路徑,可以使ansible主機(jī)也可以使受控主機(jī) dest 受控主機(jī)目錄 mode 加壓后文件權(quán)限實(shí)例
ansible all -m unarchive -a ‘src=/mnt/etc.tar.gz dest=/mnt owner=lee’ -k
ansible all -m unarchive -a “src=/opt/etc.tar.gz dest=/mnt copy=no”
8)archive
作用
壓縮
常用參數(shù)
path 打包目錄名稱 dest 聲稱打包文件名稱 format 打包格式 owner 指定文件所屬人 mode 指定文件權(quán)限實(shí)例
ansible all -m archive -a ‘path=/etc dest=/opt/etc.tar.gz format=gz owner=lee mode=700’ -k
hostname
作用
管理主機(jī)名稱
常用參數(shù)
name 指定主機(jī)名稱
實(shí)例
ansbile 172.25.254.100 -m hostname -a ‘name=lee.westos.com’
cron
作用
計(jì)劃任務(wù)
常用參數(shù)
minute 分鐘 hour 小時(shí) day 天 month 月 weekday 周 name 任務(wù)名稱 job 任務(wù)腳本或命令 disabled yes 禁用計(jì)劃任務(wù) no 啟動(dòng)計(jì)劃任務(wù) state absent 刪除計(jì)劃任務(wù)實(shí)例 ansible list1 -m cron -a "job='echo hello' name=test disable=yes" -k ansible list1 -m cron -a "job='echo hello' name=test disabled=yes" -k ansible list1 -m cron -a "job='echo hello' name=test state=absent" -kyum_repository
作用
配置系統(tǒng)軟件倉(cāng)庫(kù)源文件
常用參數(shù)
name 指定倉(cāng)庫(kù)名稱 baseurl 指定源路徑 description 指定倉(cāng)庫(kù)描述 file 指定倉(cāng)庫(kù)文件名稱 enabled 倉(cāng)庫(kù)是否啟用 gpgcheck 倉(cāng)庫(kù)是否檢測(cè)gpgkey state 默認(rèn)值present 建立 absent 為刪除實(shí)例 ansible all -m yum_repository -a "name=AppStream baseurl=http://172.25.254.250/rhel8.2/AppStream description=AppStream_westos gpgcheck=no file=westos_test" -kansible all -m yum_repository -a “name=AppStream file=westos_test state=absent” -k
dnf
作用
管理系統(tǒng)中的dnf倉(cāng)庫(kù)及管理軟件
常用參數(shù)
name 指定包 state 指定動(dòng)作 present 安裝 latest 更新 absent 刪除list 列出指定信息httpd installedallavailabledisable_gpg_check 禁用gpgkey檢測(cè) enablerepo 指定安裝包來(lái)源 disablerepo 禁用安裝包來(lái)源實(shí)例
ansible all -m dnf -a "name=httpd state=latest" ansible all -m dnf -a 'name="httpd,mariadb-server" state=present' ansible all -m dnf -a 'name=httpd state=absent' ansible all -m dnf -a 'name=httpd state=absent autoremove=no' ansible all -m dnf -a 'name=httpd state=present enablerepo=AppStream' ansible all -m dnf -a 'name="*" state=latest' ansible all -m dnf -a 'name=http://172.25.254.250/software/wps-office-xxx.rpm state=present' ansible all -m dnf -a 'name="@Virtual Tools" state=present'service
作用
管理系統(tǒng)服務(wù)狀態(tài)
#常用參數(shù)
name ##指定服務(wù)名稱
state ##指定對(duì)服務(wù)的動(dòng)作
#started
#stoped
#reseted
#reloaded
enabled ##設(shè)定服務(wù)開機(jī)是否啟動(dòng)
#yes開啟啟動(dòng)
#no開機(jī)不啟動(dòng)
實(shí)例
ansible all -m service -a “name=httpd state=started enabled=yes” -k
ansible all -m service -a “name=httpd state=restarted enabled=yes” -k
##user
#作用
模塊可以幫助我們管理遠(yuǎn)程主機(jī)上的用戶,比如創(chuàng)建用戶、修改用戶、刪除用戶、為用戶創(chuàng)建密鑰對(duì)等操作
#常用參數(shù)
name ##必須參數(shù),用于指定要操作的用戶名稱。
group ##指定用戶所在的基本組。
gourps ##指定用戶所在的附加組。
append ##指定添加附加組默認(rèn)值為no
shell ##指定用戶的默認(rèn) shell。
uid ##指定用戶的 uid 號(hào)。
comment ##指定用戶的注釋信息。
state ##用于指定用戶是否存在于遠(yuǎn)程主機(jī)
#present 建立
#absent 刪除
remove ##當(dāng)刪除用戶是刪除用戶家目錄,默認(rèn)值為no
password ##此參數(shù)用于指定用戶的密碼。但密碼為明文,
##可以用openssl password -6 '密碼’生成加密字符
generate_ssh_key ##生成sshkey
##實(shí)例
ansible all -m user -a ‘name=lee’
ansible all -m user -a ‘name=lee state=absent’
ansible all -m user -a ‘name=lee remove=yes state=absent’
ansible all -m user -a ‘name=lee remove=yes group=888’
ansible all -m user -a ‘name=lee group=888 groups=“user1,user2”’
ansible all -m user -a ‘name=lee groups=“user3”’
ansible all -m user -a ‘name=lee groups=“user1,user2” append=yes’
openssl passwd -6 ‘westos’
ansible all -m user -a ‘name=lee password=“666F4OBwqoXAigDV.dn$I2OgEPB3kfyl8CPmdh3Y8vKDqewZKrVMIDPPIt8GKnhs/DW4gZHfxrZX5ziQN7rVjISX7l14KwDQHEd.uprlV/”’
ansible all -m user -a ‘name=lee generate_ssh_key=yes’
##group
#作用
group 模塊可以幫助我們管理遠(yuǎn)程主機(jī)上的組。
#常用參數(shù)
name ##用于指定要操作的組名稱。
state ##用于指定組的狀態(tài)
#present 建立
#absent 刪除
gid ##用于指定組的gid。
##實(shí)例
ansible all -m group -a ‘name=westoslee’
ansible all -m group -a ‘name=westoslee state=absent’
ansible all -m group -a ‘name=westoslee gid=8888’
##lineinfile
path ##指定要操作的文件。
line ##指定文本內(nèi)容。
regexp ##使用正則表達(dá)式匹配對(duì)應(yīng)的行當(dāng)替換文本時(shí)
##如果有多行文本都能被匹配
##則只有最后面被匹配到的那行文本才會(huì)被替換
##當(dāng)刪除文本時(shí),如果有多行文本都能被匹配
##這么這些行都會(huì)被刪除。
state ##當(dāng)想要?jiǎng)h除對(duì)應(yīng)的文本時(shí)需要將state參數(shù)的值設(shè)置為absent
#state的默認(rèn)值為present。
backrefs ##當(dāng)內(nèi)容無(wú)匹配規(guī)則時(shí)不對(duì)文件做任何更改,默認(rèn)值為no
##向后引用regexp變量信息
insertafter ##借助insertafter參數(shù)可以將文本插入到“指定的行”之后
##insertafter參數(shù)的值可以設(shè)置為EOF或者正則表達(dá)式
insertbefore ##借助insertbefore參數(shù)可以將文本插入到“指定的行”之前
#insertbefore參數(shù)的值可以設(shè)置為BOF或者正則表達(dá)式
backup ##是否在修改文件之前對(duì)文件進(jìn)行備份。
create ##當(dāng)要操作的文件并不存在時(shí),是否創(chuàng)建對(duì)應(yīng)的文件。
實(shí)例
ansible all -m lineinfile -a ‘path=/mnt/westos line=“hello westos”’
ansible all -m lineinfile -a ‘path=/mnt/westos regexp="^westos" line=“hello westos” ’
ansible all -m lineinfile -a ‘path=/mnt/westos regexp="^test" line=“westos test”’
ansible all -m lineinfile -a ‘path=/mnt/westos regexp=’^test’ line=“westos test new” backrefs=yes’
ansible all -m lineinfile -a ‘path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes’
ansible all -m lineinfile -a ‘path=/mnt/westos line="###### westos end #####" insertafter=EOF’
ansible all -m lineinfile -a ‘path=/mnt/westos line="###### westos end lee #####" insertafter=“hello”’
ansible all -m lineinfile -a ‘path=/mnt/westos line="###### westos test #####" insertbefore=BOF’
ansible all -m lineinfile -a ‘path=/mnt/westos line="###### westos test lee #####" insertbefore=“hello”’
##replace
#作用
replace 模塊可以根據(jù)我們指定的正則表達(dá)式替換文件中的字符串,文件中所有被匹配到的字符串都會(huì)被替換
#常用參數(shù)
path ##指定要操作的文件
regexp ##指定一個(gè)正則表達(dá)式
#文件中與正則匹配的字符串將會(huì)被替換。
replace ##指定最終要替換成的字符串。
backup ##是否在修改文件之前對(duì)文件進(jìn)行備份,最好設(shè)置為yes。
##實(shí)例
ansible all -m replace -a ‘path=/mnt/westos regexp=“WESTOS” replace=“westos_lee” backup=yes’
#setup#
#作用
setup模塊用于收集遠(yuǎn)程主機(jī)的一些基本信息
#常用參數(shù)
filter ##用于進(jìn)行條件過濾。如果設(shè)置,僅返回匹配過濾條件的信息。
##實(shí)例
ansible all -m setup -k
ansible all -m setup -a “filter=‘a(chǎn)nsible_all_ipv4_addresses’” -k
#debug#
#作用
調(diào)試模塊,用于在調(diào)試中輸出信息
#常用參數(shù):
msg: ##調(diào)試輸出的消息
var: ##將某個(gè)任務(wù)執(zhí)行的輸出作為變量傳遞給debug模塊
##debug會(huì)直接將其打印輸出
verbosity: ##debug的級(jí)別(默認(rèn)是0級(jí),全部顯示)
總結(jié)
以上是生活随笔為你收集整理的2.ansible中常用模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最好的车
- 下一篇: HTTP 笔记与总结(7)HTTP 缓存