python - Ansible:根据特定事实对主机进行计数

标签 python ubuntu ansible yaml

我正在使用 Ansible 2.9.13 来管理大约 250 个 ubuntu 客户端。 如果我列出一位客户的所有事实,其中一行是:

"distribution_release": "bionic",

我现在想做的是计算机器的数量,这些机器是仿生的、焦点的以及它可能找到的任何其他版本。 所以,像这样:

bionic:  180
focal:   42
precise: 2

这可能吗?如果可以,如何实现?

最佳答案

您可以使用group_by动态创建新的动态组。

---
 - name: Count hosts based on ansible_distribution_release
   hosts: "{{ target|default('all') }}"
   gather_facts: true

   tasks:
    - name: Create dynamic groups
      group_by:
        key: dist_release_{{ ansible_distribution_release }}

 - name: Make the statistics
   hosts: localhost
   gather_facts: false

   tasks:
     - name: Sample output
       debug:
         msg: "Group {{ item }} has {{ groups[item] | length }} hosts."
       when: item.startswith('dist_release_')
       loop: "{{ groups|flatten(levels=1) }}"

第一个播放基于 {{ ansible_distribution_release }} 创建动态组,所有组均以前缀 dist_ 开头。

在第二个游戏中,动态组将仅用于创建一个相当丑陋但有效的组统计数据以及组内主机的数量。

如果您愿意,可以使用 Jinja2 模板创建一个不错的文件输出。

关于python - Ansible:根据特定事实对主机进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64801713/

相关文章:

bash - 在ansible中转义特殊字符

python - 从 Python 的 Bokeh 到 Latex 的交互式 HTML 绘图

python - 列表理解无用变量

linux - 如何查看linux系统是否使用了较大的磁盘空间?

mysql - 我的 mysql JOIN 有什么问题,我没有得到正确的结果

PHP+Ubuntu 使用gmail form localhost发送邮件

python - 为什么 Python CSV 阅读器会忽略双引号字段?

python - 为什么我不能使用 distutils 在 Python 发行版中包含这些数据文件?

ansible - 如何使用与 git url 不同的名称安装 galaxy 角色?

docker - Ansible 并重新启动 docker 服务