Ansible简介及安装

1、基本概念

        Ansible是近年来越来越火的一款轻量级运维自动化工具,主要功能为帮助运维实现运维工作的自动化、降低手动操作的失误、提升运维工作效率。常用于自动化部署软件、自动化配置、自动化管理,支持playbook编排。配置简单,无需安装客户端,被管理端主机通过ssh协议通信。Ansible本身不具有自动化部署功能,全交由其丰富的模块实现。

2、核心组件

  • 连接插件:负责与被管理端通信
  • 主机清单:配置被管理的主机
  • 模块:进行各种自动化部署
  • playbook(剧本):实现对自动化部署进行流程控制

3、架构

以上即为Ansible的基本架构图,控制端于客户端基于ssh进行通信,涉及的部署任务也是基于ssh(密码认证或者密钥认证)。

  • Ansible:核心
  • Host Inventoy:定义被管理的主机群组
  • Playbook:定义要执行的具体的任务
  • Plugins:集成的各类插件实现更多的功能
  • Modules:包含Ansible的核心模块及自定义的扩展模块

3、安装

由于ansible如那件包由epel源提供,所以需要先安装epel源。

# 安装epel源
yum install -y epel-release
# 安装
yum install ansible -y
# 查看版本
ansible --version
ansible [core 2.14.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.2 (main, Jun 22 2023, 04:35:24) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)] (/usr/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True

4、配置Ansible主机与被管理主机ssh单向ssh认证

# 生成ssh密钥
[root@Ansible ~]# 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:1s4H5dioQ0CxZvmf3IqFSc7AMn6Po9J1DGdF2Umh7RU root@Ansible
The key's randomart image is:
+---[RSA 3072]----+
|      o. ..+ooE  |
|     . o  ooo  . |
|      *  .. o .  |
|     +.oo. B .   |
|    o o=S = +    |
|   . o.BoO +     |
|   .....B B o    |
|  . ...o + o     |
|   .....o .      |
+----[SHA256]-----+
# 向客户端推送公钥
[root@Ansible ~]# ssh-copy-id 192.168.91.110

5、测试连接性

5.1、配置Host Inventoy

[root@Ansible opt]# cat hosts
[client]
192.168.91.110

[clinet]:定义主机分组

5.2、测试

# 语法
ansible $hostgroup -i 主机清单文件 -m 模块

[root@Ansible opt]# ansible client -i hosts -m ping
192.168.91.110 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
  • $hostgroup:指定主机组
  • -i:指定主机清单文件
  • -m: 指定要使用的模块

输出关键字“ping:pong”,说明连接正常,之后即可在Ansible命令行进行管理任务和通过编写playbook剧本执行批量部署任务。

以上就完成了Ansible的安装及与被管理主机间的通信。下一篇文章将介绍学习常用的模块。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值