如何根據變數中定義的 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 行交換。 (這就是獲取 UUID 的方法。)反之亦然,您可以列印用作的分割區交換在 fstab 中。讓我重新表達這個問題

問:“從 /etc/fstab 取得交換分區的 UUID。然後,顯示分區。”

答:首先收集事實並建立一個字典 UUID/分區。然後,解析 /etc/fstab 並取得交換條目的 UUID。取得 UUID 的分區。

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

,取得分區,並建立字典uuid

  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

類似問答關於

相關內容