如何根据变量中定义的 UUID 从文件中打印特定行?

如何根据变量中定义的 UUID 从文件中打印特定行?

我想/etc/fstab根据变量中定义的 UUID 打印特定行。但是当我尝试使用以下代码时,它没有给出正确的结果。

代码:

---
- name: check
  hosts: all
  become: true

  tasks:

    - name: set fact for uuid
      set_fact:
        uuid_disk: "{{ item.value.links.uuids }}"
      loop: "{{ ansible_devices | dict2items }}"
      when: ('swap' in (item.value.links.labels))

    - name: print
      debug:
        msg: "{{ uuid_disk | join() }}"

    - name: fstab
      slurp:
        src: /etc/fstab
      register: output

    - name: print
      debug:
        msg: "{{ item | select('match','UUID') | list }}"
      loop: "{{ (output.content | b64decode).splitlines() }}"

    - name: print
      debug:
        msg: "{{ item }}"
      loop: "{{ (output.content | b64decode).splitlines() }}"
      when:  (item | regex_search('^UUID')) in uuid_disk

答案1

问:“根据变量中定义的 UUID 从文件中打印行。”

A:获取文件并选择行。例如,声明在控制器上存储 /etc/fstab 的路径

  fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"

并获取文件

    - fetch:
        src: /etc/fstab
        dest: "{{ fstab_path }}"
        flat: true

给定具有 UUID 值的变量

  uuid_disk: 01865fce-8bb9-48ad-a9eb-1ff43a8db4a5

搜索线路

  uuid_line: "{{ lookup('file', fstab_path).splitlines()|
                 select('search', uuid_disk) }}"

例如,

  uuid_line:
  - UUID=01865fce-8bb9-48ad-a9eb-1ff43a8db4a5 none swap sw 0 0

完整测试剧本的示例

- hosts: all

  vars:

    uuid_disk: 01865fce-8bb9-48ad-a9eb-1ff43a8db4a5

    fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"
    uuid_line: "{{ lookup('file', fstab_path).splitlines()|
                   select('search', uuid_disk) }}"

  tasks:

    - fetch:
        src: /etc/fstab
        dest: "{{ fstab_path }}"
        flat: true
    - debug:
        var: uuid_line

上述解决方案打印出标记为交换(这就是你获取 UUID 的方式。)反之亦然,你可以打印用作交换在 fstab 中。让我重新表述一下这个问题

问:“从 /etc/fstab 获取交换分区的 UUID。然后,显示该分区。”

答:首先收集事实并创建字典 UUID/分区。然后,解析 /etc/fstab 并获取交换条目的 UUID。获取 UUID 的分区。

  1. 收集事实
    - setup:
        gather_subset: devices

,获取分区,并创建字典唯一标识

  partitions: "{{ ansible_devices|
                  json_query('*.partitions')|
                  combine }}"
  uuid: "{{ dict(partitions|
                 dict2items|
                 selectattr('value.uuid')|
                 json_query('[].[value.uuid, key]')) }}"

例如,

  uuid:
    01865fce-8bb9-48ad-a9eb-1ff43a8db4a5: sdb4
    04dc9170-bdbc-4a22-abaf-b9e3cf1ba969: sda5
    7074BA0A74B9D2D8: sda2
    9a2199dd-0662-47b4-a957-adbcf5d350f4: sdb5
    EC808480808452CE: sda1
    F86C-A380: sdb2
    c484594d-fd2e-4f57-9c14-74b8e397d8ed: sdb3
  1. 声明控制器上存储 /etc/fstab 的路径
  fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"

并获取文件

    - fetch:
        src: /etc/fstab
        dest: "{{ fstab_path }}"
        flat: true

使用过滤器社区.general.jc创建字典列表文件系统

  fstab: "{{ lookup('file', fstab_path)|
             community.general.jc('fstab') }}"

例如,

  fstab:
  - fs_file: /
    fs_freq: 0
    fs_mntops: errors=remount-ro
    fs_passno: 1
    fs_spec: UUID=c484594d-fd2e-4f57-9c14-74b8e397d8ed
    fs_vfstype: ext4
  - fs_file: /boot/efi
    fs_freq: 0
    fs_mntops: umask=0077
    fs_passno: 1
    fs_spec: UUID=F86C-A380
    fs_vfstype: vfat
  - fs_file: /export
    fs_freq: 0
    fs_mntops: defaults
    fs_passno: 2
    fs_spec: UUID=9a2199dd-0662-47b4-a957-adbcf5d350f4
    fs_vfstype: ext4
  - fs_file: none
    fs_freq: 0
    fs_mntops: sw
    fs_passno: 0
    fs_spec: UUID=01865fce-8bb9-48ad-a9eb-1ff43a8db4a5
    fs_vfstype: swap
  - fs_file: none
    fs_freq: 0
    fs_mntops: sw
    fs_passno: 0
    fs_spec: /usr/swap0
    fs_vfstype: swap

获取交换条目的 UUID

  uuid_swap: "{{ fstab|
                 selectattr('fs_vfstype', '==', 'swap')|
                 selectattr('fs_spec', 'match', 'UUID=')|
                 map(attribute='fs_spec')|
                 map('split', '=')|
                 map('last') }}"

给出

  uuid_swap:
    - 01865fce-8bb9-48ad-a9eb-1ff43a8db4a5
  1. 获取 UUID 的分区
  partition_swap: "{{ uuid_swap|map('extract', uuid) }}"

给出

 partition_swap:
    - sdb4

完整测试剧本的示例

- hosts: all

  vars:

    partitions: "{{ ansible_devices|
                    json_query('*.partitions')|
                    combine }}"
    uuid: "{{ dict(partitions|
                   dict2items|
                   selectattr('value.uuid')|
                   json_query('[].[value.uuid, key]')) }}"

    fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"
    fstab: "{{ lookup('file', fstab_path)|
               community.general.jc('fstab') }}"
    uuid_swap: "{{ fstab|
                   selectattr('fs_vfstype', '==', 'swap')|
                   selectattr('fs_spec', 'match', 'UUID=')|
                   map(attribute='fs_spec')|
                   map('split', '=')|
                   map('last') }}"
    partition_swap: "{{ uuid_swap|map('extract', uuid) }}"

  tasks:

    - setup:
        gather_subset: devices
    - debug:
        var: uuid

    - fetch:
        src: /etc/fstab
        dest: "{{ fstab_path }}"
        flat: true
    - debug:
        var: fstab

    - debug:
        var: uuid_swap
    - debug:
        var: partition_swap

答案2

关于

如何根据变量中定义的 UUID 从文件中打印特定行?

你可以看看下面的最小示例剧本

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    uuid_disk: 12345678-abcd-efgh-ijkl-123456789012

  tasks:

  - name: fstab
    slurp:
      src: /etc/fstab
    register: output

  - name: Print UUID, if there is any
    debug:
      msg: "{{ item | regex_search('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}') }}"
    loop: "{{ (output.content | b64decode).splitlines() }}"

  - name: Print line where UUID match
    debug:
      msg: "{{ item }}"
    loop: "{{ (output.content | b64decode).splitlines() }}"
    when: uuid_disk in item

类似问答关于

相关内容