自动化运维之Ansible

一、Ansible概述

基于Python开发

最流行的自动化运维工具,部署简单;

只需在控制节点部署Ansible环境,托管节点无需做任何操作(因为使用SSH);

默认使用SSH协议对设备进行管理,有大量常规运维操作模块,可实现日常绝大部分操作;

配置简单、功能强大、扩展性强;支持API及自定义模块,可通过Python轻松扩展;

通过Playbooks来定制强大的配置、状态管理;

轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;

提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台。

1.1 Ansible架构图

  1. 控制节点(Control Node):安装 Ansible 的主机,用于编写和执行 playbook 及模块。可以使用ansibleansible-inventory命令。
  2. 主机清单 (Inventory):托管节点的列表。 可以在控制节点上创建清单给 Ansible 描述如何部署主机。
  3. 托管节点(Managed Nodes):被 Ansible 管理和配置的服务器。

在这里插入图片描述

1.2 官网文档地址

Getting started with Ansible — Ansible Community Documentation

二、安装Ansible

# 此处的-i指的是index-url,也就是下载包的链接的前缀
pip3 install "ansible==2.9" -i https://pypi.tuna.tsinghua.edu.cn/simple

三、主机清单

记得把控制节点的SSH公钥添加到每个主机上的authorized_keys文件中。

清单文件格式有两种:INI 或 YAML 格式

INI格式示例

inventory.ini

[myhosts]
192.168.233.92
192.168.233.93
[myhosts:vars]
ansible_ssh_root="root"
ansible_ssh_pass="xxx"

使用[组名:vars] 功能指定root用户和密码需要安装sshpassRPM resource sshpass(x86-64) (rpmfind.net)

不推荐密码明文存储,有安全风险,请使用公钥连接托管主机。ssh-keygen生成公钥和私钥,然后使用ssh-copy-id root@ipaddr将公钥传输至托管主机的/root/.ssh/authorized_keys

可以用:来指定IP范围

[myhosts]
192.168.233.[92:93]

查看主机清单

[root@kafka1 ops]# ansible-inventory -i inventory.ini --list
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "myhosts",
            "ungrouped"
        ]
    },
    "myhosts": {
        "hosts": [
            "192.168.233.92",
            "192.168.233.93"
        ]
    }
}

对清单中的 group 执行 ping 操作

[root@kafka1 ops]# ansible myhosts -m ping -i inventory.ini
[WARNING]: Platform linux on host 192.168.233.92 is using the discovered Python
interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html
for more information.

192.168.233.92 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform linux on host 192.168.233.93 is using the discovered Python
interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html
for more information.

192.168.233.93 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值