Ansible是一種運(yùn)維自動(dòng)化工具軟件,用來(lái)批量配置服務(wù)器或網(wǎng)絡(luò)設(shè)備(目標(biāo)主機(jī))。
一、概念理解
- Ansible如何來(lái)連接目標(biāo)主機(jī)?
通過(guò)SSH協(xié)議進(jìn)行連接,詳細(xì)參考:SSH協(xié)議
- 目標(biāo)主機(jī)為何能相信Ansible,并接受其指令?
(1)Ansible知道目標(biāo)主機(jī)密碼,并通過(guò)密碼訪問(wèn)。
Ansible將密碼配置在以明文的形式配置在文件(也就是hosts文件),存在安全性問(wèn)題。
(2)Ansible主機(jī)生成密鑰對(duì),并將公鑰拷貝到目標(biāo)主機(jī)。
通過(guò)ssh-copy-id命令進(jìn)行拷貝,并修改~/.ssh的目錄權(quán)限。如:
ssh-copy-id -i /root/.ssh/id_rsa.pub root@30.0.1.43
當(dāng)目標(biāo)主機(jī)較多時(shí),這種方法也比較受限。
(3)Ansible自動(dòng)化安裝配置工具
Redhat下,通過(guò)Kickstart工具進(jìn)行,可進(jìn)行大批量的認(rèn)證。
- Ansible如何知道需要連接哪些目標(biāo)主機(jī)?
目標(biāo)主機(jī)列表定義在/etc/ansible/hosts文件,稱為 "inventory" 。定義格式為:
[webservers]
30.0.1.234
30.0.1.154
- 目標(biāo)主機(jī)上都有哪些事情需要做?
運(yùn)維過(guò)程中,需要做的事情很多,如:
(1)基礎(chǔ)命令,如ls;
(2)定時(shí)任務(wù),如crontab
(3)啟停服務(wù),如service ngnix restart
(4)包管理,如apt install ansible
......
- Ansible如何知道做這些事(任務(wù))的?
運(yùn)維人員通過(guò)兩種方式來(lái)告知Ansible做事:
一種是我們熟悉的命令行方式,類(lèi)似ansible webserver -m ping,稱之**“Ad-Hoc命令”**;
一種是通過(guò)YAML語(yǔ)法定義要執(zhí)行的命令,稱為 “Playbook” 方式。
- Ansible又是如何具備做這些事的能力的?
Ansible通過(guò) “Module” 來(lái)實(shí)現(xiàn),如command、shell、copy等等。
- 運(yùn)維人員如何知道Ansible提供了哪些Module?
查找Ansible提供的模塊
root@linux:/etc/ansible# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices' server object.
a10_server_axapi3 Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
......
模塊的詳細(xì)信息
root@linux:/etc/ansible# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success
ping:
data: # Data to return for the `ping' return value. If this parameter is set to `crash' the
module will cause an exception.
- 如何讀懂PlayBook?
(1)了解YAML
PlayBook是通過(guò)YAML語(yǔ)法來(lái)實(shí)現(xiàn)的,詳情參考:《Yaml:數(shù)據(jù)的另一種形態(tài)》。
(2)清楚PlayBook有哪些關(guān)鍵字,如何來(lái)的?
--- #文件開(kāi)始
- hosts: 30.0.1.43 #目標(biāo)主機(jī)
remote_user: root #目標(biāo)主機(jī)用戶
vars: #變量關(guān)鍵字
http_port: 8088 #定義變量
tasks: #任務(wù)關(guān)鍵字
- name: create new file #自定義任務(wù)
file: name=/tmp/playtest.txt state=touch #Module file,創(chuàng)建新文件
.......
handlers: #處理關(guān)鍵字
- name: restart apache #自定義處理名稱
service: name=httpd state=restarted #重啟服務(wù)
說(shuō)明:file、service都是module名字。
二、Ansible小示例
基礎(chǔ)環(huán)境:Ubuntu18.04 LTS
- 安裝ansible軟件
root@linux:/# apt install ansible
......
root@linux:/# apt install sshpass
查看ansible版本信息:
root@linux:/etc/ansible# ansible --version
ansible 2.5.1
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/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.17 (default, Sep 30 2020, 13:38:04) [GCC 7.5.0]
已生成ansible.cfg、hosts文件
root@linux:/etc/ansible# ls
ansible.cfg hosts
- Ansible連通目標(biāo)主機(jī)
(1)目標(biāo)主機(jī)有兩臺(tái),IP地址分別為:30.0.1.234、30.0.1.154
(2)配置hosts文件
root@linux:/etc/ansible# vi hosts
[webservers]
30.0.1.234
30.0.1.154
(3)執(zhí)行ping操作,測(cè)試連接
root@linux:~# ansible webservers -m ping
The authenticity of host '30.0.1.154 (30.0.1.154)' can't be established.
ECDSA key fingerprint is SHA256:THHVZ1IfwqJk0YpV7Qk/a+ZvMds4phRQJEbrJIJFagg.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '30.0.1.234 (30.0.1.234)' can't be established.
ECDSA key fingerprint is SHA256:THHVZ1IfwqJk0YpV7Qk/a+ZvMds4phRQJEbrJIJFagg.
Are you sure you want to continue connecting (yes/no)? yes
30.0.1.154 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '30.0.1.154' (ECDSA) to the list of known hosts.\\r\\nroot@30.0.1.154: Permission denied (publickey,password).\\r\\n",
"unreachable": true
}
30.0.1.234 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Host key verification failed.\\r\\n",
"unreachable": true
}
連接失敗
(4)修改ansible.cfg配置文件,不進(jìn)行host_key的校驗(yàn)
root@linux:~# vi /etc/ansible/ansible.cfg
# uncomment this to disable SSH key host checking
host_key_checking = False
root@linux:/etc/ansible# ansible webservers -m ping
30.0.1.154 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: root@30.0.1.154: Permission denied (publickey,password).\\r\\n",
"unreachable": true
}
30.0.1.234 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '30.0.1.234' (ECDSA) to the list of known hosts.\\r\\nroot@30.0.1.234: Permission denied (publickey,password).\\r\\n",
"unreachable": true
}
依然無(wú)法連接到目標(biāo)主機(jī),root用戶不允許進(jìn)行遠(yuǎn)程登錄
(5)修改hosts文件
root@linux:/etc/ansible# vi hosts
[webservers]
30.0.1.234
30.0.1.154
[webservers:vars]
ansible_ssh_user=linux
ansible_ssh_pass=user@linux
ansible_become=true
ansible_become_method=su
ansible_become_user=root
ansible_become_pass=root@linux
上面配置語(yǔ)句的含義為:以linux用戶登錄,然后再提高權(quán)限,切換到root用戶。
(6)再次測(cè)試,連接成功
root@linux:/etc/ansible# ansible webservers -m ping
30.0.1.234 | SUCCESS => {
"changed": false,
"ping": "pong"
}
30.0.1.154 | SUCCESS => {
"changed": false,
"ping": "pong"
}
"pong" 表示ping成功,連接成功
-
服務(wù)器
+關(guān)注
關(guān)注
12文章
9303瀏覽量
86061 -
自動(dòng)化
+關(guān)注
關(guān)注
29文章
5622瀏覽量
79663 -
網(wǎng)絡(luò)設(shè)備
+關(guān)注
關(guān)注
0文章
318瀏覽量
29765 -
工具軟件
+關(guān)注
關(guān)注
1文章
16瀏覽量
6912
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論