01_什么是ansible、基本架构、ansible工作机制、Ansible安装、配置主机清单、设置SSH无密码登录等

这篇具有很好参考价值的文章主要介绍了01_什么是ansible、基本架构、ansible工作机制、Ansible安装、配置主机清单、设置SSH无密码登录等。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.什么是ansible
1.1.基本介绍
1.2.基本架构
1.3.基本特征
1.4.优点
1.5.ansible工作机制
2.Ansible安装
2.1.机器准备
2.2.安装ansible
2.2.1.安装epel源
2.2.2.安装ansible
2.2.3.查看ansible版本
2.2.4.树状结构展示文件夹
2.2.4.1.其中ansible.cfg的内容如下
2.2.4.2.host的默认内容是
2.3.配置主机清单
2.4.设置SSH无密码登录
2.5.参考文章

1.什么是ansible

转自:https://blog.51cto.com/liqingbiao/1875921

1.1.基本介绍

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。ansible目前已经已经被红帽官方收购,是自动化运维工具中大家认可度最高的,并且上手容易,学习简单。是每位运维工程师必须掌握的技能之一。主要包括:

  • (1)、连接插件connection plugins:负责和被监控端实现通信;
  • (2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
  • (3)、各种模块核心模块、command模块、自定义模块;
  • (4)、借助于插件完成记录日志邮件等功能;
  • (5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

1.2.基本架构

01_什么是ansible、基本架构、ansible工作机制、Ansible安装、配置主机清单、设置SSH无密码登录等,# ansible,ansible

1.3.基本特征

(1)、no agents:不需要在被管控主机上安装任何客户端;
(2)、no server:无服务器端,使用时直接运行命令即可;
(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;
(4)、yaml,not code:使用yaml语言定制剧本playbook;
(5)、ssh by default:基于SSH工作;
(6)、strong multi-tier solution:可实现多级指挥。

1、部署简单,只需在主控端部署Ansible环境,被控端无需做任何操作;
2、默认使用SSH协议对设备进行管理;
3、有大量常规运维操作模块,可实现日常绝大部分操作。
4、配置简单、功能强大、扩展性强;
5、支持API及自定义模块,可通过Python轻松扩展;
6、通过Playbooks来定制强大的配置、状态管理;
7、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
8、提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台。

1.4.优点

(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上运行一次更新即可;
(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行。
(3)、使用python编写,维护更简单,ruby语法过于复杂
(4)、支持sudo

1.5.ansible工作机制

01_什么是ansible、基本架构、ansible工作机制、Ansible安装、配置主机清单、设置SSH无密码登录等,# ansible,ansible

2.Ansible安装

2.1.机器准备

本次使用docker进行虚拟化:
docker相关的命令是:

docker commit d74af80aa35a centos7.5.1804.v0.2

将镜像保存
docker save -o xxx.tar containerId

将镜像导入
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
主机名 IP 安装软件 组名
node2(管理机器) 172.17.0.2 Ansible
node3(被管理机器) 172.17.0.3 abc
node4(被管理机器) 172.17.0.4 abc
node5(备用) 172.17.0.5
node6(备用) 172.17.0.6

2.2.安装ansible

在管理机器上安装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.树状结构展示文件夹

# ansible --version          //查看ansible版本
# yum install tree -y
# tree /etc/ansible/         //树状结构展示文件夹
	/etc/ansible/
  	├── ansible.cfg        #ansible的配置文件
  	├── hosts            #ansible的主仓库,用于存储需要管理的远程主机的相关信息
  	└── roles             #角色(这是一个目录)
2.2.4.1.其中ansible.cfg的内容如下

01_什么是ansible、基本架构、ansible工作机制、Ansible安装、配置主机清单、设置SSH无密码登录等,# ansible,ansible

2.2.4.2.host的默认内容是
[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.配置主机清单

[root@node2 ansible]# cd /etc/ansible
[root@node2 ansible]# vim hosts
[abc]              # 自定义一个组名
172.17.0.3         # 添加被管理主机的IP

[aaa]
172.17.0.4

[bbb]
172.17.0.5
172.17.0.6

2.4.设置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 (此命令是在出现了-bash: ssh-copy-id: command not found之后执行)

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文章来源地址https://www.toymoban.com/news/detail-634495.html

到了这里,关于01_什么是ansible、基本架构、ansible工作机制、Ansible安装、配置主机清单、设置SSH无密码登录等的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ansible 基本使用

       ansible是一种IT自动化工具。可以配置系统、部署软件以及协调高级的IT任务,如持续部署、滚动更新。   使用文档: https://docs.ansible.com/    安装Ansible:yum install ansible -y     Invertory: ansible 管理的主机信息,包括ip、ssh端口、账号、密码等    modules: 任务都有模块来完成

    2023年04月16日
    浏览(20)
  • Ansible详解(架构,模块)及部署示例

    目录 Ansible概述 Ansible作用 Ansible特点 Ansible架构 工作流程 ansible 环境安装部署 环境准备 安装Ansible服务 Ansible 命令行模块 模块详解 ansible-doc command模块 shell模块 cron 模块 user模块 group模块 copy 模块 file 模块 hostname 模块 ping 模块 yum 模块 service/systemd 模块 script 模块 setup 模块 Y

    2024年01月21日
    浏览(29)
  • Ansible基本使用和常用模块

    1) 部署 2) 配置 3) Ans-inventory 主机清单 a.什么是主机清单 : 让 ansible 管理的节点的列表,ansible 默认读取在 /etc/ansible/hosts 文件 ,并非 /etc/hosts. b.我们会把主机清单文件存放在指定的目录中 , 运行 ansible 的时候通过 -i 选项指定主机清单文件即 可 4)  批量管理主机并执行shell命令

    2024年01月19日
    浏览(27)
  • Ansible变量是什么?如何实现任务的循环?

    Ansible 利用变量存储整个 Ansible 项目文件中可重复使用的值,从而可以简化项目的创建和维护,并减少错误的发生率。在定义Ansible变量时,通常有如下三种范围的变量: global范围:从命令行或Ansible配置中设置的变量; play范围:在 play 和相关结构中设置的变量; host范围:

    2024年02月04日
    浏览(23)
  • ansible实训-Day3(playbook的原理、结构及其基本使用)

         该篇是对ansible实训第三天内容的归纳总结,主要包括playbook组件的原理、结构及其基本使用方式。         Playbook是Ansible的核心组件之一,它是用于定义任务和配置的自动化脚本。         Ansible Playbook使用YAML语法编写,可以描述在受管主机上执行的一系列任务

    2024年02月11日
    浏览(24)
  • ansible相比shell脚本自动化有什么优势

    Ansible自动化与Shell脚本自动化相比,具有以下几个显著的优势: 轻量级与无需代理 :Ansible的一个关键优势在于其轻量级和无需在被控制服务器上安装任何代理或客户端的特点。这极大地简化了部署和维护的流程,降低了复杂性和出错的可能性。而Shell脚本通常需要在每台服

    2024年04月25日
    浏览(35)
  • ansible - 安装 -【kodcloud教程】

    目录 目录结构、主机清单 【1】安装基础环境 【2】webserver - 安装nginx 【3】安装php环境 【4】lb-server 【5】redis安装 【6】 kodcloud 【7】wordpress-proxy    ansible 10.0.0.10 lb-server 10.0.0.11 web01 10.0.0.15 web02 10.0.0.16 redis 10.0.0.20 目录结构、主机清单 【1】安装基础环境

    2024年02月11日
    浏览(25)
  • 三、05-ansible安装

    anseble 是用python 开发的 集合了多种运维自动化工具(pupet、cfengine、chef、func、fabric) 实现了批量系统配置、批量程序部署、批量运行命令等功能 无客户端 playbook 剧本文件 主要是 是yaml 的语言 (意思是 我不仅仅是一个标记语言) 1、部署简单,只需要在主控端部署Ansible环境

    2024年01月23日
    浏览(61)
  • Ansible的安装和部署

    目录 1.Ansible的安装 2.构建Ansible清单  直接书写受管主机名或ip 设定受管主机的组[组名称] 主机规格的范围化操作 指定其他清单文件 ansible命令指定清单的正则表达式 3.Ansible配置文件参数详解 配置文件的分类与优先级 常用配置参数 4.构建用户级Ansible操作环境 环境配置 主控

    2024年02月08日
    浏览(30)
  • Ansible离线安装

    Ansible rpm离线安装 离线安装包下载

    2024年04月22日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包