Ansible使用记录(一)——介绍、安装及简单使用

2023-05-16

文章目录

  • 系列开篇说明
  • 正文
    • 环境介绍
    • 安装
      • Linux版本安装
      • 官方版本安装
    • 基本概念介绍
      • 关于互信
      • 关于inventory
  • 总结

系列开篇说明

此次新开的一个自动化更新系列,主要是因为最近工作中用的比较多,而且Ansible在当今的生产环境中,对于运维来讲几乎是必备的技能。
同类型产品中除了Ansible外当然还有Puppet/Chef,但是相对于Ansible来说学习成本是比较高的,而且Ansible已经能够满足大部分的场景。
此系列博客不会从0开始写,而是多一些生产中的技巧及用法。对于0基础的同学来说,建议先去对Ansible有部分的了解再来看此系列博客。

此篇主要是介绍安装及简单使用。

正文

环境介绍

为了覆盖多种环境的情况,将会使用当前企业中较多使用的操作系统进行演示。

主机名执行用户IPOSPython版本备注
ansible01ansible10.0.2.6/24Ubuntu 20.043.8.10主控端
ansible02ansible10.0.2.7/24Ubuntu 20.043.8.10被控端
ansible03ansible10.0.2.8/24Rocky Linux 83.6.8被控端
ansible04ansible10.0.2.9/24CentOS 72.7.5被控端

安装

Ansible有多个维护版本,一是各大Linux发行版自己维护的版本(下称Linux版本),二就是使用Ansible维护的官方版本(下称官方版本)。
对于大家看过的大部分教程来说,都是使用Linux版本来安装及使用。但是我个人其实是更推荐使用官方版本
对于使用Linux版本来说,会生成一部分的配置文件,且目录比较规范,对于不太熟悉的人来说可能会省掉很多事情;当然缺点就是版本会落后于官方版本。
而官方版本不会生成Ansible的目录,不会生成配置文件,安装完需要自己来配置,对于部分人来说可能不太友好。

Linux版本安装

对于debian系的Linux来讲使用以下命令安装:

$ sudo apt-get install -y ansible

对于Redhat系的Linux来说使用以下命令安装:

# RHEL7及以下
yum install -y ansible
# RHEL8及以上
dnf install -y ansible

官方版本安装

因为Python的安装版本更加具备通用性,所以我个人更推荐使用Python安装的版本,此系列的教程也以Python的版本为准

不建议使用低于3.6.5版本的Python,因为3.6.5以前的大部分特性已被废弃
不建议直接使用操作系统中的Python来进行安装,操作系统中的默认Python应保持一个干净的环境。建议使用pyvenv来生成一个独立的Python环境来使用ansible

通过以下方式生成pyvenv

# 对于大部分Linux来说默认都是没有pyvenv包的,需要先安装才能使用
ansible@ansible01:~$ mkdir pyvenv
ansible@ansible01:~$ python3 -m venv pyvenv/ansible
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.8-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/home/ansible/pyvenv/ansible/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

ansible@ansible01:~$ sudo apt install -y python3.8-venv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libfwupdplugin1 libxmlb1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libpython3.8 libpython3.8-minimal libpython3.8-stdlib python-pip-whl python3.8 python3.8-minimal
Suggested packages:
  python3.8-doc binutils binfmt-support
The following NEW packages will be installed:
  python-pip-whl python3.8-venv
The following packages will be upgraded:
  libpython3.8 libpython3.8-minimal libpython3.8-stdlib python3.8 python3.8-minimal
5 upgraded, 2 newly installed, 0 to remove and 35 not upgraded.
Need to get 8,115 kB of archives.
After this operation, 2,331 kB of additional disk space will be used.
Get:1 https://mirrors.bfsu.edu.cn/ubuntu focal-updates/main amd64 libpython3.8 amd64 3.8.10-0ubuntu1~20.04.6 [1,625 kB]
Get:2 https://mirrors.bfsu.edu.cn/ubuntu focal-updates/main amd64 python3.8 amd64 3.8.10-0ubuntu1~20.04.6 [387 kB]
······
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for mime-support (3.64ubuntu1) ...
# 安装完成后生成pyvenv
ansible@ansible01:~$ python3 -m venv pyvenv/ansible
# 此时就会在当前目录下的pyvenv/ansible生成一个pyvenv
ansible@ansible01:~$ pyvenv/ansible/bin/python3 -V
Python 3.8.10
# 激活此Python环境
ansible@ansible01:~$ source pyvenv/ansible/bin/activate
(ansible) ansible@ansible01:~$

# 不建议再继续使用Python 2,使用Python 2的同学请尽快升级到Python3
(ansible) ansible@ansible01:~$ pip install ansible

查看版本

(ansible) ansible@ansible01:~$ pip show ansible
Name: ansible
Version: 6.7.0
Summary: Radically simple IT automation
Home-page: https://ansible.com/
Author: Ansible, Inc.
Author-email: info@ansible.com
License: GPLv3+
Location: /home/ansible/pyvenv/ansible/lib/python3.8/site-packages
Requires: ansible-core
Required-by: 

基本概念介绍

安装完之后会有一系列的ansible命令,但是我仅介绍常用的命令。

# ansible最基本的命令,通过此命令来快速执行ansible的指令
$ ansible

# 前面有提到ansible官方版本并不会生成配置文件,此命令就是用来生成配置文件的
$ ansible-config

# 用于查看各模块的用法
$ ansible-doc

# Ansible数据文件的加密/解密,此部分将在后续博客中单独讲解
$ ansible-vault

# 执行各种角色和集合相关操作,此部分将在后续博客中单独讲解
$ ansible-galaxy

# 在目标主机上执行定义的复杂任务,此部分将在后续博客中单独讲解
$ ansible-playbook

关于互信

对于大家看过的大部分教程来说,当然是使用ssh密钥对来实现Ansible主控端与被控端的互信。但是在一些企业中,可能没有root用户来直接操作(对,你没看错,对于一些管理严格的企业就算是运维也没有root用户的权限),并且可能由于使用了LDAP(或相似技术),也无法使用密钥对来做互信,所以Ansible也提供了使用账号密码的形式来执行操作。

关于inventory

以下为Ansible官方的描述:

Ansible automates tasks on managed nodes or “hosts” in your infrastructure, using a list or group of lists known as inventory. You can pass host names at the command line, but most Ansible users create inventory files. Your inventory defines the managed nodes you automate, with groups so you can run automation tasks on multiple hosts at the same time. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.
The simplest inventory is a single file with a list of hosts and groups. The default location for this file is /etc/ansible/hosts. You can specify a different inventory file at the command line using the -i option or in configuration using inventory.
Ansible 使用称为清单的列表或列表组自动执行基础设施中托管节点或“主机”上的任务。 您可以在命令行中传递主机名,但大多数 Ansible 用户会创建清单文件。 您的清单定义了您自动化的托管节点和组,因此您可以同时在多个主机上运行自动化任务。 定义清单后,您可以使用模式来选择您希望 Ansible 运行的主机或组。
最简单的清单是一个包含主机和组列表的文件。 此文件的默认位置是 /etc/ansible/hosts。 您可以在命令行中使用 -i 选项或在配置中使用 inventory 指定不同的清单文件。

简单理解就是这个文件记录了需要执行操作的所有主机,并且可以通过分组来进行区分,且可以按照分组来执行任务,这个文件就是inventory。
比如说我在当前目录下的inventory文件

(ansible) ansible@ansible01:~$ cat hosts 
[ansible02]
10.0.2.7

[ansible03]
10.0.2.8

[ansible04]
10.0.2.9

如果使用账号密码来进行操作ansible的被控端,需要安装sshpass包。

(ansible) ansible@ansible01:~$ ansible all -i hosts -u ansible -k -m ping
# -i指定inventory文件, -u指定远程连接被控端执行指令的用户,-k输入远程连接时远端的ssh密码,-m指定模块,ping模块:验证远端主机的连通性,如果可连接将会反馈"pong"
# 因为当前的ansible还没有配置文件,所以此时的参数需要通过命令选项来传递
SSH password: 
10.0.2.7 | FAILED! => {
    "msg": "to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program"
}
10.0.2.8 | FAILED! => {
    "msg": "to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program"
}
10.0.2.9 | FAILED! => {
    "msg": "to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program"
}
(ansible) ansible@ansible01:~$ sudo apt install -y sshpass

# 由于ssh的特性,在对从未验证的主机进行连接时,将会默认需要验证对端主机的公钥指纹,由于ansible的原因,此时验证将会失败,就会导致ansible连接失败
(ansible) ansible@ansible01:~$ ansible all -i hosts -u ansible -k -m ping
SSH password: 
10.0.2.9 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}
10.0.2.7 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}
10.0.2.8 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}

(ansible) ansible@ansible01:~$ ansible all -i hosts -u ansible -k --ssh-common-args='-o StrictHostKeyChecking=no' -m ping
# --ssh-common-args='-o StrictHostKeyChecking=no' ,对于所有第一次连接的主机将会默认接受其公钥指纹
SSH password: 
10.0.2.9 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.0.2.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
10.0.2.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

总结

以上就是一次简单的ansible示范,主要是给部分没用过的同学来先熟悉以下ansible的用法,后续的博客将会循序渐进,介绍一些企业中更常用的方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ansible使用记录(一)——介绍、安装及简单使用 的相关文章

随机推荐

  • storyboard中自定义cell

    iOS开发中 xff0c 在storyboard中实现自定义cell比在nib文件中实现要简单许多 首先创建自己的自定义cell的类 xff0c 我们叫做CustomCell xff0c 要继承于UITableViewCell 在这个类中定
  • 记录自己 Ubuntu 20.04 安装 CUDA 及 Pytorch

    文章目录 一 安装相关驱动1 查看显卡型号2 查找显卡驱动3 禁用 nouveau3 1 检查是否已经禁用3 2 禁用 nouveau 的具体命令 二 安装 CUDA1 选择 CUDA 版本2 进行安装3 遇到了问题 最后一步4 又尝试了
  • macOS conda 安装指定版本的 Pytorch

    因为在 macOS 下用不了 CUDA 所以安装 Pytorch 时只能安装 CPU 版本的 此外 按照 Pytorch 官网给出的安装方式 网络太慢了 并且总是中断 所以考虑 清华的镜像网站https mirrors tuna tsing
  • Python ISBN号概述,校验位计算,10位-13位转换,验证,连字符,及常用库isbnid,isbnlib,isbntools的使用

    ISBN概述 因为工作原因经常需要用到ISBN ISBN是国际标准书号的简称 xff08 International Standard Book Number xff09 xff0c 主要用于标识文献 xff0c 也即是文献的 身份证号 x
  • 转: 两款优秀的服务器网络流量监控工具:Ntopng和Munin-功能强大直观

    如果发现自己的VPS服务器异常 xff0c 一般地我们可以从服务器日志中来分析 xff0c 看看是不是有不守 规矩 的IP来源 例如 xff0c 我们可以使用服务器日志分析利器 ngxtop和GoAccess来分析统计日志当中的IP来源 连
  • VMware安装Kali操作系统(全网最详细不接受任何反驳)

    VMware安装Kali操作系统 xff08 全网最详细不接受任何反驳 xff09 1 Kali下载 xff08 1 xff09 进入kali官网https www kali org https www kali org xff08 2 x
  • 正割、余割、正弦、余弦、正切、余切之间的关系的公式 sec、csc与sin、cos、tan、cot之间的各种公式

    1 倒数关系 tan cot xff1d 1 sin csc xff1d 1 cos sec xff1d 1 2 商数关系 tan 61 sin cos cot 61 cos sin 3 平方关系 sin 43 cos 61 1 1 43
  • C++字符串拼接效率比较(+=、append、stringstream、spintf)

    事情起因很简单 xff0c 自己的代码中使用了stringstream对象进行字符串的拼接 xff0c 然后被老同事质疑效率低下 借着这个机会了解下为什么 xff1f 一 43 61 append stringsteam sprintf四种
  • 构造方法特征

    1 具有与类相同的名称 xff1b 2 不含返回类型 xff1b 3 不能在方法中用return语句返回一个值 xff0c 不能用void修饰构造方法 4 在类实例化的时候 xff0c 系统自动调用
  • java 删除字符串的一个字符

    删除字符串中的一个字符 xff0c 用的是substring xff08 xff09 函数 xff0c 参数有一个或许两个 xff0c 一个的时候代表从该字符数组中的第几个字符开始获取该参数字符后面的字符串 xff0c 从索引0开始 pub
  • Linux 下部署 Laravel 环境

    1 首先更新操作系统 xff0c 命令为yum update y 目的是为了使操作系统处于最新状态 xff1b 2 安装两个库EPEL库和Webtatic库 yum install epel release rpm Uvh https mi
  • InetAddress类的总结

    1 InetAddress类没有构造方法 xff0c 所以不能直接new出一个对象 xff1b 可以通过InetAddress类的静态方法获得InetAddress的对象 xff1b InetAddress getLocalHost Ine
  • EBS触发器

    FORM级触发器 PRE FORM该触发器是在用户双击功能后 xff0c 进入form前 WHEN NEW FORM INSTANCE该触发器是在用户一进入form时执行 WHEN FORM NAVIGATE该触发器在用户在form间切换时
  • EBS触发器执行顺序

    1 xff0e 当打开FORM时 xff1a xff08 1 xff09 PRE FORM xff08 2 xff09 PRE BLOCK xff08 BLOCK级 xff09 xff08 3 xff09 WHEN NEW FORM INS
  • 数据库优化法则

    1 有可能的话 xff0c 用一个语句处理多个更新 xff1b 尽量减少对同一个表的重复访问 2 易识别的语句有助于定位性能问题 xff08 例如给SQL语句加注释 xff0c oracle已经解决为sql语句自带注释的问题 xff09 3
  • C/C++/C#是否支持基于string的switch-case??

    C C 43 43 只支持整型值 用字符常量也可以 但编译器事实上也是把字符常量量转化成了整型值的 其实 xff1a C C 43 43 支持 int 和char xff0c switch语句中的case只能是常量 是整型 字符或枚举三种
  • 虚拟机下linux连接外网

    1虚拟机点编辑 选择虚拟网络编辑器 2按照下图配置 xff0c 注意如果自己以前更改过 xff0c 最好点击还原默认设置 3点击NET设置 xff0c 记住网关IP 这里是192 168 159 2 4选择虚拟机 设置网络适配器为NAT模式
  • Sublime Text 2.02注册码

    BEGIN LICENSE Andrew Weber Single User License EA7E 855605 813A03DD 5E4AD9E6 6C0EEB94 BC99798F 942194A6 02396E98 E62C997
  • 表格复制粘贴次数过多导致卡死解决方案

    卡死原因 每粘贴一次 for循环 一次 就会呈现几何的增加 每个字段部分字母出现重复的 解决方案 我再写个 循环 他每增加 重复的字段 我就重复的给删了 span class token comment 循环列表进行计算 span span
  • Ansible使用记录(一)——介绍、安装及简单使用

    文章目录 系列开篇说明正文环境介绍安装Linux版本安装官方版本安装 基本概念介绍关于互信关于inventory 总结 系列开篇说明 此次新开的一个自动化更新系列 xff0c 主要是因为最近工作中用的比较多 xff0c 而且Ansible在