1. 程式人生 > >走進自動化運維之Ansible服務部署,附帶(參數及模塊)詳解!

走進自動化運維之Ansible服務部署,附帶(參數及模塊)詳解!

輸入 epel源 檢測 鏈接文件 日誌輸出 運維 介紹 講解 book

何為Ansible:

簡單的自動化運維管理工具,不需要配置代理工具,基於Python研發。

Ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。

自動化運維工具“三劍客”:
工具 開發語言 結構 配置文件格式 運行任務
Ansible Python YAML 支持命令行
SaltStack Python C/S YAML 支持命令行
Puppet Ruby C/S Ruby語法格式 通過模塊實現

ansible基本架構的6個部分:

技術分享圖片

)Ansible:核心引擎

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

3)connection plugins(鏈接插件):負責和被監控端實現通信

4)playbooks(yaml):劇本執行多個任務時,非必需可以讓節點一次性運行多個任務

5)core modules(核心模塊):各種模塊核心模塊、command模塊、自定義模塊

6)custom modules(自定義模塊):借助於插件完成記錄日誌郵件等功能

優點:

1)輕量級,無需在客戶端安裝agent,更新時,只需在操作機上進行一次更新即可

2)批量任務執行可以寫成腳本,而且不用分發到遠程就可以執行

3)使用python編寫,維護更簡單,ruby語法過於復雜

4)支持sudo

安裝部署Ansible服務:

1)案列環境

角色 主機名 ip地址
控制主機 web 1 192.168.200.130
被管理主機 web 2 192.168.200.136
被管理主機 web 3 192.168.200.134

2)安裝ansible包和epel源包

[root@web1 ansible]# systemctl stop firewalld.service 
[root@web1 ansible]# setenforce 0 //關閉防火墻和網絡安全性增強功能

[root@web1 ~]# yum install epel-release ansible -y

[root@web1 ~]# ansible --version  //查看版本信息
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u‘/root/.ansible/plugins/modules‘, u‘/usr/share/ansible/plugins/modules‘]
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5  

[root@web1 ~]# yum install tree -y  

[root@web1 ~]# tree /etc/ansible/   //樹狀結構展示文件夾
/etc/ansible/
├── ansible.cfg    //配置文件
├── hosts          //主機清單文件
└── roles          //角色
1 directory, 2 files

3)配置主機清單

[root@web1 ~]# cd /etc/ansible/
[root@web1 ansible]# ls
ansible.cfg  hosts  roles

[root@web1 ansible]# vim hosts
[web1]
192.168.200.136
[web2]              //被管理主機分類
192.168.200.134

技術分享圖片

4)設置SSH無密碼登陸

[root@web1 ansible]# ssh-keygen -t rsa

[root@web1 ansible]# ssh-copy-id [email protected]    
[root@web1 ansible]# ssh-copy-id [email protected]
//配置密鑰對驗證,兩臺都需配置

技術分享圖片

5)免交互代理

[root@web1 ansible]# ssh-agent bash
[root@web1 ansible]# ssh-add
Enter passphrase for /root/.ssh/id_rsa:  //輸入之前設置的密碼
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

6)測試是否配置成功

[root@web1 ansible]# ansible all -a‘date‘
192.168.200.136 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 23:48:24 CST

192.168.200.134 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 23:48:24 CST
//可以看見兩臺被管理主機的時間都已顯示,配置成功

註:只需配置控制主機,被管理主機無需任何配置

Ansible配置文件及相關參數:

主配置文件:/etc/ansible/ansible.cfg

默認主機清單:/etc/ansible/hosts

參數:

-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:日誌輸出到該目錄,日誌文件名以主機名命名

Ansible的常用模塊(用ansible-doc -l可以顯示):

語法:

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

-f forks:啟動的並發數

-m module_name:使用的模塊

-args:模塊特有參數
1.command模塊:

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

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

192.168.200.114 | SUCCESS | rc=0 >>
2018年 08月 1日 星期三 17:09:53 CST

192.168.200.113 | SUCCESS | rc=0 >>
2018年 08月 1日 星期三 17:09:54 CST

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

192.168.200.113 | SUCCESS | rc=0 >>
2018年 08月 1日 星期三 17:11:02 CST

192.168.200.114 | SUCCESS | rc=0 >>
2018年 08月 1日 星期三 17:11:02 CST
2.cron模塊:

兩種狀態:1)present表示添加(默認使用),absent表示刪除

服務端:

創建一個任務計劃:
ansible testhost -m cron -a ‘name=test cron job=‘/bin/touch /tmp/1212.txt‘ weekday=6‘

如果刪除該cron只需要加一個字段state=absent

ansible testhost -m cron -a ‘name=test cron state=absent‘

時間表示:
分:minute
時:hour
日:day
月:month
周:weekday

客戶端:

查看任務計劃:
crontab -l
3.user模塊:

1)創建用戶:

ansible webserver -m user -a‘name=test01‘

192.168.200.136 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1001, 
    "home": "/home/test01", 
    "name": "test01", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001

2)刪除用戶:

ansible webserver -m user -a ‘name=test01 state=absent‘

192.168.200.136 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "test01", 
    "remove": false, 
    "state": "absent"
4.group模塊:

1)創建mysql組

ansible webserver -m group -a ‘name=mysql gid=306 system=yes‘

192.168.200.136 | SUCCESS => {
    "changed": true, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}

2)將mysql用戶添加到mysql組

ansible webserver -m user -a ‘name=mysql uid=306 group=mysql system=yes‘     //這裏模塊是用戶

192.168.200.136 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 306, 
    "home": "/home/mysql", 
    "name": "mysql", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 306
5.copy模塊:

本地/root/aaa復制到目標主機/tmp/aaa

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

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

設置文件屬性,使用path指定文件路徑,使用src定義源文件路徑,dest來代替源文件的符號鏈接

1)創建文件

ansible webserver -m file -a‘owner=mysql group=mysql mode=600 path=/opt/test.txt‘

192.168.200.136 | SUCCESS => {
    "changed": true, 
    "gid": 306, 
    "group": "mysql", 
    "mode": "0600", 
    "owner": "mysql", 
    "path": "/opt/test.txt", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 12, 
    "state": "file", 
    "uid": 306

2)設置test.txt的鏈接文件

ansible webserver -m file -a‘path=/opt/test.txt src=/opt/test.txt state=link‘

192.168.200.136 | FAILED! => {
    "changed": false, 
    "gid": 306, 
    "group": "mysql", 
    "mode": "0777", 
    "msg": "refusing to convert from file to symlink for /opt/test.txt", 
    "owner": "mysql", 
    "path": "/opt/test.txt", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 12, 
    "state": "file", 
    "uid": 306
7.ping模塊:

檢測制定主機的連通性

ansible webserver -m ping 

192.168.200.136 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
8.service模塊:

控制管理服務的運行狀態:使用enabled表示是否開機自啟;使用state指定服務狀態

查看兩臺主機的80端口,再啟動

ansible all -m shell -a ‘ss -tnl |grep :80 ‘‘

ansible all -m service -a ‘name=httpd state=started‘
9.shell模塊
首先創建一個shell腳本:
vim /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_test.txt

然後把該腳本分發到各個機器上

ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"

最後是批量執行該shell腳本

ansible testhost -m shell -a "/tmp/test.sh"

shell模塊,還支持遠程執行命令並且帶管道:

ansible testhost -m shell -a "cat /etc/passwd | wc -l"
10.script模塊:

可以將本地腳本復制到被管理主機上運行,需要註意用相對路徑來指定腳本

編輯本地腳本test.sh,輔助到被管理主機上運行

控制機:
[root@localhost ~]# vim test.sh
#!/bin/bash
echo "hello ansible from script"> /opt/script.txt

[root@localhost ~]# chmod +x test.sh 
[root@localhost ~]# ansible webserver -m script -a‘test.sh‘

192.168.200.136 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.200.136 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.200.136 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
    }
客戶機:
[root@localhost opt]# cat script.txt 
hello ansible from script
11.yum模塊:
安裝httpd:

ansible testhost -m yum -a ‘name=httpd‘

在name後面還可以加上state=installed,默認不加也可以安裝

開啟httpd服務,並設為開機啟動:

ansible testhost -m service -a ‘name=httpd state=started enabled=yes‘

這裏的name是centos系統裏的服務名,可以通過chkconfig --list查看。

ansible文檔的使用:

ansible-doc -l查看所有的模塊

ansible-doc cron查看指定的模塊
12.setup模塊:

收集和查看被管理主機的相關設備信息

ansible webserver -m setup

Ansible服務的安裝和命令應用基礎就介紹到此,相關內容下一篇繼續講解,感謝各位道友瀏覽!

走進自動化運維之Ansible服務部署,附帶(參數及模塊)詳解!