如何使用Ansible收集有关磁盘的事实 [英] How to gather facts about disks using Ansible

查看:185
本文介绍了如何使用Ansible收集有关磁盘的事实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写Ansible剧本,以识别RHEL机器上新添加的磁盘.计划是在创建新磁盘之前,在该时间点运行剧本并缓存磁盘.创建新磁盘后,将再次运行相同的剧本,并计算磁盘创建前后磁盘之间的差异.

I am trying to write an Ansible playbook which will identify newly added disks on a RHEL machine. The plan is to run the playbook and cache the disks at that point in time as a fact prior to creating the new disks. After creating the new disks, the the same playbook would be run again and would compute the difference in disks before and after the disks are created.

例如,lsblk最初返回以下内容:

For example, lsblk initially returns the following:

NAME              SIZE  TYPE
sda               100G  disk
├─sda1              1G  part
└─sda2             99G  part
  ├─rhel-root      50G  lvm
  ├─rhel-swap     7.9G  lvm
  └─rhel-home    41.1G  lvm
sr0              1024M  rom

添加8个新磁盘后,lsblk返回:

after adding 8 new disks, lsblk returns:

NAME              SIZE  TYPE
sda               100G  disk
├─sda1              1G  part
└─sda2             99G  part
  ├─rhel-root      50G  lvm
  ├─rhel-swap     7.9G  lvm
  └─rhel-home    41.1G  lvm
sdb              18.6G  disk
sdc              18.6G  disk
sdd              18.6G  disk
sde              18.6G  disk
sdf              18.6G  disk
sdg              18.6G  disk
sdh              18.6G  disk
sdi              18.6G  disk
sr0              1024M  rom

理想情况下,我可以收集以下形式的磁盘的初始列表:

Ideally I would be able to gather an initial list of disks of the form:

['sda']

在创建磁盘后,收集以下形式的磁盘列表:

and after creating the disks gather another list of disks of the form:

['sda', 'sdb', 'sdc', 'sdd', 'sde', 'sdf', 'sdg', 'sdh', 'sdi']

计算两个列表之间的差异将得出:

Computing the difference between the two lists would yield:

['sdb', 'sdc', 'sdd', 'sde', 'sdf', 'sdg', 'sdh', 'sdi']

这是8个新创建的磁盘.

which are the 8 newly created disks.

如果可能,我试图避免使用shellcommand模块调用.

I am trying to avoid using a shell or command module call if possible.

推荐答案

此信息通过ansible的事实收集机制.

This information is automatically gathered via ansible's fact gathering mechanism.

请参见从系统中发现的变量:事实.

例如:

#!/usr/bin/env ansible-playbook
- name: Lets look at some disks
  hosts: localhost
  become: false
  gather_facts: true
  tasks:
  - name: Output disk information
    debug:
      var: hostvars[inventory_hostname].ansible_devices

如果我们改为在设置模块上使用collect_subset配置相反,我们可以加快事实收集的速度,仅收集有关系统硬件的信息.

If we instead use the gather_subset configuration on the setup module instead we can speed up the fact gathering and only gather information about system hardware.

然后我们可以将其与python keys()方法和jinja2 list过滤器结合使用,以产生所需的输出.

We can then combine this with the python keys() method and the jinja2 list filter to produce your desired output.

#!/usr/bin/env ansible-playbook
- name: Lets look at some disks
  hosts: localhost
  become: false
  gather_facts: false
  tasks:
  - name: Collect only facts about hardware
    setup:
      gather_subset:
      - hardware

  - name: Output disks
    debug:
      var: hostvars[inventory_hostname].ansible_devices.keys() | list

还可以配置要在ansible配置文件中收集的事实使用[defaults]部分中的gather_subset键="nofollow noreferrer"> ansible.cfg .

It is also possible to configure which facts to gather in the ansible configuration file ansible.cfg using the gather_subset key in the [defaults] section.

如果要过滤掉各种磁盘类型,最简单的方法是使用map('regex_search', '*search string*')提取所需的值.您可以通过select('string')删除空值.

If you wanted to filter out various disk types the easiest way would be to use map('regex_search', '*search string*') to extract the values you want. You can the remove the nulls via select('string').

例如,使用sd *形式的磁盘:

For example with disks of the form sd*:

#!/usr/bin/env ansible-playbook
- name: Lets look at some disks
  hosts: localhost
  become: false
  gather_facts: false
  tasks:
  - name: Collect only facts about hardware
    setup:
      gather_subset:
      - hardware

  - name: Output disks
    debug:
      var: hostvars[inventory_hostname].ansible_devices.keys() | map('regex_search', 'sd.*') | select('string') | list

这篇关于如何使用Ansible收集有关磁盘的事实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆