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

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

问题描述

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

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

如果我们在设置模块上使用gather_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 配置文件中收集哪些事实 ansible.cfg 使用 [defaults] 部分中的 gather_subset 键.

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天全站免登陆