文章目录
一: ansible 的概述
1. ansible简介
-
Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具。
-
它用Python写成,类似于saltstack和Puppet,但是有一个不同和优点是我们不需要在节点中安装任何客户端。
-
它使用SSH 来和节点进行通信。Ansible基于 Python paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2模板语言,更强的远程命令执行操作。
2. ansible官方网站
链接: 官方网站
- 我们可以看到上面的红帽标志,红帽公司于2015年10月收购了ansible,而ansible成立于2013年。
3. ansible 的特点
① 部署简单,没有客户端,只需在主控端部署Ansible环境,被控端无需做任何操作;
② 模块化:调用特定的模块,完成特定任务
③ 默认使用SSH协议对设备进行管理;
④ 主从集中化管理;
⑤ 配置简单、功能强大、扩展性强;
⑥ 支持API及自定义模块,可通过Python轻松扩展;
⑦ 通过Playbooks来定制强大的配置、状态管理
⑧ 对云计算平台、大数据都有很好的支持;
⑨ 具有幂等性:一个操作在一个主机上执行一遍和执行N遍的结果是一样的
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务
4. ansible的工作机制
- Ansible 在管理节点将 Ansible 模块通过 SSH 协议推送到被管理端执行,执行完之后自动删除,可以使用 SVN 等来管理自定义模块及编排
由图可以看出Ansible的组成由一下模块组成:
① Ansible: ansible的核心模块
② Host Inventory:主机清单,也就是被管理的主机列表
③ Playbooks:ansible的剧本,可想象为将多个任务放置在一起,一块执行
④ Core Modules:ansible的核心模块
⑤ Custom Modules:自定义模块
⑥ Connection Plugins:连接插件,用于与被管控主机之间基于SSH建立连接关系
⑦ Plugins:其他插件,包括记录日志等
二. Asible的安装
1. 设置EPEL仓库
- Ansible仓库默认不在链接: yum仓库中,因此我们需要使用下面的命令启用epel仓库
[root@itlaoxin162 ~]# yum install epel-release -y
2. 使用yum安装Ansible
[root@itlaoxin162 ~]# yum install ansible
3. 查看ansible的版本
[root@itlaoxin162 ~]# ansible --version
ansible 2.9.18
ansible的命令参数
anisble命令语法: ansible [-i 主机文件] [-f 批次] [组名] [-m 模块名称] [-a 模块参数]
参数 | 功能 |
---|---|
-v | 详细模式,如果执行成功,输出详细结果 |
-i | 指定host文件路径,默认在/etc/ansible/hosts |
-f,-forks=NUM | NUM默认是整数5,指定fork开启同步进程的个数 |
-m | 指定使用的module名称,默认command模块 |
-a | 指定模块的参数: |
-k | 提示输入SSH密码,而不是使用基于ssh密钥认证 |
-sudo | 指定使用sudo获取root权限 |
-K | 提示输入sudo密码 |
-u | 指定移动端的执行用户 |
-C | 测试命令执行会改变什么内容,不会真正的去执行 |
ansible-doc 详细参数
- 列出所有模块列表
[root@itlaoxin162 ~]# ansible-doc -l
指定查看某个模块的参数
ansible-doc -s 模块名字
[root@itlaoxin162 ~]# ansible-doc -s onyx_ospf
- name: Manage OSPF protocol on Mellanox ONYX network devices
onyx_ospf:
interfaces: # List of interfaces and areas. Required if `state=present'.
ospf: # (required) OSPF instance number 1-65535
router_id: # OSPF router ID. Required if `state=present'.
state: # OSPF state.
[root@itlaoxin162 ~]# ansible-doc -s service
- name: Manage services
service:
arguments: # Additional arguments provided on the command line.
enabled: # Whether the service should start on boot. *At least one of
state and enabled are
required.*
name: # (required) Name of the service.
pattern: # If the service does not respond to the status command,
name a substring to look
for as would be found in
the output of the `ps'
command as a stand-in for a
status result. If the
string is found, the
service will be assumed to
be started.
runlevel: # For OpenRC init scripts (e.g. Gentoo) only. The runlevel
that this service belongs
to.
sleep: # If the service is being `restarted' then sleep this many
seconds between the stop
三. ansible的使用
3.1、基于端口,用户,密码定义主机清单
格式:
ansible基于ssh连接-i (inventory)参数后指定的远程主机时,也可以写端口,用户,密码。
如:
① ansible_ssh_port: 指定ssh端口 ansible_ssh_user:指定 ssh 用户
② ansible_ssh_pass: 指定 ssh 用户登录是认证密码(明文密码不安全)
③ ansible_sudo_pass: 指明 sudo 时候的密码
添加的内容如下:
[root@itlaoxin162 ~]# grep -v ^# /etc/ansible/hosts |grep -v ^$
[web-servers]
192.168.1.163 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=12345678
[root@itlaoxin16
直接添加到文件文末就可以;
测试主机的连通性
[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m ping
[WARNING]: Invalid characters were found in group names but not replaced, us
see details
192.168.1.163 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
查看组下所有的IP:
[root@itlaoxin162 ~]# ansible all --list
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
hosts (1):
192.168.1.163
2. 基于ssh密钥链接: 密钥来访问定义主机清单
设置密钥
[root@itlaoxin162 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:CWdEZJbtzH4+ypeXe80jPnBr9UX/0yChZtX5DCjKckg root@itlaoxin162
The key's randomart image is:
+---[RSA 2048]----+
| o*o |
| +. . |
| . o+ o . |
| E+ .= + + .|
| . oSo + . =.|
| o + =.o...=|
| o o oooo+*|
| . ==ooB|
| ooo++oo|
+----[SHA256]-----+
[root@itlaoxin162 ~]#
拷贝密钥并测试
[root@itlaoxin162 ~]# ssh-copy-id root@192.168.1.163
/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@192.168.1.163's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.163'"
and check to make sure that only the key(s) you wanted were added.
**登陆测试:**
[root@itlaoxin162 ~]# ssh 192.168.1.163
Last login: Wed Apr 21 08:13:14 2021 from 192.168.1.162
71服务器也发送密钥
[root@itlaoxin162 ~]# ssh-copy-id root@192.168.1.71
修改hosts
vim /etc/ansible/hosts
查看配置文件中刚刚修改的内容