日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ansible自动化管理

發布時間:2024/4/13 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ansible自动化管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

安裝

編譯安裝

解決依賴關系

#?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

三、簡單應用

1、修改主機文件/etc/ansible/hosts

注釋以非空白行、以非警號的注釋

.,$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通過ssh實現配置管理、應用部署、任務執行等功能,因此,需要事先配置ansible端能基于密鑰認證的方式聯系各被管理節點。

ansible?<host-pattern>?[-f?forks]?[-m?module_name]?[-a?args]

-m?module:默認為command

-a指定選項?-f指定主機數,默認5臺

例子:

拷貝:ansible?dbserver?-m?copy?-a?src=/root/hello.sh?dest=/tmp/

Ping:ansible?all?-m?ping?

定時任務: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是一個可讀性高的用來表達資料序列的格式。YAML參考了其他多種語言,包括:XMLC語言、PythonPerl以及電子郵件格式RFC2822等。Clark?Evans2001年在首次發表了這種語言,另外Ingy?dt?NetOren?Ben-Kiki也是這語言的共同設計者。

?

YAML?Ain't?Markup?Language,即YAML不是XML。不過,在開發的這種語言時,YAML的意思其實是:"Yet?Another?Markup?Language"(仍是一種標記語言)。其特性:

?

YAML的可讀性好

YAML和腳本語言的交互性好

YAML使用實現語言的數據類型

YAML有一個一致的信息模型

YAML易于實現

YAML可以基于流來處理

YAML表達能力強,擴展性好

?

更多的內容及規范參見http://www.yaml.org

?

4.2?YAML語法

?

YAML的語法和其他高階語言類似,并且可以簡單表達清單、散列表、標量等數據結構。其結構(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文件擴展名通常為.yaml,如example.yaml

?

?

五、ansible?playbooks

?

playbook是由一個或多個“play”組成的列表。play的主要功能在于將事先歸并為一組的主機裝扮成事先通過ansible中的task定義好的角色。從根本上來講,所謂task無非是調用ansible的一個module。將多個play組織在一個playbook中,即可以讓它們聯同起來按事先編排的機制同唱一臺大戲。下面是一個簡單示例。

?

-?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基礎組件

?

5.1.1?HostsUsers


playbook中的每一個play的目的都是為了讓某個或某些主機以某個指定的用戶身份執行任務。hosts用于指定要執行指定任務的主機,其可以是一個或多個由冒號分隔主機組;remote_user則用于指定遠程主機上的執行任務的用戶。如上面示例中的

-hosts:?webnodes

?remote_user:?root

?

不過,remote_user也可用于各task中。也可以通過指定其通過sudo的方式在遠程主機上執行任務,其可用于play全局或某任務;此外,甚至可以在sudo時使用sudo_user指定sudo時切換的用戶。

?

-?hosts:?webnodes

??remote_user:?mageedu

??tasks:

????-?name:?test?connection

??????ping:

??????remote_user:?mageedu

??????sudo:?yes

?

5.1.2?任務列表和action

?

play的主體部分是task?listtask?list中的各任務按次序逐個在hosts中指定的所有主機上執行,即在所有主機上完成第一個任務后再開始第二個。在運行自下而下某playbook時,如果中途發生錯誤,所有已執行任務都將回滾,因此,在更正playbook后重新執行一次即可。

?

task的目的是使用指定的參數執行模塊,而在模塊參數中可以使用變量。模塊執行是冪等的,這意味著多次執行是安全的,因為其結果均一致。

?

每個task都應該有其name,用于playbook的執行結果輸出,建議其內容盡可能清晰地描述任務執行步驟。如果未提供name,則action的結果將用于輸出。

?

定義task的可以使用“action:?module?options”或“module:?options”的格式,推薦使用后者以實現向后兼容。如果action一行的內容過多,也中使用在行首使用幾個空白字符進行換行。

tasks:

??-?name:?make?sure?apache?is?running

????service:?name=httpd?state=running

?

在眾多模塊中,只有commandshell模塊僅需要給定一個列表而無需使用“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來忽略錯誤信息:

tasks:

??-?name:?run?this?command?and?ignore?the?result

????shell:?/usr/bin/somecommand

????ignore_errors:?True

?

5.1.3?handlers


用于當關注的資源發生變化時采取一定的操作。

?

notify”這個action可用于在每個play的最后被觸發,這樣可以避免多次有改變發生時每次都執行指定的操作,取而代之,僅在所有的變化發生完成后一次性地執行指定操作。在notify中列出的操作稱為handler,也即notify中調用handler中定義的操作。

-?name:?template?configuration?file

??template:?src=template.j2?dest=/etc/foo.conf

??notify:

?????-?restart?memcached

?????-?restart?apache

?

handlertask列表,這些task與前述的task并沒有本質上的不同。

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










本文轉自 趙大鑫 51CTO博客,原文鏈接:http://blog.51cto.com/xinzong/1697909,如需轉載請自行聯系原作者

總結

以上是生活随笔為你收集整理的ansible自动化管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。