/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의 파티션을 가져옵니다.
- 사실을 모아라
- 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
- 컨트롤러에 /etc/fstab을 저장할 경로를 선언합니다.
fstab_path: "/tmp/test/{{ inventory_hostname }}/fstab"
그리고 파일을 가져오세요
- fetch:
src: /etc/fstab
dest: "{{ fstab_path }}"
flat: true
필터를 사용하세요커뮤니티.일반.jc사전 목록을 생성하려면fstab
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
- 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~에 대한