変数で定義された 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 を取得します。次に、パーティションを表示します。」

A: まず事実を収集し、UUID/パーティションの辞書を作成します。次に、/etc/fstab を解析して、スワップ エントリの UUID を取得します。UUID のパーティションを取得します。

  1. 事実を集める
    - setup:
        gather_subset: devices

パーティションを取得し、辞書を作成するユーザID

  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

フィルターを使用するコミュニティ辞書のリストを作成するスタバ

  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

そして類似のQ&Aについて

関連情報