【问题标题】:Filter Centos 6 only with Ansible setup and filter | not using ansible-playbook仅使用 Ansible 设置和过滤器过滤 Centos 6 |不使用 ansible-playbook
【发布时间】:2021-03-05 14:58:44
【问题描述】:

我正在尝试列出所有 Centos 6 服务器并准备升级其操作系统。

对于单个命令,我使用:

# ansible myhost -b -m setup -a 'filter=ansible_distribution_major_version'

如您所知,它只是列出了所有 Centos 6、7、8,但我只想要 Centos 6 服务器。如何在单个命令中过滤它以便方便而不是 YAML 文件并使用 ansible-playbook 运行它。

【问题讨论】:

    标签: ansible ansible-2.x ansible-facts


    【解决方案1】:

    这是您可以适应其他类型信息的一种可能方式。

    1. 启用事实缓存,以便您可以收集一次事实并重复使用(在剧本或临时命令中)。这是一个最小的ansible.cfg 文件,用于将缓存信息存储在磁盘上的 json 文件中。其他可用插件见documentation

      [defaults]
      fact_caching=jsonfile
      fact_caching_connection=/tmp/ansibleFactCache
      

      将该文件存储在当前目录中,您将从该目录中发出临时命令和 playbook 命令

    2. 为所有主机填充事实缓存:

      ansible all -i your/inventory.yml -m setup
      
    3. 您现在可以使用相同的清单并仅选择与某些条件匹配的主机名来发出以 localhost 为目标的调试临时任务。 CentOS 6 的示例就像您的问题一样:

      $ ansible localhost -i your/inventory.yml -m debug -a msg="{{ hostvars | dict2items | map(attribute='value') | selectattr('ansible_distribution', 'eq', 'CentOS') | selectattr('ansible_distribution_major_version', 'eq', '6') | map(attribute='inventory_hostname') }}"
      localhost | SUCCESS => {
          "msg": [
              "host_a",
              "host_d"
          ]
      }
      

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 2023-03-23
      • 2017-10-19
      • 1970-01-01
      相关资源
      最近更新 更多