ansible集群控制

2021-10-22 02:11:37 字數 2377 閱讀 5431

#介紹

ansible是新出現的自動化運維工具,基於python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,

實現了批量系統配置、批量程式部署、批量執行命令等功能。

無客戶端。

#工作原理

#ansible基礎

1.定義主機清單 vim /etc/ansible/hosts

2.測試連通性 ansible localhost -m ping -m 指定模組。ping只是其中乙個模組。還有shell,yum等等

3.簡潔輸出 ansible host1 -m ping -o -o成行

inventory -主機清單

1 增加主機組 vim /etc/ansible/hosts [webserver] host1 host2 host3 host4

2 增加使用者名稱 密碼 [webserver]

host[1:4] ansible_ssh_user=『root』 ansible_ssh_pass=『666666』

組:變數相同變數可以定義乙個

[webserver]

host[1:4]

[webserver:vars]

ansible_ssh_user=『root』

ansible_ssh_pass=『666666』

子分組[apache]

host[1:2]

[nginx]

host[3:4]

[webserver:children]

apache

nginx

[webserver:vars]

ansible_ssh_user=『root』

ansible_ssh_pass=『666666』

1.shell模組 通過shell模組ansible 可直接加shell命令

ansible webserver -m shell -a 『hostname』 -o 獲取主機名

ansible webserver -m shell -a 『hostname』 -o -f 2 -f 2 指定執行緒數

2.複製模組 copy

ansible webserver -m copy -a 『src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes』src源位址 dest目標位址

3.使用者模組

ansible webserver -m user -a 『name=qianfeng state=present』 建立

echo 『512050951』 | openssl passwd -1 -stdin 生成加密檔案

ansible webserver -m user -a 『name=qianfeng password=「1

11xvzsjmdr$5wi4ouaq.emxap6s.n272.」』 加密

4.軟體包管理

ansible host2 -m yum -a 『name=「httpd」 state=latest』 安裝apache

5.服務模組 ansible-doc service

ansible host2 -m service -a 『name=httpd state=started』 啟動

ansible host2 -m service -a 『name=httpd state=started enabled=yes』 開機啟動

6.檔案模組 ansible-doc file

ansible host1 -m file -a 『path=/tmp/88.txt mode=777 state=touch』 建立檔案

7.收集模組 ansible-doc setup

ansible host3 -m setup 查詢所有資訊

ansible host3 -m setup -a 『filter=ansible_all_ipv4_addresses』

**roles則是在ansible中,playbooks的目錄組織結構。將**或檔案進行模組化,成為roles的檔案目錄組織結構,易讀,**可重用,層次清晰。

nginx 角色名

files 普通檔案

handlers 觸發器程式

tasks 主任務

templates 金甲模板(有變數的檔案)

vars 自定義變數

自用 ansible集群

1.環境準備 3臺虛擬機器 centos7 ansible,管理節點 192.168.189.171 centos6 被管理節點 192.168.189.162 centos6 被管理節點 192.168.189.163 所有節點的防火牆和iptables都關一下!centos7 systemctl...

集群工具ansible簡單使用

ansible是與puppet saltstack類似的集群管理工具,其優點是僅需要ssh和python即可使用,而不像puppet saltstack那樣都需要客戶端。與puppet類似,ansible也支援豐富的功能 安裝方法為 yum y install ansible或pip install...

Ansible流程控制

在開發中,經常會編寫一些流程控制相關的邏輯,如條件判斷 迴圈等,ansible也提供了對條件判斷 迴圈等的操作。ansible的模板配置是基於jinja2的,如變數的定義 關於when的更多示例 hosts proxy tasks name deploying to development.debu...