ansible自动化管理
安裝
編譯安裝
解決依賴關(guān)系
#?yum?-y?install?python-jinja2?PyYAML?python-paramiko?python-babel?python-crypto
#?tar?xf?ansible-1.5.4.tar.gz
#?cd?ansible-1.5.4
#?python?setup.py?build
#?python?setup.py?install
#?mkdir?/etc/ansible
#?cp?-r?examples/*?/etc/ansible
yum包安裝
rpm?-ivh?http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpmyum?clean?all
yum?install?ansible?-y
三、簡(jiǎn)單應(yīng)用
1、修改主機(jī)文件/etc/ansible/hosts
注釋以非空白行、以非警號(hào)的注釋
.,$s/^\([^[:space:]#]\)/#/g
添加組
[ceshiserver]
172.16.29.195
172.16.29.196
?
2、配置ssh
[root@localhost?~]#?ssh-keygen?-t?rsa?-f?/root/.ssh/id_rsa?-P?''
[root@localhost?~]#?ssh-copy-id?-i?.ssh/id_rsa.pub?root@172.16.29.195
[root@localhost?~]#?ssh-copy-id?-i?.ssh/id_rsa.pub?root@172.16.29.196
3、批量操作
ansible通過(guò)ssh實(shí)現(xiàn)配置管理、應(yīng)用部署、任務(wù)執(zhí)行等功能,因此,需要事先配置ansible端能基于密鑰認(rèn)證的方式聯(lián)系各被管理節(jié)點(diǎn)。
ansible?<host-pattern>?[-f?forks]?[-m?module_name]?[-a?args]
-m?module:默認(rèn)為command
-a指定選項(xiàng)?-f指定主機(jī)數(shù),默認(rèn)5臺(tái)
例子:
拷貝:ansible?dbserver?-m?copy?-a?“src=/root/hello.sh?dest=/tmp/”
Ping:ansible?all?-m?ping?
定時(shí)任務(wù):ansible?all?-m?cron?-a?‘name=”custom?job”?minute=*/3?hour=*?day=*?month=*?weekday=*?job=”/usr/sbin/ntpdate?172.16.0.1”’
Yum安裝:ansible?all?-m?yum?-a?“state=present?name=corosync”
Yum卸載:ansible?all?-m?yum?-a?“state=absent?name=corosync”
查看:ansible?all?-a?'ls?/tmp'
?
ansible?模塊使用方法幫助:
ansible-doc:?Show?Ansible?module?documentation
?-l,?--list????????????List?available?modules
??-s,?--snippet?????????Show?playbook?snippet?for?specified?module(s)
ansible-doc?-l列出所有支持的模塊
Ansible-doc?-s?group?查看group的用法
?
四、YAML
?
4.1?YAML介紹
?
YAML是一個(gè)可讀性高的用來(lái)表達(dá)資料序列的格式。YAML參考了其他多種語(yǔ)言,包括:XML、C語(yǔ)言、Python、Perl以及電子郵件格式RFC2822等。Clark?Evans在2001年在首次發(fā)表了這種語(yǔ)言,另外Ingy?dt?Net與Oren?Ben-Kiki也是這語(yǔ)言的共同設(shè)計(jì)者。
?
YAML?Ain't?Markup?Language,即YAML不是XML。不過(guò),在開(kāi)發(fā)的這種語(yǔ)言時(shí),YAML的意思其實(shí)是:"Yet?Another?Markup?Language"(仍是一種標(biāo)記語(yǔ)言)。其特性:
?
YAML的可讀性好
YAML和腳本語(yǔ)言的交互性好
YAML使用實(shí)現(xiàn)語(yǔ)言的數(shù)據(jù)類型
YAML有一個(gè)一致的信息模型
YAML易于實(shí)現(xiàn)
YAML可以基于流來(lái)處理
YAML表達(dá)能力強(qiáng),擴(kuò)展性好
?
更多的內(nèi)容及規(guī)范參見(jiàn)http://www.yaml.org。
?
4.2?YAML語(yǔ)法
?
YAML的語(yǔ)法和其他高階語(yǔ)言類似,并且可以簡(jiǎn)單表達(dá)清單、散列表、標(biāo)量等數(shù)據(jù)結(jié)構(gòu)。其結(jié)構(gòu)(Structure)通過(guò)空格來(lái)展示,序列(Sequence)里的項(xiàng)用"-"來(lái)代表,Map里的鍵值對(duì)用":"分隔。下面是一個(gè)示例。
?
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。
?
?
五、ansible?playbooks
?
playbook是由一個(gè)或多個(gè)“play”組成的列表。play的主要功能在于將事先歸并為一組的主機(jī)裝扮成事先通過(guò)ansible中的task定義好的角色。從根本上來(lái)講,所謂task無(wú)非是調(diào)用ansible的一個(gè)module。將多個(gè)play組織在一個(gè)playbook中,即可以讓它們聯(lián)同起來(lái)按事先編排的機(jī)制同唱一臺(tái)大戲。下面是一個(gè)簡(jiǎn)單示例。
?
-?hosts:?webnodes
??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
?
5.1?playbook基礎(chǔ)組件
?
5.1.1?Hosts和Users
playbook中的每一個(gè)play的目的都是為了讓某個(gè)或某些主機(jī)以某個(gè)指定的用戶身份執(zhí)行任務(wù)。hosts用于指定要執(zhí)行指定任務(wù)的主機(jī),其可以是一個(gè)或多個(gè)由冒號(hào)分隔主機(jī)組;remote_user則用于指定遠(yuǎn)程主機(jī)上的執(zhí)行任務(wù)的用戶。如上面示例中的
-hosts:?webnodes
?remote_user:?root
?
不過(guò),remote_user也可用于各task中。也可以通過(guò)指定其通過(guò)sudo的方式在遠(yuǎn)程主機(jī)上執(zhí)行任務(wù),其可用于play全局或某任務(wù);此外,甚至可以在sudo時(shí)使用sudo_user指定sudo時(shí)切換的用戶。
?
-?hosts:?webnodes
??remote_user:?mageedu
??tasks:
????-?name:?test?connection
??????ping:
??????remote_user:?mageedu
??????sudo:?yes
?
5.1.2?任務(wù)列表和action
?
play的主體部分是task?list。task?list中的各任務(wù)按次序逐個(gè)在hosts中指定的所有主機(jī)上執(zhí)行,即在所有主機(jī)上完成第一個(gè)任務(wù)后再開(kāi)始第二個(gè)。在運(yùn)行自下而下某playbook時(shí),如果中途發(fā)生錯(cuò)誤,所有已執(zhí)行任務(wù)都將回滾,因此,在更正playbook后重新執(zhí)行一次即可。
?
task的目的是使用指定的參數(shù)執(zhí)行模塊,而在模塊參數(shù)中可以使用變量。模塊執(zhí)行是冪等的,這意味著多次執(zhí)行是安全的,因?yàn)槠浣Y(jié)果均一致。
?
每個(gè)task都應(yīng)該有其name,用于playbook的執(zhí)行結(jié)果輸出,建議其內(nèi)容盡可能清晰地描述任務(wù)執(zhí)行步驟。如果未提供name,則action的結(jié)果將用于輸出。
?
定義task的可以使用“action:?module?options”或“module:?options”的格式,推薦使用后者以實(shí)現(xiàn)向后兼容。如果action一行的內(nèi)容過(guò)多,也中使用在行首使用幾個(gè)空白字符進(jìn)行換行。
tasks:
??-?name:?make?sure?apache?is?running
????service:?name=httpd?state=running
?
在眾多模塊中,只有command和shell模塊僅需要給定一個(gè)列表而無(wú)需使用“key=value”格式,例如:
tasks:
??-?name:?disable?selinux
????command:?/sbin/setenforce?0
?
如果命令或腳本的退出碼不為零,可以使用如下方式替代:
tasks:
??-?name:?run?this?command?and?ignore?the?result
????shell:?/usr/bin/somecommand?||?/bin/true
?
或者使用ignore_errors來(lái)忽略錯(cuò)誤信息:
tasks:
??-?name:?run?this?command?and?ignore?the?result
????shell:?/usr/bin/somecommand
????ignore_errors:?True
?
5.1.3?handlers
用于當(dāng)關(guān)注的資源發(fā)生變化時(shí)采取一定的操作。
?
“notify”這個(gè)action可用于在每個(gè)play的最后被觸發(fā),這樣可以避免多次有改變發(fā)生時(shí)每次都執(zhí)行指定的操作,取而代之,僅在所有的變化發(fā)生完成后一次性地執(zhí)行指定操作。在notify中列出的操作稱為handler,也即notify中調(diào)用handler中定義的操作。
-?name:?template?configuration?file
??template:?src=template.j2?dest=/etc/foo.conf
??notify:
?????-?restart?memcached
?????-?restart?apache
?
handler是task列表,這些task與前述的task并沒(méi)有本質(zhì)上的不同。
handlers:
????-?name:?restart?memcached
??????service:??name=memcached?state=restarted
????-?name:?restart?apache
??????service:?name=apache?state=restarted
?
案例:
heartbeat.yaml
-?hosts:?hbhosts
??remote_user:?root
??tasks:
????-?name:?ensure?heartbeat?latest?version
??????yum:?name=heartbeat?state=present
????-?name:?authkeys?configure?file
??????copy:?src=/root/hb_conf/authkeys?dest=/etc/ha.d/authkeys
????-?name:?authkeys?mode?600
??????file:?path=/etc/ha.d/authkeys?mode=600
??????notify:
????????-?restart?heartbeat
????-?name:?ha.cf?configure?file
??????copy:?src=/root/hb_conf/ha.cf?dest=/etc/ha.d/ha.cf
??????notify:?
???????-?restart?heartbeat
??handlers:
??-?name:?restart?heartbeat
????service:?name=heartbeat?state=restarted
本文轉(zhuǎn)自 趙大鑫 51CTO博客,原文鏈接:http://blog.51cto.com/xinzong/1697909,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的ansible自动化管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 清华裴丹:AIOps 落地路线图
- 下一篇: selenium webdirver之r