一文掌握 Ansible 自動化運維

1一、基本部署
安裝Ansible
#?yum?-y?install?epel-release
#?yum?list?all?*ansible*
#?yum?info?ansible
#?yum?-y?install?ansible
Ansible配置文件
/etc/ansible/ansible.cfg????主配置文件
/etc/ansible/hosts??????????Inventory
/usr/bin/ansible-doc????????幫助文件
/usr/bin/ansible-playbook???指定運行任務(wù)文件
定義Inventory
#?cd?/etc/ansible/
#?cp?hosts{,.bak}
#?>?hosts
#?cat?hosts
[webserver]
127.0.0.1
192.168.10.149
[dbserver]
192.168.10.113
使用秘鑰方式連接
#?ssh-keygen?-t?rsa?
#?ssh-copy-id?-i?/root/.ssh/[email protected]
#?ssh-copy-id?-i?/root/.ssh/[email protected]
#?ssh-copy-id?-i?/root/.ssh/[email protected]
使用幫助
#?ansible-doc?-l????????????????列出ansible所有的模塊
#?ansible-doc?-s?MODULE_NAME????查看指定模塊具體適用
Ansible命令應(yīng)用基礎(chǔ)
語法:ansible [-f forks] [-m module_name] [-a args]
這次命令對哪些主機(jī)生效的
inventory group name
ip
all
-f forks 一次處理多少個主機(jī)
-m module_name 要使用的模塊
-a args 模塊特有的參數(shù)
# ansible 192.168.10.113 -m command -a 'date'
# ansible webserver -m command -a 'date'
# ansible all -m command -a 'date'
2二、常見模塊
command?????命令模塊(默認(rèn)模塊)用于在遠(yuǎn)程主機(jī)執(zhí)行命令;不能使用變量,管道等
#?ansible?all?-a?'date'
cron????????計劃任務(wù)????
???month???指定月份
???minute??指定分鐘
???job?????指定任務(wù)
???day?????表示那一天
???hour????指定小時
???weekday?表示周幾
???state???表示是添加還是刪除
?????? present:安裝
?????? absent:移除
???#?ansible?webserver?-m?cron?-a?'minute="*/10"?job="/bin/echo?hello"?name="test?cron?job"'???#不寫默認(rèn)都是*,每個任務(wù)都必須有一個名字?
???#?ansible?webserver?-a?'crontab?-l'
???#?ansible?webserver?-m?cron?-a?'minute="*/10"?job="/bin/echo?hello"?name="test?cron?job"?state=absent'??#移除任務(wù)
user????用戶賬號管理
???name????用戶名
???uid?????uid
???state???狀態(tài)??
???group???屬于哪個組
???groups??附加組
???home????家目錄
???createhome??是否創(chuàng)建家目錄
???comment?注釋信息
???system??是否是系統(tǒng)用戶
???
???#?ansible?all?-m?user?-a?'name="user1"'
???#?ansible?all?-m?user?-a?'name="user1"?state=absent'
group???組管理
???gid?????gid??????
???name????組名???????????????
???state???狀態(tài)???????????
???system??是否是系統(tǒng)組
???#?ansible?webserver?-m?group?-a?'name=mysql?gid=306?system=yes'
???#?ansible?webserver?-m?user?-a?'name=mysql?uid=306?system=yes?group=mysql'
copy????復(fù)制文件(復(fù)制本地文件到遠(yuǎn)程主機(jī)的指定位置)
???src?????定義本地源文件路徑
???dest????定義遠(yuǎn)程目錄文件路徑(絕對路徑)
???owner???屬主
???group???屬組
???mode????權(quán)限
???content?取代src=,表示直接用此處的信息生成為文件內(nèi)容
???#?yum?-y?install?libselinux-python
???#?ansible?all?-m?copy?-a?'src=/etc/fstab?dest=/tmp/fstab.ansible?owner=root?mode=640'
???#?ansible?all?-m?copy?-a?'content="hello?ansible
Hi?ansible"?dest=/tmp/test.ansible'
file????設(shè)置文件的屬性
???path|dest|name??對那個文件做設(shè)定
???
???創(chuàng)建文件的符號鏈接:
?????? src:????指定源文件
?????? path:???指明符號鏈接文件路徑
???#?ansible?all?-m?file?-a?'owner=mysql?group=mysql?mode=644?path=/tmp/fstab.ansible'
???#?ansible?all?-m?file?-a?'path=/tmp/fstab.link?src=/tmp/fstab.ansible?state=link'
ping????測試指定主機(jī)是否能連接
???#?ansible?all?-m?ping
service?管理服務(wù)運行狀態(tài)
???enabled?是否開機(jī)自動啟動
???name????指定服務(wù)名
???state???指定服務(wù)狀態(tài)
???????started?????啟動服務(wù)
???????stoped??????停止服務(wù)
???????restarted???重啟服務(wù)
???arguments???服務(wù)的參數(shù)
???#?ansible?webserver?-m?service?-a?'enabled=true?name=httpd?state=started'
shell???在遠(yuǎn)程主機(jī)上運行命令
???尤其是用到管道變量等功能的復(fù)雜命令
???#?ansible?all?-m?shell?-a?'echo?devopsman?|?passwd?--stdin?user1'
script??將本地腳本復(fù)制到遠(yuǎn)程主機(jī)并運行之
???#?ansible?all?-m?script?-a?'/tmp/test.sh'
yum?????安裝程序包
???name????程序包名稱(不指定版本就安裝最新的版本latest)
???state???present,latest表示安裝,absent表示卸載
???#?ansible?webserver?-m?yum?-a?'name=httpd'
???#?ansible?all?-m?yum?-a?'name=ntpdate'??#默認(rèn)就是安裝
???#?ansible?all?-m?yum?-a?'name=ntpdate?state=absent'
setup???收集遠(yuǎn)程主機(jī)的facts
???每個被管理節(jié)點在接受并運行管理命令之前,會將自己主機(jī)相關(guān)信息,如操作系統(tǒng)版本,IP地址等報告給遠(yuǎn)程的ansible主機(jī)?
???#?ansible?all?-m?setup
3三、Ansible playbook
組成結(jié)構(gòu):
inventory???????#以下操作應(yīng)用的主機(jī)
modules?????????#調(diào)用哪些模塊做什么樣的操作
ad?hoc?commands?#在這些主機(jī)上運行哪些命令
playbooks???
???tasks???????#任務(wù),即調(diào)用模塊完成的某操作
???variable????#變量
???templates???#模板
???handlers????#處理器,由某事件觸發(fā)執(zhí)行的操作
???roles???????#角色
4四、YAML
4.1 YAML介紹
YAML是一個可讀性高的用來表達(dá)資料序列的格式。YAML參考了其它多種語言,包括:XML、C語言、Python、Perl以及電子郵件格式RFC2822等。ClarkEvans在2001年首次發(fā)表了這種語言,另外Ingy dot Net與Oren Ben-Kiki也是這語言的共同設(shè)計者。
YAML Ain't Markup Language,即YAML不是XML,不過,在開發(fā)這種語言時,YAML的意思其實是:"Yet Another Markup Language"(仍是一種標(biāo)記語言),其特性:
YAML的可讀性好 YAML和腳本語言的交互性好 YAML使用實現(xiàn)語言的數(shù)據(jù)類型 YAML有一個一致的信息模型 YAML易于實現(xiàn) YAML可以基于流來處理 YAML表達(dá)能力強(qiáng),擴(kuò)展性好
更多的內(nèi)容及規(guī)范參見[1]
4.2 YAML語法
YAML的語法和其他高階語言類似,并且可以簡單表達(dá)清單、散列表、標(biāo)量等數(shù)據(jù)結(jié)構(gòu),其結(jié)構(gòu)(structure)通過空格來展示,序列(sequence)里的項用"-"來表示,Map里面的鍵值對用":"分割,下面是一個示例。
name:?john?smith
age:?41
gender:?male
spouse:
???name:jane?smith
???age:37
???gender:?female
children:
???-???name:jimmy?smith
???????age:17
???????gender:?male
???-???name:jenny?smith
???????age:?13
???????gender:?female
YAML文件擴(kuò)展名通常為.yaml,如example.yaml
Docker+K8s+Jenkins 主流技術(shù)全解視頻資料
4.2.1 list
列表的所有元素均使用"-"打頭,例如:
#?A?list?of?testy?fruits
-?Apple
-?Orange
-?Strawberry
-?Mango
4.2.2 dictionary
字典通過key與value進(jìn)行標(biāo)識,例如:
---
#?An?employee?record
name:?Example?Developer
job:?Developer
skill:?Elite
也可以將key:value放置于{}中進(jìn)行表示,例如:
---
#An?exmloyee?record
{name:?Example?Developer,?job:?Developer,?skill:?Elite}
5五、Ansible基礎(chǔ)元素
5.1 變量
5.1.1 變量命名
變量名僅能由字母、數(shù)字和下劃線組成,且只能以字母開頭。
5.1.2 facts
facts是由正在通信的遠(yuǎn)程目標(biāo)主機(jī)發(fā)回的信息,這些信息被保存在ansible變量中。要獲取指定的遠(yuǎn)程主機(jī)所支持的所有facts,可使用如下命令進(jìn)行:
#ansible?hostname?-m?setup
5.1.3 register
把任務(wù)的輸出定義為變量,然后用于其他任務(wù),實例如下:
tasks:
???-?shell:?/usr/bin/foo
?????register:?foo_result
?????ignore_errors:?True
5.1.4 通過命令行傳遞變量
在運行playbook的時候也可以傳遞一些變量供playbook使用,示例如下:
#ansible-playbook?test.yml?--extra-vars?"hosts=www?user=devopsman"
5.1.5 通過roles傳遞變量
當(dāng)給一個主機(jī)應(yīng)用角色的時候可以傳遞變量,然后在角色內(nèi)使用這些變量,示例如下:
-?hosts:?webserver
?roles:
???-?common
???-?{role:?foo_app_instance,?dir:?'/web/htdocs/a.com',?port:?8080}
5.2 Inventory
ansible的主要功用在于批量主機(jī)操作,為了便捷的使用其中的部分主機(jī),可以在inventory file中將其分組命名,默認(rèn)的inventory file為/etc/ansible/hosts
inventory file可以有多個,且也可以通過Dynamic Inventory來動態(tài)生成。
5.2.1 inventory文件格式
inventory文件遵循INI文件風(fēng)格,中括號中的字符為組名。可以將同一個主機(jī)同時歸并到多個不同的組中;此外,當(dāng)如若目標(biāo)主機(jī)使用非默認(rèn)的SSH端口,還可以在主機(jī)名稱之后使用冒號加端口號來表明。
ntp.devopsman.cn
[webserver]
www1.devopsman.cn:2222
www2.devopsman.cn
[dbserver]
db1.devopsman.cn
db2.devopsman.cn
db3.devopsman.cn
如果主機(jī)名遵循相似的命名模式,還可使用列表的方式標(biāo)識個主機(jī),例如:
[webserver]
www[01:50].example.com
[databases]
db-[a:f].example.com
5.2.2 主機(jī)變量
可以在inventory中定義主機(jī)時為其添加主機(jī)變量以便于在playbook中使用,例如:
[webserver]
www1.devopsman.cn http_port=80 maxRequestsPerChild=808
www2.devopsman.cn http_port=8080 maxRequestsPerChild=909
5.2.3 組變量
組變量是指賦予給指定組內(nèi)所有主機(jī)上的在playbook中可用的變量。例如:
[webserver]
www1.devopsman.cn
www2.devopsman.cn
[webserver:vars]
ntp_server=ntp.devopsman.cn
nfs_server=nfs.devopsman.cn
5.2.4 組嵌套
inventory中,組還可以包含其它的組,并且也可以向組中的主機(jī)指定變量。不過,這些變量只能在ansible-playbook中使用,而ansible不支持。例如:
[apache]
httpd1.devopsman.cn
httpd2.devopsman.cn
[nginx]
ngx1.devopsman.cn
ngx2.devopsman.cn
[webserver:children] #固定格式
apache
nginx
[webserver:vars]
ntp_server=ntp.devopsman.cn
5.2.5 inventory參數(shù)
ansible基于ssh連接inventory中指定的遠(yuǎn)程主機(jī)時,還可以通過參數(shù)指定其交互方式,這些參數(shù)如下所示:
ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass
ansible_sudo_pass
ansible_connection
ansible_ssh_private_key_file
ansible_shell_type
ansible_python_interpreter
5.3 條件測試
如果需要根據(jù)變量、facts或此前任務(wù)的執(zhí)行結(jié)果來做為某task執(zhí)行與否的前提時要用到條件測試。
5.3.1 when語句
在task后添加when字句即可使用條件測試;when語句支持jinja2表達(dá)式語句,例如:
tasks:
?-?name:?'shutdown?debian?flavored?system"
???command:?/sbin/shutdown?-h?now
???when:?ansible_os_family?==?"Debian"
when語句中還可以使用jinja2的大多"filter",例如果忽略此前某語句的錯誤并基于其結(jié)果(failed或success)運行后面指定的語句,可使用類似如下形式;
tasks:
?-?command:/bin/false
???register:?result
???ignore_errors:?True
?-?command:?/bin/something
???when:?result|failed
?-?command:?/bin/something_else
???when:?result|success
?-?command:?/bin/still/something_else
???when:?result|skipped
此外,when語句中還可以使用facts或playbook中定義的變量
#?cat?cond.yml?
-?hosts:?all
?remote_user:?root
?vars:
?-?username:?user10
?tasks:
?-?name:?create?{{?username?}}?user
???user:?name={{?username?}}?
???when:?ansible_fqdn?==?"node1.exercise.com"
5.4 迭代
當(dāng)有需要重復(fù)性執(zhí)行的任務(wù)時,可以使用迭代機(jī)制。其使用格式為將需要迭代的內(nèi)容定義為item變量引用,并通過with_items語句來指明迭代的元素列表即可。例如:
-?name:?add?server?user
?user:?name={{?item?}}?state=persent?groups=wheel
?with_items:
???-?testuser1
???-?testuser2
上面語句的功能等同于下面的語句:
-?name:?add?user?testuser1
?user:?name=testuser1?state=present?group=wheel
-?name:?add?user?testuser2
?user:?name=testuser2?state=present?group=wheel
事實上,with_items中可以使用元素還可為hashes,例如:
-?name:?add?several?users
?user:?name={{?item.name}}?state=present?groups={{?item.groups?}}
?with_items:
???-?{?name:?'testuser1',?groups:?'wheel'}
???-?{?name:?'testuser2',?groups:?'root'}
Ansible的循環(huán)機(jī)制還有更多的高級功能,具體請參考官方文檔[2]
6六、模板示例:
#?grep?'{{'?conf/httpd.conf?
MaxClients???????{{?maxClients?}}
Listen?{{?httpd_port?}}
#?cat?/etc/ansible/hosts
[webserver]
127.0.0.1?httpd_port=80?maxClients=100
192.168.10.149?httpd_port=8080?maxClients=200
#?cat?apache.yml?
-?hosts:?webserver
?remote_user:?root
?vars:
?-?package:?httpd
?-?service:?httpd
?tasks:
?-?name:?install?httpd?package
???yum:?name={{?package?}}?state=latest
?-?name:?install?configuration?file?for?httpd
???template:?src=/root/conf/httpd.conf?dest=/etc/httpd/conf/httpd.conf
???notify:?
???-?restart?httpd
?-?name:?start?httpd?service
???service:?enabled=true?name={{?service?}}?state=started
?
?handlers:
?-?name:?restart?httpd
???service:?name=httpd?state=restarted
7七、Ansible playbooks
playbook是由一個或多個"play"組成的列表。play的主要功能在于將事先歸并為一組的主機(jī)裝扮成事先通過ansible中的task定義好的角色。從根本上來講,所有task無非是調(diào)用ansible的一個module。將多個play組織在一個playbook中,即可以讓他們連同起來按事先編排的機(jī)制同唱一臺大戲。下面是一個簡單示例。
-?hosts:?webserver
?vars:
???http_port:?80
???max_clients:?256
?remote_user:?root
?tasks:
?-?name:?ensure?apache?is?at?the?latest?version
???yum:?name=httpd?state=latest
?-?name:?ensure?apache?is?running
???service:?name=httpd?state=started
?handlers:
???-?name:?restart?apache
?????service:?name=httpd?state=restarted
7.1 playbook基礎(chǔ)組件
7.1.1 Hosts和Users
playbook中的每一個play的目的都是為了讓某個或某些主機(jī)以某個指定的用戶身份執(zhí)行任務(wù)。hosts用于指定要執(zhí)行指定任務(wù)的主機(jī),其可以使一個或多個由冒號分隔主機(jī)組;remote_user則用于指定遠(yuǎn)程主機(jī)的執(zhí)行任務(wù)的用戶,如上面的實例中的
-?hosts:?webserver
?remote_user:?root
不過,remote_user也可用于各task中,也可以通過指定其通過sudo的方式在遠(yuǎn)程主機(jī)上執(zhí)行任務(wù),其可用于play全局或其任務(wù);此外,甚至可以在sudo時使用sudo_user指定sudo時切換的用戶。
-?hosts:?webserver
?remote_user:?devopsman
?tasks:
??-?name:?test?connection
????ping:
????remote_user:?devopsman
????sudo:?yes
7.1.2 任務(wù)列表和action
play的主題部分是task list。task list中的各任務(wù)按次序逐個在hosts中指定的所有主機(jī)上執(zhí)行,即在所有主機(jī)上完成第一個任務(wù)后再開始第二個。在運行自上而下某playbook時,如果中途發(fā)生錯誤,所有已執(zhí)行任務(wù)都可能回滾,在更正playbook后重新執(zhí)行一次即可。
taks的目的是使用指定的參數(shù)執(zhí)行模塊,而在模塊參數(shù)中可以使用變量。模塊執(zhí)行是冪等的。這意味著多次執(zhí)行是安全的,因為其結(jié)果均一致。
每個task都應(yīng)該有其name,用于playbook的執(zhí)行結(jié)果輸出,建議其內(nèi)容盡可能清晰地描述任務(wù)執(zhí)行步驟,如果為提供name,則action的結(jié)果將用于輸出。
定義task可以使用"action: module options"或”module:options“的格式推薦使用后者以實現(xiàn)向后兼容。如果action一行的內(nèi)容過多,也中使用在行首使用幾個空白字符進(jìn)行換行。
tasks:
?-?name:make?sure?apache?is?running
???service:?name=httpd?state=started
tasks:
?-?name:?run?this?command?and?ignore?the?result
???shell:?/usr/bin/somecommand?||?/bin/true
在眾多的模塊中,只有command和shell模塊僅需要給定一個列表而無需使用"key=value"格式,例如:
tasks:
?-?name:?disable?selinux
???command:?/sbin/setenforce?0
如果命令或腳本的退出碼不為零,可以使用如下方式替代:
或者使用ignore_errors來忽略錯誤信息:
tasks:
?-?name:?run?this?command?and?ignore?the?result
???shell:?/usr/bin/somecommand
???ignore_errors:?True
7.1.3handlers
用于當(dāng)關(guān)注的資源發(fā)生變化時采取一定的操作。
"notify"這個action可用于在每個play的最后被觸發(fā),這樣可以避免多次有改變發(fā)生時每次都執(zhí)行執(zhí)行的操作,取而代之,僅在所有的變化發(fā)生完成后一次性地執(zhí)行指定操作,在notify中列出的操作稱為handlers,也即notify中調(diào)用handlers中定義的操作。
-?name:?template?configuration?file
?template:?src=template.j2?dest=/etc/foo.conf
?notify:
???-?restart?memcached
???-?restart?apache
handlers是task列表,這些task與前述的task并沒有本質(zhì)上的不同。
handlers:
?-?name:?restart?memcached
???service:?name=memcached?state=restarted
?-?name:?restart?apache
???service:?name=apache?state=restarted
簡單示例1:
#?cat?nginx.yml?
-?hosts:?webserver
?remote_user:?root
?tasks:
?-?name:?create?nginxn?group
???group:?name=nginx?system=yes?gid=208
?-?name:?create?nginx?user
???user:?name=nginx?uid=208?group=nginx?system=yes
-?hosts:?dbserver
?remote_user:?root
?tasks:
?-?name:?copy?file?to?dbserver
???copy:?src=/etc/inittab?dest=/tmp/inittab.ans
???
#?ansible-playbook?nginx.yml
簡單示例2:
#?cat?apache.yml?
-?hosts:?webserver
?remote_user:?root
?tasks:
?-?name:?install?httpd?package
???yum:?name=httpd?state=latest
?-?name:?install?configuration?file?for?httpd
???copy:?src=/root/conf/httpd.conf?dest=/etc/httpd/conf/httpd.conf
?-?name:?start?httpd?service
???service:?enabled=true?name=httpd?state=started
#?ansible-playbook?apache.yml
handlers 示例:
#?cat?apache.yml?
-?hosts:?webserver
?remote_user:?root
?tasks:
?-?name:?install?httpd?package
???yum:?name=httpd?state=latest
?-?name:?install?configuration?file?for?httpd
???copy:?src=/root/conf/httpd.conf?dest=/etc/httpd/conf/httpd.conf
???notify:?
???-?restart?httpd
?-?name:?start?httpd?service
???service:?enabled=true?name=httpd?state=started
?
?handlers:
?-?name:?restart?httpd
???service:?name=httpd?state=restarted
#??ansible-playbook?apache.yml
variable 示例1:
#?cat?apache.yml?
-?hosts:?webserver
?remote_user:?root
?vars:
?-?package:?httpd
?-?service:?httpd
?tasks:
?-?name:?install?httpd?package
???yum:?name={{?package?}}?state=latest
?-?name:?install?configuration?file?for?httpd
???copy:?src=/root/conf/httpd.conf?dest=/etc/httpd/conf/httpd.conf
???notify:?
???-?restart?httpd
?-?name:?start?httpd?service
???service:?enabled=true?name={{?service?}}?state=started
?
?handlers:
?-?name:?restart?httpd
???service:?name=httpd?state=restarted
variable 示例2:(在playbook中可以使用所有的變量)
#?cat?facts.yml?
-?hosts:?webserver
?remote_user:?root
?tasks:
?-?name:?copy?file
???copy:?content="{{?ansible_all_ipv4_addresses?}}?"?dest=/tmp/vars.ans
8八、roles
ansible自1.2版本引入的新特性,用于層次性、結(jié)構(gòu)化地組織playbook。roles能夠根據(jù)層次型結(jié)構(gòu)自動轉(zhuǎn)載變量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。簡單來講,roles就是通過分別將變量、文件、任務(wù)、模板以及處理器放置于單獨的目錄中,并可以便捷地include他們的一種機(jī)制。角色一般用于基于主機(jī)構(gòu)建服務(wù)的場景中,但也可以使用于構(gòu)建守護(hù)進(jìn)程的場景中
Docker+K8s+Jenkins 主流技術(shù)全解視頻資料
一個roles的案例如下所示:
site.yml
webserver.yml
fooserver.yml
roles/
???common/
???????files/
???????templates/
???????tasks/
???????handlers/
???????vars/
???????meta/
???webserver/
???????files/
???????templates/
???????tasks/
???????handlers/
???????vars/
???????meta/
而在playbook中,可以這樣使用role:
-?hosts:?webserver
?roles:
???-?common??
???-?webserver
也可以向roles傳遞參數(shù),例如:
-?hosts:?webserver
?roles:
???-?common
???-?{?role:?foo_app_instance,?dir:'/opt/a',port:5000}
???-?{?role:?foo_app_instance,?dir:'/opt/b',port:5001}
甚至也可以條件式地使用roles,例如:
-?hosts:webserver
?roles:
???-?{?role:?some_role,?when:?"ansible_so_family?==?'RedHat"?}
8.1 創(chuàng)建role的步驟
創(chuàng)建以roles命名的目錄: 在roles目錄中分別創(chuàng)建以各角色命名的目錄,如webserver等 在每個角色命名的目錄中分別創(chuàng)建files、handlers、meta、tasks、templates和vars目錄;用不到的目錄可以創(chuàng)建為空目錄,也可以不創(chuàng)建 在playbook文件中,調(diào)用各角色
8.2 role內(nèi)各目錄中可應(yīng)用的文件
task目錄:至少應(yīng)該包含一個為main.yml的文件,其定義了此角色的任務(wù)列表;此文件可以使用include包含其它的位于此目錄中的task文件; file目錄:存放由copy或script等模板塊調(diào)用的文件; template目錄:template模塊會自動在此目錄中尋找jinja2模板文件; handlers目錄:此目錄中應(yīng)當(dāng)包含一個main.yml文件,用于定義此角色用到的各handlers,在handler中使用inclnude包含的其它的handlers文件也應(yīng)該位于此目錄中; vars目錄:應(yīng)當(dāng)包含一個main.yml文件,用于定義此角色用到的變量 meta目錄:應(yīng)當(dāng)包含一個main.yml文件,用于定義此角色的特殊設(shè)定及其依賴關(guān)系;ansible1.3及其以后的版本才支持; default目錄:應(yīng)當(dāng)包含一個main.yml文件,用于為當(dāng)前角色設(shè)定默認(rèn)變量時使用此目錄;
#?mkdir?-pv?ansible_playbooks/roles/{webserver,dbserver}/{tasks,files,templates,meta,handlers,vars}?
#?cp?/etc/httpd/conf/httpd.conf?files/??
#?pwd
/root/ansible_playbooks/roles/webserver?
#?cat?tasks/main.yml?
-?name:?install?httpd?package
?yum:?name=httpd?state=present
-?name:?install?configuretion?file
?copy:?src=httpd.conf?dest=/etc/httpd/conf/httpd.conf
?tags:
?-?conf
?notify:
?-?restart?httpd
-?name:?start?httpd
?service:?name=httpd?state=started
#?cat?handlers/main.yml?
-?name:?restart?httpd
?service:?name=httpd?state=restarted
???
#?pwd;ls
/root/ansible_playbooks
roles??site.yml?
#?cat?site.yml?
-?hosts:?webserver
?remote_user:?root
?roles:
?-?webserver
#?ansible-playbook?site.yml
9九、Tags
tags用于讓用戶選擇運行或跳過playbook中的部分代碼。ansible具有冪等性,因此會自動跳過沒有變化的部分,即便如此,有些代碼為測試其確實沒有發(fā)生變化的時間依然會非常的長。此時,如果確信其沒有變化,就可以通過tags跳過此些代碼片段。
tags:在playbook可以為某個或某些任務(wù)定義一個"標(biāo)簽",在執(zhí)行此playbook時,通過為ansible-playbook命令使用--tags選項能耐實現(xiàn)僅運行指定的tasks而非所有的;
#?cat?apache.yml?
-?hosts:?webserver
?remote_user:?root
?vars:
?-?package:?httpd
?-?service:?httpd
?tasks:
?-?name:?install?httpd?package
???yum:?name={{?package?}}?state=latest
?-?name:?install?configuration?file?for?httpd
???template:?src=/root/conf/httpd.conf?dest=/etc/httpd/conf/httpd.conf
???tags:
???-?conf
???notify:?
???-?restart?httpd
?-?name:?start?httpd?service
???service:?enabled=true?name={{?service?}}?state=started
?
?handlers:
?-?name:?restart?httpd
???service:?name=httpd?state=restarted
#?ansible-playbook?apache.yml?--tags='conf'
特殊tags:always #無論如何都會運行
作者:?kangvcar 來源: https://my.oschina.net/kangvcar/blog/1830155 10T 技術(shù)資源大放送!包括但不限于:Linux、虛擬化、容器、云計算、網(wǎng)絡(luò)、Python、Go 等。在公眾號內(nèi)回復(fù)「10T」,即可免費獲!
推薦閱讀:
shell編程100例(附PDF下載)
IPv6技術(shù)白皮書(附PDF下載)
Linux主流發(fā)行版本配置IP總結(jié)(Ubuntu、CentOS、Redhat、Suse)
批量安裝Windows系統(tǒng)
無人值守批量安裝服務(wù)器
運維必備的《網(wǎng)絡(luò)端口大全》,看這一份就夠了。
收藏:服務(wù)器和存儲知識入門
什么叫SSH?原理詳解,看這一篇就夠了!
Nginx面試40問(收藏吃灰)
20 個 Linux 服務(wù)器性能調(diào)優(yōu)技巧
超詳細(xì)!一文帶你了解LVS四層負(fù)載均衡企業(yè)級實踐!
收藏 | Linux系統(tǒng)日志位置及包含的日志內(nèi)容介紹
100 道 Linux 常見面試題,建議收藏,慢慢讀~
服務(wù)器12種基本故障+排查方法
IT運維管理常用工具大全,讓你成為真正的高手
什么是QoS?
有收獲,點個在看?


