1.什么是ansible
1.1.基本介紹
1.2.基本架構(gòu)
1.3.基本特征
1.4.優(yōu)點
1.5.ansible工作機(jī)制
2.Ansible安裝
2.1.機(jī)器準(zhǔn)備
2.2.安裝ansible
2.2.1.安裝epel源
2.2.2.安裝ansible
2.2.3.查看ansible版本
2.2.4.樹狀結(jié)構(gòu)展示文件夾
2.2.4.1.其中ansible.cfg的內(nèi)容如下
2.2.4.2.host的默認(rèn)內(nèi)容是
2.3.配置主機(jī)清單
2.4.設(shè)置SSH無密碼登錄
2.5.參考文章
1.什么是ansible
轉(zhuǎn)自:https://blog.51cto.com/liqingbiao/1875921
1.1.基本介紹
ansible是新出現(xiàn)的自動化運(yùn)維工具,基于Python開發(fā),集合了眾多運(yùn)維工具(puppet、cfengine、chef、func、fabric)的優(yōu)點,實現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能。
ansible是基于 paramiko 開發(fā)的,并且基于模塊化工作,本身沒有批量部署的能力。真正具有批量部署的是ansible所運(yùn)行的模塊,ansible只是提供一種框架。ansible不需要在遠(yuǎn)程主機(jī)上安裝client/agents,因為它們是基于ssh來和遠(yuǎn)程主機(jī)通訊的。ansible目前已經(jīng)已經(jīng)被紅帽官方收購,是自動化運(yùn)維工具中大家認(rèn)可度最高的,并且上手容易,學(xué)習(xí)簡單。是每位運(yùn)維工程師必須掌握的技能之一。主要包括:
- (1)、連接插件connection plugins:負(fù)責(zé)和被監(jiān)控端實現(xiàn)通信;
- (2)、host inventory:指定操作的主機(jī),是一個配置文件里面定義監(jiān)控的主機(jī);
- (3)、各種模塊核心模塊、command模塊、自定義模塊;
- (4)、借助于插件完成記錄日志郵件等功能;
- (5)、playbook:劇本執(zhí)行多個任務(wù)時,非必需可以讓節(jié)點一次性運(yùn)行多個任務(wù)。
1.2.基本架構(gòu)
1.3.基本特征
(1)、no agents:不需要在被管控主機(jī)上安裝任何客戶端;
(2)、no server:無服務(wù)器端,使用時直接運(yùn)行命令即可;
(3)、modules in any languages:基于模塊工作,可使用任意語言開發(fā)模塊;
(4)、yaml,not code:使用yaml語言定制劇本playbook;
(5)、ssh by default:基于SSH工作;
(6)、strong multi-tier solution:可實現(xiàn)多級指揮。
1、部署簡單,只需在主控端部署Ansible環(huán)境,被控端無需做任何操作;
2、默認(rèn)使用SSH協(xié)議對設(shè)備進(jìn)行管理;
3、有大量常規(guī)運(yùn)維操作模塊,可實現(xiàn)日常絕大部分操作。
4、配置簡單、功能強(qiáng)大、擴(kuò)展性強(qiáng);
5、支持API及自定義模塊,可通過Python輕松擴(kuò)展;
6、通過Playbooks來定制強(qiáng)大的配置、狀態(tài)管理;
7、輕量級,無需在客戶端安裝agent,更新時,只需在操作機(jī)上進(jìn)行一次更新即可;
8、提供一個功能強(qiáng)大、操作性強(qiáng)的Web管理界面和REST API接口——AWX平臺。
1.4.優(yōu)點
(1)、輕量級,無需在客戶端安裝agent,更新時,只需在操作機(jī)上運(yùn)行一次更新即可;
(2)、批量任務(wù)執(zhí)行可以寫成腳本,而且不用分發(fā)到遠(yuǎn)程就可以執(zhí)行。
(3)、使用python編寫,維護(hù)更簡單,ruby語法過于復(fù)雜
(4)、支持sudo
1.5.ansible工作機(jī)制
2.Ansible安裝
2.1.機(jī)器準(zhǔn)備
本次使用docker進(jìn)行虛擬化:
docker相關(guān)的命令是:
docker commit d74af80aa35a centos7.5.1804.v0.2
將鏡像保存
docker save -o xxx.tar containerId
將鏡像導(dǎo)入
docker load < xxx.tar
============================================================
docker run -itd --restart=always --name node2 --hostname node2 -v D:/dockerworkspace/node2/hosts:/etc/hosts -v D:/dockerworkspace/node2/hostname:/etc/hostname -v D:/dockerworkspace/node2/data:/data -v D:/dockerworkspace/node2/profile:/etc/profile -v D:/dockerworkspace/node2/software:/root/software -v D:/dockerworkspace/node2/installed:/root/installed -v D:/dockerworkspace/node2/workspace:/root/workspace -p 20022:22 --ip 172.17.0.2 1694ea743590 /usr/sbin/sshd -D
docker run -itd --restart=always --name node3 --hostname node3 -v D:/dockerworkspace/node3/hosts:/etc/hosts -v D:/dockerworkspace/node3/hostname:/etc/hostname -v D:/dockerworkspace/node3/data:/data -v D:/dockerworkspace/node3/profile:/etc/profile -v D:/dockerworkspace/node3/software:/root/software -v D:/dockerworkspace/node3/installed:/root/installed -v D:/dockerworkspace/node3/workspace:/root/workspace -p 30022:22 --ip 172.17.0.3 1694ea743590 /usr/sbin/sshd -D
docker run -itd --restart=always --name node4 --hostname node4 -v D:/dockerworkspace/node4/hosts:/etc/hosts -v D:/dockerworkspace/node4/hostname:/etc/hostname -v D:/dockerworkspace/node4/data:/data -v D:/dockerworkspace/node4/profile:/etc/profile -v D:/dockerworkspace/node4/software:/root/software -v D:/dockerworkspace/node4/installed:/root/installed -v D:/dockerworkspace/node4/workspace:/root/workspace -p 40022:22 --ip 172.17.0.4 1694ea743590 /usr/sbin/sshd -D
docker run -itd --restart=always --name node5 --hostname node5 -v D:/dockerworkspace/node5/hosts:/etc/hosts -v D:/dockerworkspace/node5/hostname:/etc/hostname -v D:/dockerworkspace/node5/data:/data -v D:/dockerworkspace/node5/profile:/etc/profile -v D:/dockerworkspace/node5/software:/root/software -v D:/dockerworkspace/node5/installed:/root/installed -v D:/dockerworkspace/node5/workspace:/root/workspace -p 50022:22 --ip 172.17.0.5 1694ea743590 /usr/sbin/sshd -D
docker run -itd --restart=always --name node6 --hostname node6 -v D:/dockerworkspace/node6/hosts:/etc/hosts -v D:/dockerworkspace/node6/hostname:/etc/hostname -v D:/dockerworkspace/node6/data:/data -v D:/dockerworkspace/node6/profile:/etc/profile -v D:/dockerworkspace/node6/software:/root/software -v D:/dockerworkspace/node6/installed:/root/installed -v D:/dockerworkspace/node6/workspace:/root/workspace -p 60022:22 --ip 172.17.0.6 1694ea743590 /usr/sbin/sshd -D
主機(jī)名 | IP | 安裝軟件 | 組名 |
---|---|---|---|
node2(管理機(jī)器) | 172.17.0.2 | Ansible | |
node3(被管理機(jī)器) | 172.17.0.3 | abc | |
node4(被管理機(jī)器) | 172.17.0.4 | abc | |
node5(備用) | 172.17.0.5 | ||
node6(備用) | 172.17.0.6 |
2.2.安裝ansible
在管理機(jī)器上安裝ansible
2.2.1.安裝epel源
[root@node2 ~]# yum install -y epel-release
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): extras/7/x86_64/primary_db | 243 kB 00:00:00
(2/2): updates/7/x86_64/primary_db | 11 MB 00:00:01
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================================
Installing:
epel-release noarch 7-11 extras 15 k
Transaction Summary
================================================================================================================================================
Install 1 Package
Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release-7-11.noarch 1/1
Verifying : epel-release-7-11.noarch 1/1
Installed:
epel-release.noarch 0:7-11
Complete!
[root@node2 ~]#
2.2.2.安裝ansible
[root@node2 ~]# yum install ansible -y
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 3.9 kB 00:00:00
* base: mirrors.163.com
* epel: ftp.jaist.ac.jp
* extras: mirrors.163.com
* updates: mirrors.163.com
epel | 4.7 kB 00:00:00
(1/3): epel/x86_64/group_gz | 96 kB 00:00:00
(2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:01
xxxxxxxxxxxxxxxxxxxxxxxxx
--> Running transaction check
---> Package python-backports.x86_64 0:1.0-8.el7 will be installed
---> Package python-pycparser.noarch 0:2.14-1.el7 will be installed
--> Processing Dependency: python-ply for package: python-pycparser-2.14-1.el7.noarch
--> Running transaction check
Complete!
[root@node2 ~]#
2.2.3.查看ansible版本
[root@node2 ~]# ansible --version
ansible 2.9.25
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, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@node2 ~]#
2.2.4.樹狀結(jié)構(gòu)展示文件夾
# ansible --version //查看ansible版本
# yum install tree -y
# tree /etc/ansible/ //樹狀結(jié)構(gòu)展示文件夾
/etc/ansible/
├── ansible.cfg #ansible的配置文件
├── hosts #ansible的主倉庫,用于存儲需要管理的遠(yuǎn)程主機(jī)的相關(guān)信息
└── roles #角色(這是一個目錄)
2.2.4.1.其中ansible.cfg的內(nèi)容如下
2.2.4.2.host的默認(rèn)內(nèi)容是
[root@node2 ansible]# cat hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
## www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
# Here's another example of host ranges, this time there are no
# leading 0s:
## db-[99:101]-node.example.com
[root@node2 ansible]#
2.3.配置主機(jī)清單
[root@node2 ansible]# cd /etc/ansible
[root@node2 ansible]# vim hosts
[abc] # 自定義一個組名
172.17.0.3 # 添加被管理主機(jī)的IP
[aaa]
172.17.0.4
[bbb]
172.17.0.5
172.17.0.6
2.4.設(shè)置SSH無密碼登錄
[root@node2 ansible]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:sZtXen524PUZoQP5C76faiL/Rpx9IOUIFpVB7SIjymM root@node2
The key's randomart image is:
+---[RSA 2048]----+
| .+++ |
| o . o |
| o . * |
| . = * + . |
| . . S + O o .|
| E o B =.o.|
| . . o = o.+o+|
| . o * .=.o|
| o.=+== . |
+----[SHA256]-----+
[root@node2 ansible]#
yum -y install openssh-clients (此命令是在出現(xiàn)了-bash: ssh-copy-id: command not found之后執(zhí)行)文章來源:http://www.zghlxwxcb.cn/news/detail-634495.html
ssh-copy-id root@172.17.0.3
ssh-copy-id root@172.17.0.4
ssh-copy-id root@172.17.0.5
ssh-copy-id root@172.17.0.6
免交互代理:
[root@node2 ansible]# ssh-agent bash
[root@node2 ansible]# ssh-add
[root@node2 ansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.0.3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.17.0.3's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.17.0.3'"
and check to make sure that only the key(s) you wanted were added.
[root@node2 ansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.0.4
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.17.0.4's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.17.0.4'"
and check to make sure that only the key(s) you wanted were added.
[root@node2 ansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.0.5
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.17.0.5's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.17.0.5'"
and check to make sure that only the key(s) you wanted were added.
[root@node2 ansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.0.6
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.17.0.6's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.17.0.6'"
and check to make sure that only the key(s) you wanted were added.
[root@node2 ansible]#
2.5.參考文章
https://blog.51cto.com/u_13630803/2153392
https://www.cnblogs.com/cyleon/p/11164344.html
https://blog.51cto.com/u_13630803/2153820文章來源地址http://www.zghlxwxcb.cn/news/detail-634495.html
到了這里,關(guān)于01_什么是ansible、基本架構(gòu)、ansible工作機(jī)制、Ansible安裝、配置主機(jī)清單、設(shè)置SSH無密碼登錄等的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!