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

  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 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."
    }
    

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

    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
    
  2. 动态Inventory文件
    上面的文件是静态的,每次都需要手动更改它,其实Inventory还可以是动态的,比如一个puthon脚本,运行的时候,这个脚本会返回一个数组。更多信息参考官方文档:https://docs.ansible.com/ansible/latest/inventory_guide/intro_dynamic_inventory.html#intro-dynamic-inventory

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值