如何使用Ansible计算有关主机(如OS版本)的统计信息?

如何使用Ansible计算有关主机(如OS版本)的统计信息?,ansible,Ansible,我想获得一些关于操作系统版本或其他可用主机属性的统计数据 理想情况下,我希望获得如下信息: debian 8 - 10 machines debian 7 - 5 machine centos 7 - 1 machine 使用Ansible很难完全以这种格式获取它。我认为最好的办法是创建一个自定义回调插件。否则,您将始终拥有任务及其状态等的Ansible典型输出格式 如果格式实际上并不重要,而您只是想要信息,您可以这样做: - group_by: key=sys_{{ ansible_dist

我想获得一些关于操作系统版本或其他可用主机属性的统计数据

理想情况下,我希望获得如下信息:

debian 8 - 10 machines
debian 7 - 5 machine
centos 7 - 1 machine

使用Ansible很难完全以这种格式获取它。我认为最好的办法是创建一个自定义回调插件。否则,您将始终拥有任务及其状态等的Ansible典型输出格式

如果格式实际上并不重要,而您只是想要信息,您可以这样做:

- group_by: key=sys_{{ ansible_distribution }}_{{ ansible_distribution_major_version }}

- debug: msg="{{ groups[item] | length }}"
  with_items: groups
  delegate_to: localhost
  run_once: true
  when: item | match("^sys_")
TASK [debug] *******************************************************************
ok: [some.host -> localhost] => (item=sys_Debian_8) => {
    "item": "sys_Debian_8", 
    "msg": "10"
}

ok: [some.host -> localhost] => (item=sys_Debian_7) => {
    "item": "sys_Debian_7", 
    "msg": "2"
}
ok: [some.host -> localhost] => (item=sys_CentOS_7) => {
    "item": "sys_CentOS_7", 
    "msg": "1"
}
这将输出如下内容:

- group_by: key=sys_{{ ansible_distribution }}_{{ ansible_distribution_major_version }}

- debug: msg="{{ groups[item] | length }}"
  with_items: groups
  delegate_to: localhost
  run_once: true
  when: item | match("^sys_")
TASK [debug] *******************************************************************
ok: [some.host -> localhost] => (item=sys_Debian_8) => {
    "item": "sys_Debian_8", 
    "msg": "10"
}

ok: [some.host -> localhost] => (item=sys_Debian_7) => {
    "item": "sys_Debian_7", 
    "msg": "2"
}
ok: [some.host -> localhost] => (item=sys_CentOS_7) => {
    "item": "sys_CentOS_7", 
    "msg": "1"
}
由于调试任务循环到Ansible已知的所有组,因此它也将循环到您在清单中定义的所有组以及
all
ungroup
。上面的示例在:item | match(^sys)时对此进行过滤(
),但跳过的项仍将作为跳过的任务可见

但如果您有一组固定的组,您可以简单地筛选这些组:

with_items: groups | difference(['all', 'ungrouped', 'all-your-other-groups-here'])

使用Ansible很难完全以这种格式获取它。我认为最好的办法是创建一个自定义回调插件。否则,您将始终拥有任务及其状态等的Ansible典型输出格式

如果格式实际上并不重要,而您只是想要信息,您可以这样做:

- group_by: key=sys_{{ ansible_distribution }}_{{ ansible_distribution_major_version }}

- debug: msg="{{ groups[item] | length }}"
  with_items: groups
  delegate_to: localhost
  run_once: true
  when: item | match("^sys_")
TASK [debug] *******************************************************************
ok: [some.host -> localhost] => (item=sys_Debian_8) => {
    "item": "sys_Debian_8", 
    "msg": "10"
}

ok: [some.host -> localhost] => (item=sys_Debian_7) => {
    "item": "sys_Debian_7", 
    "msg": "2"
}
ok: [some.host -> localhost] => (item=sys_CentOS_7) => {
    "item": "sys_CentOS_7", 
    "msg": "1"
}
这将输出如下内容:

- group_by: key=sys_{{ ansible_distribution }}_{{ ansible_distribution_major_version }}

- debug: msg="{{ groups[item] | length }}"
  with_items: groups
  delegate_to: localhost
  run_once: true
  when: item | match("^sys_")
TASK [debug] *******************************************************************
ok: [some.host -> localhost] => (item=sys_Debian_8) => {
    "item": "sys_Debian_8", 
    "msg": "10"
}

ok: [some.host -> localhost] => (item=sys_Debian_7) => {
    "item": "sys_Debian_7", 
    "msg": "2"
}
ok: [some.host -> localhost] => (item=sys_CentOS_7) => {
    "item": "sys_CentOS_7", 
    "msg": "1"
}
由于调试任务循环到Ansible已知的所有组,因此它也将循环到您在清单中定义的所有组以及
all
ungroup
。上面的示例在:item | match(^sys)
时对此进行过滤(
),但跳过的项仍将作为跳过的任务可见

但如果您有一组固定的组,您可以简单地筛选这些组:

with_items: groups | difference(['all', 'ungrouped', 'all-your-other-groups-here'])