自動化運維Ansible之安裝部署


1、SSH分發

ansible自動化部署條件
1.建議基於ssh密鑰方式建立遠程連接
2.基於ssh口令方式建立遠程連接(不建議)

在部署之前需要保證管理主機受控主機能夠基於ssh密鑰的方式進行遠程連接

管理主機生成SSH密鑰(私鑰和公鑰),分發公鑰到每台受控主機

1.安裝sshpass

[root@m01 ~]# yum install sshpass -y

2.生成密鑰

//  直接生成密鑰
[root@m01 ~]# ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""
Generating public/private dsa key pair.
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:gfr8/bG2IAzxNJiom7WGwba8G26BZ5yfxJMp6O3Ouh4 root@m01
The key's randomart image is:
+---[DSA 1024]----+
|                 |
|     . +         |
|    . = +        |
| . . . + o       |
| +=ooo. S        |
|ooBB*+ o         |
|.EO=ooo o . .    |
| o+=o  . o ..o   |
|.=O=    . .o+.   |
+----[SHA256]-----+

3.分發密鑰

//  免交互式批量分發公鑰腳本
[root@m01 ~]# vim ~/ssh-fenfa.sh
#!/bin/bash
rm -f /root/.ssh/id_dsa 
ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""
  for ip in 7 8 
do
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no" 10.4.7.$ip
done

// 執行腳本
[root@m01 ~]# sh ~/ssh-fenfa.sh

4.一鍵ssh登錄測試for循環

[root@m01 ~]# for i in 7 8 ;do ssh 10.4.7.$i  date ;done
Mon Feb  3 17:23:50 CST 2020
Mon Feb  3 17:23:50 CST 2020

2、安裝Ansible

安裝方法有很多,這里僅僅以Centos7 yum安裝為例。

Ansible軟件默認不在標准倉庫中,需要用到repo源。

1.需在管理機器上安裝:

// 添加repo
[root@m01 ~]# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

// yum安裝ansilbe
[root@m01 ~]# yum install ansible -y
[root@m01 ~]# rpm -qa ansible

// 檢查ansible版本
[root@m01 ~]# ansible --version
ansible 2.9.2
  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 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

2.添加主機清單

[root@m01 ~]# vim /etc/ansible/hosts
[sa]
10.4.7.7
10.4.7.8

[sa] 分組下添加了兩個hosts

3、測試ansible

ping模塊用於測試ansible與被受控端的連通性

[root@m01 ~]# ansible sa -m ping
10.4.7.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.4.7.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

3、Ansible清單管理

主機清單路徑:/etc/ansible/hosts
/etc/ansible/hosts主機資產清單文件,用於定義被管理主機的認證信息, 例如ssh登錄用戶名、密碼以及key相關信息。如何配置Inventory文件

主機可以是IP地址形式出現也可以是主機名的形式出現,但是以主機名形式出現就必須要在ansible機器上有對應主機名和IP地址的hosts解析

主機:
1.主機支持主機名通配以及正則表達式,例如web[1:3].jason.com代表三台主機
2.主機支持基於非標准的ssh端口,例如web1.jason.com:6666
3.主機支持指定變量,可對個別主機的特殊配置,如登陸用戶,密碼
4.主機組支持指定變量[group_name:vars],同時支持嵌套組[game:children]

主機組:
1.支持嵌套組,例如[game:children],那么在game模塊下面的組都會被game所包含
2.支持指定變量,例如[game:vars]在下面指定變量

  • 基於密碼連接
[root@m01 ~]# cat /etc/ansible/hosts

// 方式一、主機+端口+密碼
[webservers]
10.0.0.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
10.0.0.41 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'

// 方式二、主機+端口+密碼
[webservers]
web[1:2].jason.com ansible_ssh_pass='123456'

// 方式三、主機+端口+密碼
// 添加三台主機至webserver【low版】
[webservers]
web1.jason.com
web2.jason.com
web3.jason.com

// 添加三台主機至webserver【改良版】
[webservers]
web[1:3].jason.com

// 添加三台主機至webserver【密碼版】
[webservers]
web1.jason.com ansible_ssh_pass='123456'
web2.jason.com ansible_ssh_pass='123456'
web3.jason.com ansible_ssh_pass='123456'

// 添加三台主機至webserver【密碼改良版】
[webservers]
web[1:3].jason.com ansible_ssh_pass='123456'

// 添加三台主機至webserver【密碼拆分版】
[webservers]
web1.jason.com
web2.jason.com
web3.jason.com
[webservers:vars]
ansible_ssh_pass='123456'
  • 基於密鑰連接,需要先創建公鑰和私鑰,並下發公鑰至被控端
// 利用非交換式工具實現批量分發公鑰與批量管理服務器
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31

// 方式一、主機+端口+密鑰
[group_name]
10.0.0.31:22
10.0.0.41

// 方式二、別名+主機+端口+密鑰
[group_name]
nfs-node1 ansible_ssh_host=10.0.0.31 ansible_ssh_port=22
  • 主機組使用方式
// 方式一、主機組變量+主機+密碼
[apache]
web1.jason.com
web2.jason.com
web3.jason.com
[apache:vars]
ansible_ssh_pass='123456'

// 方式二、主機組變量+主機+密鑰
[nginx]
10.0.0.7
10.0.0.8

// 定義多組,多組匯總整合
// webservers組包括兩個子組[apapche,nginx]
[webservers:children]
[group_name1]
[nginx]
  • ansible [主機模塊名] --list-hosts
//  查看該主機模塊中所定義的主機的IP地址
[root@m01 ~]# ansible nginx --list-hosts
 hosts (2):
  10.0.0.7
  10.0.0.8


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM