1. 程式人生 > >centos7.3 安裝ansible

centos7.3 安裝ansible

ansible

1、ansible介紹
            ansible是新出現的自動化運維工具,基於Python研發。糅合了眾多老牌運維工具的優點實現了批量操作系統配置、批量程序的部署、批量運行命令等功能。僅需在管理工作站上安裝ansible程序配置被管控主機的IP信息,被管控的主機無客戶端。ansible應用程序存在於epel(第三方社區)源,依賴於很多python組件。主要包括:

(1)、連接插件connection plugins:負責和被監控端實現通信;

(2)、host inventory:指定操作的主機,是一個配置文件裏面定義監控的主機;

(3)、各種模塊核心模塊、command模塊、自定義模塊;

(4)、借助於插件完成記錄日誌郵件等功能;

(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性運行多個任務。

2、ansible特性

(1)模塊化設計,調用特定的模塊來完成特定任務,本身是核心組件,短小精悍;

(2)基於Python語言實現,由Paramiko(python的一個可並發連接ssh主機功能庫), PyYAML和Jinja2(模板化)三個關鍵模塊實現;

(3)部署簡單,agentless無客戶端工具;

(4)主從模式工作;

(5)支持自定義模塊功能;

(6)支持playbook劇本,連續任務按先後設置順序完成;

(7)期望每個命令具有冪等性:

3.ansible架構

(1)ansible core:ansible自身核心模塊

(2)host inventory:主機庫,定義可管控的主機列表

(3)connection plugins:連接插件,一般默認基於ssh協議連接

(4)modules:core modules(自帶模塊)、custom modules(自定義模塊)

(5)playbooks:劇本,按照所設定編排的順序執行完成安排任務

4.配置文件:

(1)ansible應用程序的主配置文件:/etc/ansible/ansible.cfg

(2) Host Inventory定義管控主機:/etc/ansible/hosts

5.配置

1.配置時間同步
2.關閉防火墻
3.關閉selinux
4.配置DNS
5.下載yum源

環境準備:
4臺centos 7.3,騰訊雲
ansible-server 10.0.0.11/24

test1 10.0.0.6/24
test2 10.0.0.14/24
test3 10.0.0.10/24

參考文章:
http://www.linuxidc.com/Linux/2015-10/123801.htm
http://docs.ansible.com/ansible/latest/intro_installation.html

安裝ansible,在ansible-server上操作
1、yum安裝
yum install ansible -y

ansible --version

若遇報錯,則需paramiko模塊安裝

https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.11.tar.gz

tar xvzf ecdsa-0.11.tar.gz

cd ecdsa-0.11

python setup.py install

https://pypi.python.org/packages/source/p/paramiko/paramiko-1.15.1.tar.gz

#tar xvzf paramiko-1.15.1.tar.gz

cd paramiko-1.15.1

python setup.py install

2、設置用於節點鑒權的SSH密鑰並復制到節點上

ssh-keygen -t rsa

ssh-copy-id -i 10.0.0.14

 ssh-copy-id -i 10.0.0.6

 ssh-copy-id -i 10.0.0.10

3、為ansible定義節點的清單

vim /etc/ansible/hosts

[test]
10.0.0.6
10.0.0.10
10.0.0.14

4、嘗試在ansible服務端運行命令

 ansible -m ping ‘test‘

ansible命令參數介紹

-m:要執行的模塊,默認為command
-a:模塊的參數
-u:ssh連接的用戶名,默認用root,ansible.cfg中可以配置
-k:提示輸入ssh登錄密碼。當使用密碼驗證的時候用
-s:sudo運行
-U:sudo到那個用戶,默認為root
-K:提示輸入sudo密碼,當不是NOPASSWD模式時使用
-C:只是測試一下會改變什麽內容,不會真正去執行
-c:連接類型(default=smart)
-f:fork多少個進程並發處理,默認為5個
-i:指定hosts文件路徑,默認default=/etc/ansible/hosts
-I 指定pattern,對<host_pattern>已匹配的主機中再過濾一次
--list-hosts:只打印有哪些主機會執行這個playbook文件,不是實際執行
-M:要執行的模塊路徑,默認為/usr/share/ansible
-o:壓縮輸出,摘要輸出
--private-key 私鑰路徑
-T: ssh連接超時時間,默認10秒
-t:日誌輸出到該目錄,日誌文件名以主機名命名
-v:verbost

ansible基本命令的使用

語法

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

-f forks:啟動的並發數

-m module_name:使用的模塊

-args:模塊特有參數

[root@yunwei ~]# ansible web -m ping

192.168.31.114 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.31.113 | SUCCESS => {
"changed": false,
"ping": "pong"
}

command模塊

使用command模塊執行date指令,ansible 默認模塊,不支持變量傳遞。

[root@yunwei ~]# ansible web -m command -a ‘date‘

192.168.31.114 | SUCCESS | rc=0 >>
2017年 03月 24日 星期五 17:09:53 CST

192.168.31.113 | SUCCESS | rc=0 >>
2017年 03月 24日 星期五 17:09:54 CST

[root@yunwei ~]# ansible web -a ‘date‘

192.168.31.113 | SUCCESS | rc=0 >>
2017年 03月 24日 星期五 17:11:02 CST

192.168.31.114 | SUCCESS | rc=0 >>
2017年 03月 24日 星期五 17:11:02 CST

copy模塊

把本地/root/aaa 考到目標主機/tmp/bbb

[root@yunwei ~]# ansible web -m copy -a ‘src=/root/aaa dest=/tmp/bbb owner=root group=root mode=0644‘

192.168.31.114 | SUCCESS => {
"changed": false,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/bbb",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/tmp/bbb",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 0,
"state": "file",
"uid": 0
[root@yunwei ~]# ansible web -a ‘ls -l /tmp/bbb‘
192.168.31.114 | SUCCESS | rc=0 >>
-rw-r--r--. 1 root root 0 3月 24 17:23 /tmp/bbb

命令行簡單舉例

#ansible <pattern_goes_here> -m <module_name> -a <arguments>
1、[root@ju ~]# ansible 192.168.116.138 -m ping #對單臺主機測試ping
2、[root@ju ~]# ansible all -m ping #對/etc/ansible/hosts中所有主機測試ping
3、[root@ju ~]# ansible webserver -a "/bin/echo hello" #運行命令
4、[root@ju ~]# ansible all -a "uptime"
5、[root@ju ~]# ansible dbserver -m copy -a "src=/tmp/ansible dest=/tmp/ansible_1" #copy文件
6、[root@ju ~]# ansible 192.168.116.138 -m file -a "dest=/tmp/ansible_1 mode=600 owner=ju group=ju" #改變文件屬性
7、[root@ju ~]# ansible 192.168.116.138 -m service -a "name=httpd state=running" #啟動服務,或者放到開機啟動的同時運行
8、[root@ju ~]# ansible all -m setup #打印主機的清單,將輸出用於描述每一臺主機的JSON對象,其中包括總體內存、已使用內存、CPU、網絡、磁盤信息、操作系統版本以及內核版本等等。
[root@yunwei ~]# ansible web -m ping

192.168.31.114 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.31.113 | SUCCESS => {
"changed": false,
"ping": "pong"
}

2.168.31.113 | SUCCESS | rc=0 >>
2017年 03月 24日 星期五 17:11:02 CST

192.168.31.114 | SUCCESS | rc=0 >>
2017年 03月 24日 星期五 17:11:02 CST

定義主機與組

1、[root@ju ~]# vim /etc/ansible/hosts #添加主機,並在所配置的主機上與ansible建立互信
2、192.168.116.138
3、192.168.116.139
4、192.168.116.139:7022 #定義一個ssh端口為7022的主機
5、juserver ansible_ssh_port=22 ansible_ssh_host=192.168.116.25 #利用別名定義一個主機,使用的時候直接使用juserver這個別名即可
6、[webserver] #建立分組
7、192.168.116.2
8、192.168.116.3
9、192.168.116.4
10、www[01:50].example.com #支持通配符匹配www01 www02 ...www50
11、[dbserver]
12、192.168.116.5
13、192.168.116.6
14、db-[a:f].example.com #支持字母匹配a b c...f
15、[weballserver:children] #組嵌套,不過這個只能用在playbook中,ansible命令行中使用不了
16、webserver
17、dbserver
18、[myserver]
19、192.168.116.7 http_port=8000 maxRequestsPerChild=808 #可以為每個主機單獨指定一些變量,這些變量可以在playbooks中使用
20、192.168.116.8 http_port=303 maxRequestsPerChild=909
21、[weixinserver]
22、192.168.116.9
23、192.168.116.10
24、[weixinserver:vars] #也可以為一個組指定變量,組內每個主機都可以使用該變量
25、ntp_server=ntp.weixinserver.example.com
26、proxy=proxy.weixinserver.example.com

ansible保留主機變量

1、#ansible_ssh_host:指定主機別名對應的真實IP,如:251 ansible_ssh_host=192.168.116.251,隨後連接該主機無須指定完整IP,只需指定251 就行
2、#ansible_ssh_port:指定連接到這個主機的ssh 端口,默認22
3、#ansible_ssh_user:連接到該主機的ssh用戶
4、#ansible_ssh_pass:連接到該主機的ssh密碼(連-k 選項都省了),安全考慮還是建議使用私鑰或在命令行指定-k 選項輸入
5、#ansible_sudo_pass:sudo 密碼
6、#ansible_sudo_exe(v1.8+的新特性):sudo 命令路徑
7、#ansible_connection:連接類型,可以是local、ssh 或paramiko,ansible1.2 之前默認為paramiko
8、#ansible_ssh_private_key_file:私鑰文件路徑
9、#ansible_shell_type:目標系統的shell類型,默認為sh,如果設置csh/fish,那麽命令需要遵循它們語法
10、#ansible_pythoninterpreter:python 解釋器路徑,默認是/usr/bin/python,但是如要要連freeBSD系統的話,就需要該指令修改python路徑
11、#ansible
_interpreter:這裏的""可以是ruby或perl或其他語言的解釋器,作用和ansible_python_interpreter類似

分離主機和組的變量定義

1、#為host 和group 定義一些比較復雜的變量時(如array、hash),可以用單獨文件保存host和group 變量,以YAML 格式書寫變量,避免都寫在hosts 文件顯得混亂,如果hosts 文件路徑為:
2、/etc/ansible/hosts
3、#則host 和group 變量目錄結構:
4、/etc/ansible/host_vars/all #host_vars 目錄用於存放host 變量,all 文件對所有主機有效
5、/etc/ansible/host_vars/foosball #文件foosball 要和hosts 裏面定義的主機名一樣,表示只對foosball 主機有效
6、/etc/ansible/group_vars/all #group_vars 目錄用於存放group 變量,all 文件對所有組有效
7、/etc/ansible/group_vars/raleigh #文件raleigh 要和hosts 裏面定義的組名一樣,表示對raleigh 組下的所有主機有效
8、#這裏/etc/ansible/group_vars/raleigh 格式如下,YAML 格式要求:
9、ntp_server: acme.example.org #變量名:變量值
10、database_server: storage.example.org

主機匹配方式

1、表示通配inventory 中的所有主機
all 、‘‘ 星號必須引起來
4、#也可以指定具有規則特征的主機或者主機名
one.example.com
one.example.com:two.example.com
192.168.1.50
192.168.1.

9、意思是這兩個組中的所有主機
webservers:dbservers
11、非模式匹配:表示在webservers 組不在phoenix 組的主機
webservers:!phoenix
13、交集匹配:表示同時都在webservers 和staging 組的主機
webservers:&staging
15、webservers:dbservers:&staging:!phoenix
上面這個例子表示“‘webservers’ 和 ‘dbservers’ 兩個組中隸屬於 ‘staging’ 組並且不屬於 ‘phoenix’ 組的機器才執行命令”

查看模塊幫助

1、[root@db ansible]# ansible-doc ping
2、> PING
3、A trivial test module, this module always returns pong‘ on successful contact. It does not make sense in playbooks, but it is useful from/usr/bin/ansible‘
4、# Test ‘webservers‘ status
5、ansible webservers -m ping

centos7.3 安裝ansible