自動(dòng)化運(yùn)維工具 Ansible

一、Ansible介紹

1. 自動(dòng)化運(yùn)維工具對(duì)比

    1. Puppet:基于 Ruby 開(kāi)發(fā),采用 C/S 架構(gòu),擴(kuò)展性強(qiáng),基于 SSL,遠(yuǎn)程命令執(zhí)行相對(duì)較弱
    1. SaltStack:基于 Python 開(kāi)發(fā),采用 C/S 架構(gòu),相對(duì) puppet 更輕量級(jí),配置語(yǔ)法使用 YAML,使得配置腳本更簡(jiǎn)單.需要配置客戶端以及服務(wù)器端。每臺(tái)被控制節(jié)點(diǎn)需要安裝agent
    1. Ansible:基于 Python開(kāi)發(fā),分布式,無(wú)需客戶端,輕量級(jí),配置語(yǔ)法使用YAML語(yǔ)言,更強(qiáng)的遠(yuǎn)程命令執(zhí)行操作

2. Ansible簡(jiǎn)介

ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,基于Python開(kāi)發(fā),分布式,無(wú)需客戶端,輕量級(jí),實(shí)現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能,ansible是基于模塊工作的,本身沒(méi)有批量部署的能力。真正具有批量部署的是ansible所運(yùn)行的模塊,ansible只是提供一種框架。

Ansible特性:

  • no agents:不需要在被管控主機(jī)上安裝任何客戶端,更新時(shí),只需在操作機(jī)上進(jìn)行一次更新即可(不用安裝客戶端。分布式的)
  • no server:無(wú)服務(wù)器端,使用時(shí)直接運(yùn)行命令即可
  • modules in any languages:基于模塊工作,可使用任意語(yǔ)言開(kāi)發(fā)模塊
  • yaml,not code:使用yaml語(yǔ)言定制劇本playbook
  • ssh by default:基于SSH工作
  • strong multi-tier solution:可實(shí)現(xiàn)多級(jí)指揮
image.png

圖片解釋:

  • connection plugins:連接插件,負(fù)責(zé)和被監(jiān)控端實(shí)現(xiàn)通信,默認(rèn)使用SSH連接
  • host inventory:主機(jī)清單,是一個(gè)配置文件里面定義監(jiān)控的主機(jī)
  • modules : 模塊,核心模塊、command模塊、自定義模塊等
  • plugins : modules功能的補(bǔ)充,包括連接插件,郵件插件等
  • playbook:編排,定義 Ansible 多任務(wù)配置文件,非必需

二、Ansible安裝

1. 準(zhǔn)備環(huán)境----關(guān)閉防護(hù)墻和selinux

環(huán)境:
主機(jī):4臺(tái)  一個(gè)控制節(jié)點(diǎn) 3個(gè)被控制節(jié)點(diǎn)
解析:本地互相解析(所有機(jī)器)
# vim /etc/hosts
192.168.181.128 ansible-web1
192.168.181.129 ansible-web2
192.168.181.130 ansible-web3
192.168.181.146 ansible-server  (控制節(jié)點(diǎn)服務(wù)器端)
配置ssh公鑰認(rèn)證:控制節(jié)點(diǎn)需要發(fā)送ssh公鑰給所有被控制節(jié)點(diǎn)
[root@ansible-server ~]# ssh-keygen

[root@ansible-server ~]# ssh-copy-id -i ansible-web1     #所有被控機(jī)器

[root@ansible-server ~]# ssh ansible-web1     #手動(dòng)連接一遍所有被控機(jī)器

所有機(jī)器:
systemctl stop firewalld && setenforce 0

2. 控制節(jié)點(diǎn)安裝ansible

 1. 配置EPEL網(wǎng)絡(luò)yum源(直接第二步 沒(méi)有ansible再執(zhí)行這一步)
 [root@ansible-server ~]# yum install -y epel-release
 2. 安裝ansible
 [root@ansible-server ~]# yum install -y ansible
 3.查看版本
 [root@ansiable-server ~]# ansible --version
 4.查看幫助
 [root@ansible-server ~]# ansible --help

3. ansible基礎(chǔ)----inventory主機(jī)清單

inventory文件通常用于定義要管理主機(jī)的認(rèn)證信息,例如ssh登錄用戶名、密碼以及key相關(guān)信息。
官方文檔

#查看配置文件:
[root@ansible-server ~]# rpm  -qc ansible       #-q:---query查詢
/etc/ansible/ansible.cfg       #主要設(shè)置一些ansible初始化的信息,比如日志存放路徑、模塊、等配置信息
/etc/ansible/hosts             #被控服務(wù)器的信息,默認(rèn)位置為/etc/ansible/hosts

#添加主機(jī)或者主機(jī)組:
[root@ansible-server ~]# vim /etc/ansible/hosts    #在最后追加被管理端的機(jī)器
[ansible-webs]
ansible-web1
ansible-web2
ansible-web3

-------------------------------例----------------------------------
#添加主機(jī)
ansible-web1          #可以使用主機(jī)名稱或IP地址

#添加主機(jī)組:
[webservers]      #使用[]標(biāo)簽指定主機(jī)組 ----標(biāo)簽自定義
192.168.181.129     #如果未解析添加ip
ansible-web2      #解析添加主機(jī)名

#組可以包含其他組:
[webservers1]     #組一
ansible-web1
[webservers2]     #組二
ansible-web2
[weball:children]      #weball組包括兩個(gè)子組,children--照寫(xiě)
webservers1        #組一
webservers2        #組二
------------------------------------------------------------------

#為一個(gè)組指定變量,組內(nèi)每個(gè)主機(jī)都可以使用該變量:
[ansible-webs:vars]          #設(shè)置ansible-webs組變量,vars--照寫(xiě)
ansible_ssh_port=22          #端口
ansible_ssh_user=root        #遠(yuǎn)程用戶
ansible_ssh_private_key_file=/root/.ssh/id_rsa      #私鑰位置
#ansible_ssh_pass=1234          #也可以定義密碼,如果沒(méi)有互傳秘鑰可以使用密碼。
Ansible Inventory 常見(jiàn)的內(nèi)置參數(shù):
image.png
查看組內(nèi)主機(jī)列表:

語(yǔ)法:ansible 組名 --list-hosts

[root@ansible-server ~]# ansible ansible-webs --list-hosts
 hosts (3):
   ansible-web1
   ansible-web2
   ansible-web3
擴(kuò)展:自定義主機(jī)列表使用密碼登錄:
[root@ansible-server ~]# vim /opt/hostlist
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root    #被控服務(wù)器賬號(hào)
#ansible_ssh_private_key_file=/root/.ssh/id_rsa
ansible_ssh_pass=test    #被控服務(wù)器密碼

[all]
ansible-web1
ansible-web2
ansible-web3

#使用:
[root@ansible-server ~]# ansible -i /opt/hostlist all -m ping -o
小注釋:如果不通,手動(dòng)連接第一次,第一次需要手動(dòng)輸入密碼。"第一次"
-i:指定清單文件
注意:這里的ping并不是真正意義上的ping而是探測(cè)遠(yuǎn)程主機(jī)ssh是否可以連接!判斷ssh端口是否存活

4. 測(cè)試

語(yǔ)法:
ansible <pattern> -m <module_name> -a <arguments>
pattern:主機(jī)清單里定義的主機(jī)組名,主機(jī)名,IP,別名等,all表示所有的主機(jī),支持通配符,正則
-m module_name:模塊名稱,默認(rèn)為command
-a arguments:傳遞給模塊的參數(shù)
-o :橫著顯示(單行顯示)

示例:
#使用ping模塊檢查ansible節(jié)點(diǎn)的連通性:
#指定單臺(tái)機(jī)器
[root@ansible-server ~]# ansible ansible-web1 -m ping -o
ansible-web1 | SUCCESS => {"changed": false, "ping": "pong"}

#指定多臺(tái)機(jī)器
[root@ansible-server ~]# ansible ansible-web1,ansible-web2 -m ping -o
ansible-web1 | SUCCESS => {"changed": false, "ping": "pong"}
ansible-web2 | SUCCESS => {"changed": false, "ping": "pong"}

#指定組
[root@ansible-server ~]# ansible ansible-webs -m ping -o
ansible-web2 | SUCCESS => {"changed": false, "ping": "pong"}
ansible-web1 | SUCCESS => {"changed": false, "ping": "pong"}
ansible-web3 | SUCCESS => {"changed": false, "ping": "pong"}
-------------------------------------------------------------------------------------------

#執(zhí)行shell命令:
[root@ansible-server ~]# ansible ansible-web1 -m shell -a 'uptime' -o
ansible-web1 | SUCCESS | rc=0 | (stdout)  21:28:25 up  2:53,  3 users,  load average: 0.16, 0.05, 0.06

#不加 -m  默認(rèn)是 command 模塊
[root@ansible-server ~]# ansible ansible-web1 -a 'uptime' -o
ansible-web1 | SUCCESS | rc=0 | (stdout)  21:29:07 up  2:54,  3 users,  load average: 0.08, 0.04, 0.05

#給節(jié)點(diǎn)增加用戶:
[root@ansible-server ~]# ansible ansible-web1 -a 'useradd tom'
ansible-web1 | SUCCESS | rc=0 | (stdout)

[root@ansible-web1 ~]# id tom
uid=1000(tom) gid=1000(tom) 組=1000(tom)

[root@ansible-server ~]# ansible ansible-web1 -a 'grep tom /etc/passwd' -o
ansible-web1 | SUCCESS | rc=0 | (stdout) tom:x:1000:1000::/home/tom:/bin/bash

#重定向輸出到本地文件中:
[root@ansible-server ~]# ansible ansible-web1 -a 'df -Th' > a.txt

[root@ansible-server ~]# cat a.txt 
ansible-web1 | SUCCESS | rc=0 >>
文件系統(tǒng)                類型      容量  已用  可用 已用% 掛載點(diǎn)
/dev/mapper/centos-root xfs       8.0G  2.2G  5.9G   27% /
devtmpfs                devtmpfs  226M     0  226M    0% /dev
tmpfs                   tmpfs     237M     0  237M    0% /dev/shm
tmpfs                   tmpfs     237M  4.7M  232M    2% /run
tmpfs                   tmpfs     237M     0  237M    0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  125M  890M   13% /boot
tmpfs                   tmpfs      48M     0   48M    0% /run/user/0

5. Ad-Hoc

ad hoc就是執(zhí)行簡(jiǎn)單的命令 (一條命令) 。對(duì)于復(fù)雜的命令則為 playbook。

列出ansible支持的模塊:
-l:獲取列表
-s module_name:獲取指定模塊的使用信息

#看所有模塊(A10,華為,docker,EC2,aws等等廣大廠商設(shè)備)
[root@ansible-server ~]# ansible-doc -l

#查看模塊使用信息,了解其功能:
[root@ansible-server ~]# ansible-doc -s yum
常用模塊
1. 遠(yuǎn)程復(fù)制備份 copy 模塊

模塊參數(shù)詳解:
src=:指定源文件路徑
dest=:目標(biāo)地址(拷貝到哪里)
owner=:指定屬主
group=:指定屬組
mode=:指定權(quán)限,可以以數(shù)字指定比如0644
backup=:在覆蓋之前將原文件備份,備份文件包含時(shí)間信息。有兩個(gè)選項(xiàng):yes|no

[root@ansible-server ~]# vim test.txt     #創(chuàng)建一個(gè)測(cè)試文件
[root@ansible-server ~]# cat test.txt 
test

#將 server 下 /root/test.txt 拷貝到 web2/root/test.txt 屬主屬組root 權(quán)限644
[root@ansible-server ~]# ansible ansible-web2 -m copy -a 'src=/root/test.txt dest=/root owner=root group=root mode=644' -o
ansible-web2 | SUCCESS => {"changed": false, "checksum": "016c49988e9afc97ab7fe8126867a986e20432f8", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/root/test.txt", "secontext": "system_u:object_r:admin_home_t:s0", "size": 11, "state": "file", "uid": 0}

[root@ansible-server ~]# vim test.txt      #追加如下內(nèi)容
[root@ansible-server ~]# cat test.txt 
test
ceshi

#將源文件先備份在拷貝
[root@ansible-server ~]# ansible ansible-web2 -m copy -a 'src=/root/test.txt dest=/root owner=root group=root mode=644 backup=yes'
ansible-web2 | SUCCESS => {
    "backup_file": "/root/test.txt.12190.2020-03-19@16:34:42~", 
    "changed": true, 
    "checksum": "016c49988e9afc97ab7fe8126867a986e20432f8", 
    "dest": "/root/test.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "b1b8d0a109a1ed187e314557b35655b2", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 11, 
    "src": "/root/.ansible/tmp/ansible-tmp-1584606879.85-10069951504721/source", 
    "state": "file", 
    "uid": 0
}

[root@ansible-web2 ~]# ls      #查看web2
anaconda-ks.cfg  test.txt  test.txt.12190.2020-03-19@16:34:42~
[root@ansible-web2 ~]# cat test.txt
test
ceshi
[root@ansible-web2 ~]# cat test.txt.12190.2020-03-19\@16\:34\:42~ 
test

注意:如果文件沒(méi)有變化,不會(huì)備份。只有文件內(nèi)容不同,才會(huì)做備份。

#移動(dòng)被控制節(jié)點(diǎn)的文件
[root@ansible-server ~]# ansible ansible-web2 -a 'mv /root/test.txt /opt'
ansible-web2 | SUCCESS | rc=0 | (stdout)

[root@ansible-web2 ~]# cat /opt/test.txt 
test
ceshi
2. 軟件包管理 yum 模塊

模塊參數(shù)詳解:
state=:狀態(tài)是什么,干什么
state=absent:remove安裝包
state=latest:最新的(安裝)
state=removed:卸載

#安裝tree
[root@ansible-server ~]# ansible ansible-web3 -m yum -a "name=tree state=latest"

#查看
[root@ansible-web3 ~]# tree
.
└── anaconda-ks.cfg

0 directories, 1 file

#卸載tree
[root@ansible-server ~]# ansible ansible-web3 -m yum -a "name=tree state=removed"
或者
[root@ansible-server ~]# ansible ansible-web3 -m yum -a "name=tree state=absent"

#查看
[root@ansible-web3 ~]# tree
-bash: /usr/bin/tree: 沒(méi)有那個(gè)文件或目錄
3. 服務(wù)管理 service 模塊

模塊參數(shù)詳解:
state=:狀態(tài)是什么,干什么
state=started:?jiǎn)?dòng)
state=stopped:停止
state=restarted:重啟
enabled=yes:開(kāi)機(jī)自啟
enabled=no:開(kāi)機(jī)不自啟

#啟動(dòng)nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx state=started"

#停止nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx state=stopped"

#重啟nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx state=restarted"

#開(kāi)機(jī)自啟nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx enabled=yes"

#開(kāi)機(jī)不自啟nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx enabled=no"
4. 文件 file 模塊

模塊參數(shù)詳解:
owner=:修改屬主
group=:修改屬組
mode=:修改權(quán)限
path=:要修改文件的路徑
recurse=:遞歸的設(shè)置文件的屬性,只對(duì)目錄有效,兩個(gè)選項(xiàng):yes|no
state=touch:創(chuàng)建一個(gè)新的空文件
state=directory:創(chuàng)建一個(gè)新的目錄,當(dāng)目錄存在時(shí)不會(huì)進(jìn)行修改

#在 web1 的/root下創(chuàng)建 目錄chen 權(quán)限777 屬主屬組chen
[root@ansible-server ~]# ansible ansible-web1 -m file -a 'path=/root/chen state=directory mode=777 owner=chen group=chen'

#在 web1 的/root/chen下創(chuàng)建 文件chen.txt 權(quán)限777 屬主屬組chen
[root@ansible-server ~]# ansible ansible-web1 -m file -a 'path=/root/chen/chen.txt state=touch mode=777 owner=chen group=chen'

#查看
[root@ansible-web1 ~]# ll
總用量 0
drwxrwxrwx. 2 chen chen 22 3月  20 00:33 chen
[root@ansible-web1 ~]# ll chen
總用量 0
-rwxrwxrwx. 1 chen chen 0 3月  20 00:33 chen.txt

#遞歸更改 web1 /root/chen目錄 權(quán)限755 屬主屬組root
[root@ansible-server ~]# ansible ansible-web1 -m file -a 'path=/root/chen owner=root group=root mode=755 state=directory rec
urse=yes'

#查看
[root@ansible-web1 ~]# ll
總用量 0
drwxr-xr-x. 2 root root 22 3月  20 00:33 chen
[root@ansible-web1 ~]# ll chen
總用量 0
-rwxr-xr-x. 1 root root 0 3月  20 00:33 chen.txt
5. 收集信息 setup 模塊

模塊參數(shù)詳解:
filter=:過(guò)濾信息

#收集所有信息
[root@ansible-server ~]# ansible ansible-web1 -m setup

#收集 web1 的ip
[root@ansible-server ~]# ansible ansible-web1 -m setup -a 'filter=ansible_all_ipv4_addresses'
ansible-web1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.181.128"
        ]
    }, 
    "changed": false
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容