4.Ansible Inventory介绍及实战 - A list or group of lists nodes

news/2025/2/19 8:07:29/
  1. 什么是inventory
    官方解释:Ansible automates tasks on managed nodes or “hosts” in your infrastructure, using a list or group of lists known as inventory.
    在这里插入图片描述
    Ansible可以同时与您基础设施中的一个或多个系统协同工作。为了与多台服务器协同工作, Ansible需要与这些服务器建立连接。这是通过使用SSH(适用于Linux)和PowerShell远程处理(适用于Windows)来完成的。 这就是Ansible无代理的原因。无代理意味着您不需要在目标机器上安装任何额外的软件,就可以使用Ansible。一个简单的SSH连接就能满足Ansible的需求。
    大多数其他编排工具的主要缺点之一是,在调用任何类型的自动化之前, 都需要在目标系统上配置代理。现在, 有关这些目标系统的信息存储在inventory文件中。如果您不创建新的inventory文件,Ansible将使用位于etc/Ansible/hosts位置的默认库存文件。

    让我们看一个示例Inventory文件。它只是一个接一个地列出了许多服务器,是ini格式的。您还可以将不同的服务器分组在一起,方法是在方括号内的组名下定义服务器,并在下面的行中定义属于该组的服务器列表。可以在单个Inventory文件中定义多个组。
    在这里插入图片描述
    让我们仔细看看Inventory文件。例如, 我有一个服务器列表,名称从14。但是, 我想在Ansible中使用别名(如Web服务器或数据库服务器)来引用这些服务器。我可以通过在行首添加每个服务器的别名,并将该服务器的地址分配给Ansibleansible_host参数来实现这一点。ansible_host是一个inventory参数, 用于指定服务器的域名或IP地址。在这里插入图片描述
    还有其他参数。
    在这里插入图片描述
    其中包括ansible_connection, ansible_port, ansible_useransible_ssh_pass

    1. ansible_connection: 定义了Ansible连接到目标服务器的方式。这是到Linux服务器的ssh连接或者到Windows服务器的winrm。这就是我们定义要连接的目标主机是Linux主机还是Windows主机的方式。
      您也可以将其设置为localhost,以表示我们希望使用本地主机,而不连接到任何远程主机。如果您没有多个服务器可供使用, 则可以简单地从清单文件中的本地主机开始。
    2. ansible_port:定义要连接的端口。默认情况下, 它被设置为SSH的端口22,但如果您需要更改, 您可以使用这个参数进行不同设置。
    3. ansible_user: 定义用于进行远程连接的用户。默认情况下,对于Linux计算机, 设置为root。果需要更改此设置, 可以用这个参数。
    4. ansible_ssh_pass: 定义了Linuxssh密码。请注意, 以这种纯文本格式存储密码可能不是很理想。最佳实践是在服务器之间设置基于SSH密钥的无密码身份验证,并且您绝对应该在生产或公司环境中这样做。 如果是windows,使用ansible_password
      但现在, 我们想从Ansible的基本知识开始,而不想过多地讨论安全性或其他主题。因此, 首先, 我们将从用户名和密码的基本设置开始。
  2. inventory实战
    1)打开宿主机mac上的terminalssh进入到ansiblecontroller,然后通过vim命令创建一个inventory.txt文件,写入如下的配置。这里指定了target1作为别名,可以在Ansible中用别名来访问这台虚拟机;同时指定了ssh连接的密码(通常公司的系统上不推荐使用这种方式,应该用ssh key,实现无密码访问)
    在这里插入图片描述
    2)使用Ansible使用ping测试controllertarget1是否能连通,同时带上inventory配置文件

    ansible target1 -m ping -i inventory.txt
    

    幸运的是,你可能会遇到下面这个错误:

    target1 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possiblebecause Host Key checking is enabled and sshpass doesnot support this.  Please add this host's fingerprint to yourknown_hosts file to manage this host."
    }
    

    在这里插入图片描述
    解决方式有两种:

    1. ansible-controller手动ssh连接到ansible-target1,然后手动接受fingerprint
      在这里插入图片描述
    2. ansible-controller的配置文件/etc/ansible/ansible.cfg,并且搜host key,将host_key_checking标记为False,默认是true。(不推荐这样干)
      # uncomment this to disable SSH key host checking
      #host_key_checking = False
      
  1. 接受fingerprint完成之后,继续使用ansible target1 -m ping -i inventory.txt测试连通性
    很幸运的是,又可能又会遇到下面这个错误:
    一般是因为指定的inventory文件路径不对,-i读取的是当前所在路径下的inventory.txt,也就是/etc/ansible/inventory.txt,实际上我们创建的inventory.txt是在用户目录。

    [osboxes@ansiblecontroller ansible]$ ansible target1 -m ping -i inventory.txt
    [WARNING]: Unable to parse /etc/ansible/inventory.txt as an inventory source
    [WARNING]: No inventory was parsed, only implicit localhost is available
    [WARNING]: provided hosts list is empty, only localhost is available. Note that
    the implicit localhost does not match 'all'
    [WARNING]: Could not match supplied host pattern, ignoring: target1
    

    cd ~,然后再跑上面的ansible命令,看见下面类似的返回,就表示成功了

    [osboxes@ansiblecontroller ~]$ ansible target1 -m ping -i inventory.txt
    target1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
    }
    
  1. 最后给一个完整的inventory例子,包含group的用法:

    # Sample Inventory File# Web Servers
    web_node1 ansible_host=web01.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
    web_node2 ansible_host=web02.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
    web_node3 ansible_host=web03.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass# DB Servers
    sql_db1 ansible_host=sql01.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass
    sql_db2 ansible_host=sql02.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass[db_nodes]
    sql_db1
    sql_db2[web_nodes]
    web_node1
    web_node2
    web_node3[boston_nodes]
    sql_db1
    web_node1[dallas_nodes]
    sql_db2
    web_node2
    web_node3
    

    更多关于Ansible的文章,请参考我的Ansible专栏:https://blog.csdn.net/u011069294/category_12331290.html


http://www.ppmy.cn/news/90528.html

相关文章

EasyRecovery16绿色版安装下载及使用教程

如果你已经在下载了PC版本的EasyRecovery,那么该如何安装EasyRecovery呢?现在就呈上EasyRecovery教程,以便顺利完成安装。EasyRecovery不仅能够恢复多种类型的数据,更能够适用于不同媒体介质,其中包括计算机&#xff0…

总结MySQL 的一些知识点:MySQL 连接的使用

MySQL 连接的使用 在前几章节中,我们已经学会了如何在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据。 本章节我们将向大家介绍如何使用 MySQL 的 JOIN 在两个或多个表中查询数据。 你可以在 SELECT…

麓言信息 学习UI设计师有没有前途

而目前市场上的UI设计师大多从美工、平面设计师等转岗,自身缺乏系统的Ui设计培训和学习,很难适应当前企业的高规格要求,也导致了一名合格的Ui设计师在社会上是十分值钱、十分抢手的。现在越来越多的年轻群体已经把求职的目光聚焦在UI设计领域…

List<Model> distinct 不起作用

在对 List<Model> 使用 Distinct() 方法时&#xff0c;如果没有得到预期的结果&#xff0c;可能是由于你的 Model 类型没有正确重写 Equals() 和 GetHashCode() 方法所导致的。 当调用 Distinct() 方法时&#xff0c;它使用对象的哈希码和相等性来确定哪些元素是独特的。…

四.Retrofit

文章目录 前言1.如何封装OkHttp2.Retrofit的设计思想3.ServiceMethod存在的价值4.Retrofit的整理流程5.Retrofit使用到了哪些设计模式面试题 前言 核心思想就是AOP思想&#xff0c;面向切面编程。 AOP使用场景比如&#xff1a;LeakCanary、BlockCanary、Matrix、LifeCycle、Ok…

GMesh网格选项介绍

GMesh网格介绍 2D mesh algorithm MeshAdapt&#xff1a;这是一种自适应网格算法&#xff0c;可在需要更大的精度或在某些区域需要更密集的网格时自动添加额外的网格。该算法的优点包括较高的收敛性和灵活性&#xff0c;它可以让用户在需要的地方添加更多的网格&#xff0c;但…

[RSA议题分析] eBPF Warfare - Detecting Kernel eBPF Rootkits with Tracee

文章目录 简介议题分析基础知识用户空间与内核空间hookrootkit追踪技术 eBPF架构Tracee - 一个运行时安全检查工具 RootKit种类与各个阶段的攻防LD_PRELOAD RootKitKernel Module RooKitKernel RootKit HidingKernel RootKit Hooksyscall table hookingfile operations hooking…

App 软件开发《判断1》试卷答案及解析

《判断1》试卷答案及解析 注&#xff1a;本文章所有答案及解析均来自 ChatGPT 的回答&#xff0c;正确性请自行甄辨。 文章目录 《判断1》试卷答案及解析判断题&#xff08;对的打“√”&#xff0c;错的打“”&#xff1b;共0分&#xff09;1&#xff0e;原生App内部运行的是二…