溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

自動(dòng)化運(yùn)維工具ansible——安裝及模塊管理

發(fā)布時(shí)間:2020-02-27 22:49:45 來(lái)源:網(wǎng)絡(luò) 閱讀:544 作者:一拳超人007 欄目:云計(jì)算

ansible簡(jiǎn)介

?ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,基于Python開(kāi)發(fā),集合了眾多運(yùn)維工具(puppet、cfengine、chef、func、fabric)的優(yōu)點(diǎn),實(shí)現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能。

自動(dòng)化運(yùn)維工具ansible——安裝及模塊管理

?ansible是基于模塊工作的,本身沒(méi)有批量部署的能力。真正具有批量部署的是ansible所運(yùn)行的模塊,ansible只是提供一種框架。主要包括:
(1)、連接插件connection plugins:負(fù)責(zé)和被監(jiān)控端實(shí)現(xiàn)通信;
(2)、host inventory:指定操作的主機(jī),是一個(gè)配置文件里面定義監(jiān)控的主機(jī);
(3)、各種模塊核心模塊、command模塊、自定義模塊;
(4)、借助于插件完成記錄日志郵件等功能;
(5)、playbook:劇本執(zhí)行多個(gè)任務(wù)時(shí),非必需可以讓節(jié)點(diǎn)一次性運(yùn)行多個(gè)任務(wù)。

ansible的架構(gòu)

連接其他主機(jī)默認(rèn)使用ssh協(xié)議

自動(dòng)化運(yùn)維工具ansible——安裝及模塊管理

ansible core核心引擎:即ansible本身
host inventory主機(jī)清單:用來(lái)定義ansible所管理主機(jī),默認(rèn)是在ansible的hosts配置文件中定義被管理主機(jī),同時(shí)也支持自定義動(dòng)態(tài)主機(jī)清單和指定其他配置文件的位置
connect plugin連接插件:負(fù)責(zé)和被管理主機(jī)實(shí)現(xiàn)通信,除支持使用SSH連接被管理主機(jī)外,ansible還支持其他的連接方式,所有需要有連接插件將各個(gè)主機(jī)用連接插件連接到ansible
playbook劇本:用來(lái)集中定義ansible任務(wù)的配置文件,即將多個(gè)任務(wù)定義在一個(gè)劇本中由ansible自動(dòng)執(zhí)行,可以由控制主機(jī)針對(duì)多臺(tái)被管理主機(jī)同時(shí)運(yùn)行多個(gè)任務(wù)
core modules核心模塊:是ansible自帶的模塊,使用這些模塊將資源分發(fā)到被管理主機(jī)使其執(zhí)行特定任務(wù)或匹配特定的狀態(tài)
custom modules自定義模塊:用于完成模塊功能的補(bǔ)充,可借助相關(guān)插件完成記錄日志,發(fā)送郵件等功能

實(shí)驗(yàn)環(huán)境

控制主機(jī) 192.168.13.128
被管理主機(jī) 192.168.13.129
被管理主機(jī) 192.168.13.130

一,ansible的安裝(在控制主機(jī)上)

1,安裝ansible服務(wù)

[root@promote ~]# systemctl stop firewalld.service   ##關(guān)閉所有主機(jī)的防火墻
[root@promote ~]# setenforce 0
[root@promote ~]# yum install epel-release -y   ##安裝epel源
[root@promote ~]# yum install ansible -y   ##安裝ansible服務(wù)
[root@promote ~]# yum install tree -y
[root@promote ~]# tree /etc/ansible/   ##查看ansible屬性結(jié)構(gòu)
/etc/ansible/
├── ansible.cfg  ##配置文件
├── hosts    ##主機(jī)清單
└── roles

2,編輯hosts主機(jī)清單

[root@promote ~]# vim /etc/ansible/hosts   ##編輯ansible主機(jī)清單
[webserver]
192.168.13.129  ##web的主機(jī)地址
[mysql]
192.168.13.130  ##mysql的主機(jī)地址

3,生成秘鑰對(duì),推送

[root@promote ~]# ssh-keygen -t rsa   ##生成秘鑰對(duì)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   ##回車(chē)
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):    ##輸入密碼
Enter same passphrase again:    ##確認(rèn)密碼
[root@promote ~]# ls .ssh/   ##查看秘鑰
id_rsa  id_rsa.pub
[root@promote ~]# ssh-copy-id root@192.168.13.129   ##上傳秘鑰到后面的服務(wù)器上
[root@promote ~]# ssh-copy-id root@192.168.13.130   

4,使用ansible命令行執(zhí)行

[root@promote ~]# ansible webserver -m command -a 'date'  ##使用ansible命令行模塊執(zhí)行date
Enter passphrase for key '/root/.ssh/id_rsa':   ##輸入秘鑰密碼
192.168.13.129 | CHANGED | rc=0 >>
2020年 01月 23日 星期三 23:57:16 CST

[root@promote ~]# ansible mysql -m command -a 'date'         
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.13.130 | CHANGED | rc=0 >>
2020年 01月 23日 星期三 23:57:38 CST

[root@promote ~]# ssh-agent bash  ##免交互代理
[root@promote ~]# ssh-add    ##添加
Enter passphrase for /root/.ssh/id_rsa:   ##輸入秘鑰
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
[root@promote ~]# ansible webserver -m command -a 'date'   ##繼續(xù)執(zhí)行命令行模塊實(shí)現(xiàn)免交互
192.168.13.129 | CHANGED | rc=0 >>
2020年 01月 23日 星期三 23:58:26 CST

[root@promote ~]# ansible mysql -m command -a 'date'         
192.168.13.130 | CHANGED | rc=0 >>
2020年 01月 23日 星期三 23:58:39 CST

二,ansible模塊管理

1,command命令行模塊

[root@promote ~]# ansible all -a 'date'
192.168.13.130 | CHANGED | rc=0 >>
2020年 01月 30日 星期四 00:17:02 CST

192.168.13.129 | CHANGED | rc=0 >>
2020年 01月 30日 星期四 00:17:02 CST
[root@promote ~]# ansible all -a 'ls /'  ##查看后兩臺(tái)主機(jī)的根目錄
##如果不加-m模塊,則默認(rèn)運(yùn)行command模塊all是所有主機(jī)

2,cron計(jì)劃性任務(wù)模塊

[root@promote ~]# ansible-doc -s cron  ##查看cron模塊信息
[root@promote ~]# ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo haha" name="test haha"'
##選擇cron模塊指定時(shí)間,工作內(nèi)容,名稱
192.168.13.129 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "envs": [], 
        "jobs": [
                "test haha"
        ]
}
[root@promote ~]# ansible webserver -a 'crontab -l'   ##執(zhí)行命令行查看計(jì)劃性任務(wù)
192.168.13.129 | CHANGED | rc=0 >>
#Ansible: test haha
*/1 * * * * /usr/bin/echo haha

[root@promote ~]# ansible webserver -m cron -a 'name="test haha" state=absent' ##移除計(jì)劃性任務(wù)
192.168.13.129 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "envs": [], 
        "jobs": []
}

3,user模塊(請(qǐng)求的是useradd,userdel,usermod三個(gè)指令)

[root@promote ~]# ansible-doc -s user  ##查看user模塊信息
[root@promote ~]# ansible all -m user -a 'name=test'  ##給所有主機(jī)創(chuàng)建test用戶
192.168.13.129 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "comment": "", 
        "create_home": true, 
        "group": 1001, 
        "home": "/home/test", 
        "name": "test", 
        "shell": "/bin/bash", 
        "state": "present", 
        "system": false, 
        "uid": 1001
}
192.168.13.130 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "comment": "", 
        "create_home": true, 
        "group": 1001, 
        "home": "/home/test", 
        "name": "test", 
        "shell": "/bin/bash", 
        "state": "present", 
        "system": false, 
        "uid": 1001
}
[root@promote ~]# ansible webserver -m user -a 'name=test state=absent' 
##刪除webserver中test用戶
192.168.13.129 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "force": false, 
        "name": "test", 
        "remove": false, 
        "state": "absent"
}

4,group模塊(請(qǐng)求的是groupadd,groupdel,groupmod三個(gè)指令)

[root@promote ~]# ansible mysql -m group -a 'name=mysql gid=306 system=yes' 
##創(chuàng)建mysql系統(tǒng)組
192.168.13.130 | SUCCESS => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "gid": 306, 
        "name": "mysql", 
        "state": "present", 
        "system": true
}
[root@promote ~]# ansible mysql -a 'tail -1 /etc/group'  ##查看創(chuàng)建的情況
192.168.13.130 | CHANGED | rc=0 >>
mysql:x:306:

[root@promote ~]# ansible mysql -m user -a 'name=test02 uid=306 group=mysql system=yes'
##創(chuàng)建系統(tǒng)用戶test02并加入到mysql組中
192.168.13.130 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "comment": "", 
        "create_home": true, 
        "group": 306, 
        "home": "/home/test02", 
        "name": "test02", 
        "shell": "/bin/bash", 
        "state": "present", 
        "system": true, 
        "uid": 306
}
[root@promote ~]# ansible mysql -a 'id test02'   ##查看系統(tǒng)用戶test02的信息                                
192.168.13.130 | CHANGED | rc=0 >>
uid=306(test02) gid=306(mysql) 組=306(mysql)

5,copy模塊

[root@promote ~]# ansible-doc -s copy  ##copy模塊的信息
[root@promote ~]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=644'
##復(fù)制源到目標(biāo),屬組和文件權(quán)限
192.168.13.130 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "checksum": "0725780c6841b5cae04ba31a054b6090d701bc19", 
        "dest": "/opt/fstab.bak", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "4a95e64f6c25098ca5e0613c5283e8f1", 
        "mode": "0644", 
        "owner": "root", 
        "secontext": "system_u:object_r:usr_t:s0", 
        "size": 595, 
        "src": "/root/.ansible/tmp/ansible-tmp-1580550278.09-89338211954459/source", 
        "state": "file", 
        "uid": 0
}
[root@promote ~]# ansible mysql -a 'ls -l /opt'   ##查看是否復(fù)制成功
192.168.13.130 | CHANGED | rc=0 >>
總用量 4
-rw-r--r--. 1 root root 595 2月   1 17:44 fstab.bak
drwxr-xr-x. 2 root root   6 3月  26 2015 rh
[root@promote ~]# ansible mysql -m copy -a 'content="hello!" dest=/opt/test.txt'
##用copy進(jìn)行寫(xiě)入文件內(nèi)容
192.168.13.130 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "checksum": "8f7d88e901a5ad3a05d8cc0de93313fd76028f8c", 
        "dest": "/opt/test.txt", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "5a8dd3ad0756a93ded72b823b19dd877", 
        "mode": "0644", 
        "owner": "root", 
        "secontext": "system_u:object_r:usr_t:s0", 
        "size": 6, 
        "src": "/root/.ansible/tmp/ansible-tmp-1580550521.27-190936730009060/source", 
        "state": "file", 
        "uid": 0
}
[root@promote ~]# ansible mysql -a 'cat /opt/test.txt'   ##查看寫(xiě)入的文件內(nèi)容
192.168.13.130 | CHANGED | rc=0 >>
hello!

6,file模塊(文件屬性)

[root@promote ~]# ansible mysql -m file -a 'path=/opt/test.txt owner=test02 group=mysql mode=666'
##指定文件的屬主,屬組,文件的權(quán)限
192.168.13.130 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "gid": 306, 
        "group": "mysql", 
        "mode": "0666", 
        "owner": "test02", 
        "path": "/opt/test.txt", 
        "secontext": "system_u:object_r:usr_t:s0", 
        "size": 6, 
        "state": "file", 
        "uid": 306
}
[root@promote ~]# ansible mysql -a 'ls -l /opt/test.txt'  ##查看文件的屬性
192.168.13.130 | CHANGED | rc=0 >>
-rw-rw-rw-. 1 test02 mysql 6 2月   1 17:48 /opt/test.txt
[root@promote ~]# ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'
##創(chuàng)建鏈接性文件
192.168.13.130 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dest": "/opt/test.txt.link", 
        "gid": 0, 
        "group": "root", 
        "mode": "0777", 
        "owner": "root", 
        "secontext": "unconfined_u:object_r:usr_t:s0", 
        "size": 13, 
        "src": "/opt/test.txt", 
        "state": "link", 
        "uid": 0
}
[root@promote ~]# ansible mysql -a 'ls -l /opt/'          ##查看文件的屬性                         
192.168.13.130 | CHANGED |   rc=0 >>
總用量 8
-rw-r--r--. 1 root   root  595 2月   1 17:44 fstab.bak
drwxr-xr-x. 2 root   root    6 3月  26 2015 rh
-rw-rw-rw-. 1 test02 mysql   6 2月   1 17:48 test.txt
lrwxrwxrwx. 1 root   root   13 2月   1 17:55 test.txt.link -> /opt/test.txt
[root@promote ~]# ansible mysql -m file -a 'path=/opt/abc.txt state=touch'  ##創(chuàng)建一個(gè)空文件
192.168.13.130 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dest": "/opt/abc.txt", 
        "gid": 0, 
        "group": "root", 
        "mode": "0644", 
        "owner": "root", 
        "secontext": "unconfined_u:object_r:usr_t:s0", 
        "size": 0, 
        "state": "file", 
        "uid": 0
}
[root@promote ~]# ansible mysql -a 'ls -l /opt/'   ##查看創(chuàng)建情況
192.168.13.130 | CHANGED | rc=0 >>
總用量 8
-rw-r--r--. 1 root   root    0 2月   1 17:57 abc.txt
-rw-r--r--. 1 root   root  595 2月   1 17:44 fstab.bak
drwxr-xr-x. 2 root   root    6 3月  26 2015 rh
-rw-rw-rw-. 1 test02 mysql   6 2月   1 17:48 test.txt
lrwxrwxrwx. 1 root   root   13 2月   1 17:55 test.txt.link -> /opt/test.txt
[root@promote ~]# ansible mysql -m file -a 'path=/opt/abc.txt state=absent'   ##刪除文件    
192.168.13.130 | CHANGED => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "path": "/opt/abc.txt", 
        "state": "absent"
}
[root@promote ~]# ansible mysql -a 'ls -l /opt/'           ##查看文件的信息                
192.168.13.130 | CHANGED | rc=0 >>
總用量 8
-rw-r--r--. 1 root   root  595 2月   1 17:44 fstab.bak
drwxr-xr-x. 2 root   root    6 3月  26 2015 rh
-rw-rw-rw-. 1 test02 mysql   6 2月   1 17:48 test.txt

7,ping模塊(測(cè)試被管理主機(jī)是否在線)

[root@promote ~]# ansible all -m ping
192.168.13.130 | SUCCESS => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
}
192.168.13.129 | SUCCESS => {
        "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
}

8,yum模塊

[root@promote ~]# ansible-doc -s yum ##yum模塊信息
[root@promote ~]# ansible webserver -m yum -a 'name=httpd'  ##安裝httpd服務(wù)
[root@promote ~]# ansible webserver -m yum -a 'name=httpd state=absent'   ##移除服務(wù)

9,service模塊

[root@promote ~]# ansible webserver -m service -a 'name=httpd enabled=true state=started'
##開(kāi)啟httpd服務(wù)
[root@promote ~]# ansible webserver -a 'systemctl status httpd' ##查看開(kāi)啟的情況

10,shell模塊

[root@promote ~]# ansible webserver -m user -a 'name=jerry'  ##創(chuàng)建用戶
[root@promote ~]# ansible webserver -m shell -a 'echo abc123 | passwd --stdin jerry' ##創(chuàng)建密碼
192.168.13.129 | CHANGED | rc=0 >>
更改用戶 jerry 的密碼 。
passwd:所有的身份驗(yàn)證令牌已經(jīng)成功更新。

11,script模塊(腳本模塊)

[root@promote ~]# cd /opt/
[root@promote opt]# vim test.sh  ##編輯腳本文件
#!/bin/bash
echo "this is test script" > /opt/script.txt 
chmod 666 /opt/script.txt
[root@promote opt]# chmod +x test.sh   ##給執(zhí)行權(quán)限
[root@promote opt]# ansible all -m script -a 'test.sh'   ##執(zhí)行腳本
[root@promote opt]# ansible all -a 'cat /opt/script.txt'   ##查看執(zhí)行情況
192.168.13.130 | CHANGED | rc=0 >>
this is test script

192.168.13.129 | CHANGED | rc=0 >>
this is test script

12,setup模塊(收集信息模塊)

[root@promote opt]# ansible mysql -m setup  ##查看mysql主機(jī)的信息

謝謝閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI