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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ansible介绍+基本操作

發(fā)布時(shí)間:2025/3/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ansible介绍+基本操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1ansible介紹

- Ansible基于Python語(yǔ)言實(shí)現(xiàn),由paramiko和PyYAML兩個(gè)關(guān)鍵模塊構(gòu)建

- 不需要安裝客戶端,通過(guò)sshd去通信

- 基于模塊工作,模塊可以由任何語(yǔ)言開發(fā)

- 不僅支持命令行使用模塊,也支持編寫yaml格式的playbook,易于編寫和閱讀

- 有提供UI(瀏覽器圖形化)www.ansible.com/tower,收費(fèi)的

- 官方文檔 ?http://docs.ansible.com/ansible/latest/index.html

- ansible已經(jīng)被redhat公司收購(gòu),它在github上是一個(gè)非常受歡迎的開源軟件,github地址https://github.com/ansible/ansible

- 一本不錯(cuò)的入門電子書 https://ansible-book.gitbooks.io/ansible-first-book/

- Ansible的基本架構(gòu):

1. 核心引擎:即圖中所看到的ansible

2. 核心模塊(core module):在模塊庫(kù)(module library)中分為兩塊,一個(gè)是核心模塊另外一個(gè)就是自定義模塊(custom modules)。核心模塊中都是ansible自帶的模塊,Ansible模塊資源分發(fā)到遠(yuǎn)程節(jié)點(diǎn)使其執(zhí)行特定任務(wù)或匹配一個(gè)特定的狀態(tài)。這些核心核心模塊都遵循這batteries included哲學(xué)。其實(shí)這里這個(gè)還是很有意思的,batterires included:Python has a large standard library, commonly cited as one of Python’s greatest strengths,providing tools suited to many tasks. 這就意味著Python有這巨大的庫(kù)支持你完成你想完成的任務(wù)工作。

3. 自定義模塊(custom modules):如果在Ansible中滿足不了你所需求的模塊,那么Ansible也能提供添加自定義化的模塊。

4. 插件(plugin):這里我的理解就是完成較小型的任務(wù)。輔助協(xié)助模塊來(lái)完成某個(gè)功能。

5. 劇本(playbook):定義需要給遠(yuǎn)程主機(jī)執(zhí)行的一系列任務(wù)。例如安裝一個(gè)nginx服務(wù),那么我們可以把這拆分為幾個(gè)任務(wù)放到一個(gè)playbook中。例如:第一步需要下載nginx的安裝包。第二步我可能考慮需要做的就是將我事先寫好的nginx.conf的配置文件下發(fā)的目標(biāo)服務(wù)器上。第三步,我們需要把服務(wù)啟動(dòng)起來(lái)。第四步,我們可能需要檢查端口是否正常開啟。那么這些步驟可以通過(guò)playbook來(lái)進(jìn)行整合,然后通過(guò)inventory來(lái)下發(fā)到想要執(zhí)行劇本的主機(jī)上。并且playbook也支持交互式執(zhí)行playbook里面的步驟,而且如果有那一個(gè)步驟執(zhí)行返回了一個(gè)錯(cuò)誤報(bào)告,那么我們可以僅僅只單獨(dú)執(zhí)行這個(gè)步驟。你可以把playbook理解成為一個(gè)組策略,控制管理這個(gè)OU下所有的主機(jī)行為。

6. 連接插件(connectior plugins):Ansible默認(rèn)是基于SSH連接到目標(biāo)機(jī)器上執(zhí)行操作的。但是同樣的Ansible支持不同的連接方法,要是這樣的話就需要連接插件來(lái)幫助我們完成連接了。

7. 主機(jī)清單(host inventory):為Ansible定義了管理主機(jī)的策略。一般小型環(huán)境下我們只需要在host文件中寫入主機(jī)的IP地址即可,但是到了中大型環(huán)境我們有可能需要使用靜態(tài)inventory或者動(dòng)態(tài)主機(jī)清單來(lái)生成我們所需要執(zhí)行的目標(biāo)主機(jī)。


2 ansible安裝

- 環(huán)境:準(zhǔn)備兩臺(tái)機(jī)器?

- chy 192.168.212.10 ?//只需要在這臺(tái)機(jī)器上安裝ansible

- chy01 192.168.212.11

- 安裝如下 chy//192.168.212.10?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [root@chy?~]#?yum?list?|grep?ansible?//看到有2.4版本的yum包,只需要安裝自帶的源即可 ansible.noarch??????????????????????????2.4.0.0-5.el7??????????????????extras??? ansible-doc.noarch??????????????????????2.4.0.0-5.el7??????????????????extras??? ansible-inventory-grapher.noarch????????2.3.2-1.el7????????????????????epel????? ansible-lint.noarch?????????????????????3.4.15-1.el7???????????????????epel????? ansible-openstack-modules.noarch????????0-20140902git79d751a.el7???????epel????? ansible-review.noarch???????????????????0.13.0-2.el7???????????????????epel????? kubernetes-ansible.noarch???????????????0.6.0-0.1.gitd65ebd5.el7???????epel? python2-ansible-tower-cli.noarch????????3.1.7-1.el7????????????????????epel?? [root@chy?~]#?yum?install?-y?ansible?ansible-doc?//安裝ansible [root@chy?~]#?ls?/root/.ssh/?//查看有沒有密鑰對(duì),如果有就無(wú)需在重新生成,如果之前沒有生成過(guò)需要用ssh-keygen生成一下密鑰對(duì)。 authorized_keys??id_rsa???????????id_rsa.pub???????known_hosts??? 之后將公鑰復(fù)制到另一臺(tái)機(jī)器上 [root@chy01?~]#?cat?.ssh/authorized_keys?//公鑰已經(jīng)放在了里面 [root@chy?~]#?ssh?chy01 The?authenticity?of?host?'chy01?(192.168.212.11)'?can't?be?established. ECDSA?key?fingerprint?is?de:d2:32:86:e0:89:5c:2c:51:68:92:9b:7e:40:52:5c. Are?you?sure?you?want?to?continue?connecting?(yes/no)??yes Warning:?Permanently?added?'chy01'?(ECDSA)?to?the?list?of?known?hosts. Last?login:?Wed?Nov??8?01:04:24?2017?from?chy #?cat?/etc/motd? [root@chy?~]#?vi?/etc/ansible/hosts?//配置主機(jī)組 ##?[webservers] ##?alpha.example.org ##?beta.example.org ##?192.168.1.100 ##?192.168.1.110 [testhost] chy02 chy01 說(shuō)明:?testhost為主機(jī)組名字,自定義的。?下面兩個(gè)ip為組內(nèi)的機(jī)器ip。 增加testhost里的內(nèi)容,testhost下面可以寫主機(jī)名也可以寫ip地址,如果寫了主機(jī)名切記需要去/etc/hosts里配置,如果做了dns則不需要去/etc/hosts配置(這里需要注意的是組里添加的機(jī)器的前提是都要做密鑰認(rèn)證的)

3ansible遠(yuǎn)程執(zhí)行命令

-遠(yuǎn)程執(zhí)行命令操作如下

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [root@chy?~]#?ansible?testhost?-m?command?-a?'w' chy01?|?SUCCESS?|?rc=0?>> ?01:46:28?up??1:00,??2?users,??load?average:?0.16,?0.05,?0.06 USER?????TTY??????FROM?????????????LOGIN@???IDLE???JCPU???PCPU?WHAT root?????pts/0????192.168.212.1????00:54???37:48???0.02s??0.02s?-bash root?????pts/1????chy??????????????01:46????0.00s??0.26s??0.05s?w chy02?|?SUCCESS?|?rc=0?>> ?01:46:28?up??2:24,??2?users,??load?average:?0.00,?0.01,?0.05 USER?????TTY??????FROM?????????????LOGIN@???IDLE???JCPU???PCPU?WHAT root?????pts/0????192.168.212.1????00:27????1:08m??0.02s??0.02s?-bash root?????pts/1????chy??????????????01:46????1.00s??0.30s??0.00s?w 語(yǔ)法介紹:ansible?后跟定義的主機(jī)組?-m?跟模塊,-?a跟需要執(zhí)行的命令 [root@chy?~]#?ansible?chy01?-m?command?-a?'hostname' chy01?|?SUCCESS?|?rc=0?>> chy01 如上也可以只針對(duì)一臺(tái)機(jī)器進(jìn)行操作

如果出現(xiàn)了如下錯(cuò)誤,請(qǐng)按照如下的方法解決

1 2 3 ansible?127.0.0.1?-m??command?-a?'hostname' ?錯(cuò)誤:"msg":?"Aborting,?target?uses?selinux?but?python?bindings?(libselinux-python)?aren't?installed!" ?解決:yum?install?-y?libselinux-python

4 anonymous拷貝文件或目錄

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root@chy?~]#?ansible?chy01?-m?copy?-a?"src=/etc/ansible?dest=/tmp/ansible_test?owner=root?group=root?mode=0755" chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"dest":?"/tmp/ansible_test/",? ????"failed":?false,? ????"src":?"/etc/ansible" } 拷貝目錄注意:源目錄會(huì)放到目標(biāo)目錄下面去,如果目標(biāo)指定的目錄不存在,它會(huì)自動(dòng)創(chuàng)建。如果拷貝的是文件,dest指定的名字和源如果不同,并且它不是已經(jīng)存在的目錄,相當(dāng)于拷貝過(guò)去后又重命名。但相反,如果desc是目標(biāo)機(jī)器上已經(jīng)存在的目錄,則會(huì)直接把文件拷貝到該目錄下面。 [root@chy?~]#?ansible?chy01?-m?copy?-a?"src=/etc/passwd?dest=/tmp??owner=root?group=root?mode=0755"?//copy文件 chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"checksum":?"84c5bb4be970a90c7157c2d57401ca0ac0039eca",? ????"dest":?"/tmp/passwd",? ????"failed":?false,? ????"gid":?0,? ????"group":?"root",? ????"md5sum":?"177c3249629069b366250d27ef7820df",? ????"mode":?"0755",? ????"owner":?"root",? ????"size":?2182,? ????"src":?"/root/.ansible/tmp/ansible-tmp-1510078509.74-153887684649484/source",? ????"state":?"file",? ????"uid":?0 }

[root@chy ~]# ansible chy01 -m copy -a "src=/etc/passwd dest=/tmp/copy.txt ?owner=root group=root mode=0755" //這里需要注意一下就是如果想要將cp過(guò)去的文件改名稱直接可以在cp的過(guò)程中改名字,在操作時(shí)拷貝到相應(yīng)的目錄后,后面直接跟文件名,這樣就會(huì)直接cp過(guò)去的

chy01 | SUCCESS => {

? ? "changed": true,?

? ? "checksum": "84c5bb4be970a90c7157c2d57401ca0ac0039eca",?

? ? "dest": "/tmp/copy.txt",?

? ? "failed": false,?

? ? "gid": 0,?

? ? "group": "root",?

? ? "md5sum": "177c3249629069b366250d27ef7820df",?

? ? "mode": "0755",?

? ? "owner": "root",?

? ? "size": 2182,?

? ? "src": "/root/.ansible/tmp/ansible-tmp-1510078638.87-216978821762184/source",?

? ? "state": "file",?

? ? "uid": 0

}

[root@chy01 ~]# ls -ls /tmp/copy.txt ?//可以查看到已經(jīng)cp過(guò)來(lái)并且已經(jīng)改名成功

4 -rwxr-xr-x 1 root root 2182 Nov ?8 02:17 /tmp/copy.txt


5 ansible遠(yuǎn)程執(zhí)行腳本

- shell模塊是用來(lái)執(zhí)行腳本的,如下是更詳細(xì)的操作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 [root@chy?~]#?cat?/tmp/1.sh? #!/bin/bash ?echo?`date`?>?/tmp/ansible_test.txt 寫一個(gè)輸出時(shí)間的腳本 之前的saltstack腳本可以直接搞到遠(yuǎn)程并且執(zhí)行,但是ansible必須先拷貝,之后在執(zhí)行 [root@chy?~]#?ansible?testhost?-m?copy?-a?"src=/tmp/1.sh?dest=/tmp/test.sh?mode=0755"?//執(zhí)行的是兩臺(tái)機(jī)器 chy02?|?SUCCESS?=>?{ ????"changed":?true,? ????"checksum":?"1d452b51a06996a4ead87e91a7a288d3318f3e0c",? ????"dest":?"/tmp/test.sh",? ????"failed":?false,? ????"gid":?0,? ????"group":?"root",? ????"md5sum":?"8d6e5eb9fca38ae7c456a9da182e4426",? ????"mode":?"0755",? ????"owner":?"root",? ????"size":?50,? ????"src":?"/root/.ansible/tmp/ansible-tmp-1510080028.29-281241865001849/source",? ????"state":?"file",? ????"uid":?0 } chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"checksum":?"1d452b51a06996a4ead87e91a7a288d3318f3e0c",? ????"dest":?"/tmp/test.sh",? ????"failed":?false,? ????"gid":?0,? ????"group":?"root",? ????"md5sum":?"8d6e5eb9fca38ae7c456a9da182e4426",? ????"mode":?"0755",? ????"owner":?"root",? ????"size":?50,? ????"src":?"/root/.ansible/tmp/ansible-tmp-1510080028.3-155921385180503/source",? ????"state":?"file",? ????"uid":?0 } [root@chy?~]#?ansible?testhost?-m?shell?-a?"/tmp/test.sh"?//之后在執(zhí)行腳本,-a?后跟執(zhí)行的腳本即可 chy01?|?SUCCESS?|?rc=0?>> chy02?|?SUCCESS?|?rc=0?>>

- command不能帶管道符,而shell可以用管道符

測(cè)試如下

1 2 3 4 5 6 7 8 [root@chy?~]#?ansible?chy01?-m?command?-a?"cat?/etc/passwd|wc?-l" chy01?|?FAILED?|?rc=1?>> cat:無(wú)效選項(xiàng)?--?l Try?'cat?--help'?for?more?information.non-zero?return?code [root@chy?~]#?ansible?chy01?-m?shell??-a?"cat?/etc/passwd|wc?-l" chy01?|?SUCCESS?|?rc=0?>> 34

6 管理計(jì)劃

- 管理計(jì)劃用到的模塊是cron

如下操作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@chy?~]#?ansible?chy01?-m?cron?-a?"name='test?cron'?job='/bin/touch/tmp/1212.txt'?weekday=6" -m?后跟模塊的名稱,-a?跟執(zhí)行的命令"name?跟任務(wù)的名稱,job跟命令是什么,在后面就是分時(shí)日月周,如果定義就跟具體的,不定義直接跟*就可以 chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"envs":?[],? ????"failed":?false,? ????"jobs":?[ ????????"test?cron" ????] } [root@chy01?ansible]#?crontab?-l?//查看任務(wù)計(jì)劃 0?0?*?*?*?/bin/bash?/usr/local/sbin/nginx_logrotate.sh 0?1?*?*?*?/bin/python?/home/jumpserver/manage.py?crontab?run?3718e5baf203ed0f54703b2f0b7e9e16?#?django-cronjobs?for?jumpserver */10?*?*?*?*?/bin/python?/home/jumpserver/manage.py?crontab?run?9956b75140f4453ab1dc4aeb62962a74?#?django-cronjobs?for?jumpserver #?Lines?below?here?are?managed?by?Salt,?do?not?edit #Ansible:?test?cron *?*?*?*?6?/bin/touch/tmp/1212.txt You?have?new?mail?in?/var/spool/mail/root [root@chy?~]#?ansible?testhost?-m?cron?-a?"name='test?cron'?state=absent"?刪除任務(wù)計(jì)劃 chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"envs":?[],? ????"failed":?false,? ????"jobs":?[] } 切記在做任務(wù)計(jì)劃時(shí)千萬(wàn)不要手動(dòng)修改,任務(wù)計(jì)劃里的#?#?Lines?below?here?are?managed?by?Salt,?do?not?edit #Ansible:?test?cron 這行內(nèi)容。

希望看過(guò)的童鞋多多指教,謝謝!



本文轉(zhuǎn)自我不是瘦子51CTO博客,原文鏈接:http://blog.51cto.com/chy940405/1979724,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者


總結(jié)

以上是生活随笔為你收集整理的ansible介绍+基本操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。