[
{
"Hostname": "cavia",
"Farm": "paolo",
"Cluster": "paperino",
"Gateway": "10.14.35.1",
"Dns": "172.26.5.110,172.26.5.111,172.26.16.11,172.26.16.12",
"Routes": "0",
"Network": [
{
"MAC":"00:50:56:b6:c0:db",
"Vlan":"107 - 10.14.8.0/24 - BE WW TF",
"Scope": "Production",
"IP": "10.14.35.9",
"MASK": "255.255.255.224"
},
{
"MAC":"50:56:b6:19:0c",
"Vlan":"5 - 10.4.81.0/24 - BE WW TF",
"Scope": "BE",
"IP": "10.4.5.9",
"MASK": "255.255.255.224"
},
{
"MAC":"00:50:56:b6:19:aa",
"Vlan":"4 - 0.14.81.0/24 - BE WW TF",
"Scope": "NFS",
"IP": "172.10.0.5",
"MASK": "255.255.0.0"
},
{
"MAC":"00:50:b6:19:0c",
"Vlan":"10087 - 10.14.81.0/24 - BE WW TF",
"Scope": "Backup",
"IP": "10.4.96.28",
"MASK": "255.255.254.0"
}
],
"Disk": [
{
"ID": "36000c29ccb2f18976786181535e88772",
"Scope": "New",
"DiskFs": "/prova"
},
{
"ID": "36000c29ccb2f18976786181535e86553",
"Scope": "New",
"DiskFs": "/pippo"
}
]
}
]
实际上,我能够: -从json中获取磁盘ID,然后在系统上查找该ID -如果上一步确实起作用,则剧本会创建分区,vg,lvol,文件系统并挂载文件系统 -我所缺少的是..仅当json文件中的Scope为“ New”时才必须执行这些操作。
我分享了剧本的详细信息:
---
- name: "Phase 4 : Filesystem Configuration"
hosts: just_parsed
data: "{{ lookup('file', '../data/data.json') }}"
gather_facts: true
tasks:
- set_fact:
disklen: "{{ data[0].Disk | length }}"
# - debug:
# var: data[0].Disk[{{ item }}].DiskFs
# with_sequence: start=0 end={{ disklen|int -1 }}
- name: Clearing any existing mountpoint
file:
path: "{{ data[0].Disk[item | int() ].DiskFs }}"
state: absent
when: data[0].Disk[item | int() ].Scope =='New'
with_sequence: start=0 end={{ disklen|int -1 }}
become: true
become_method: sudo
- name: Creating new mountpoint
file:
path: "{{ data[0].Disk[item | int() ].DiskFs }}"
state: directory
mode: '0755'
when: data[0].Disk[item | int() ].Scope =='New'
with_sequence: start=0 end={{ disklen|int -1 }}
become: true
become_method: sudo
- name: Creating partitions
parted:
device: "/dev/{{ item.1.dev }}"
number: 1
flags: [ lvm ]
label: msdos
state: present
become: true
become_method: sudo
loop: "{{ data[0].Disk|
json_query('[].{dev: DiskFs, id: ID}')|
product(
ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}'))|
list }}"
when: item.1.ids|map('search', item.0.id) is any
- name: Creating volume groups
lvg:
vg: "{{ item.0.dev | basename }}-vg"
pvs: "/dev/{{ item.1.dev }}1"
become: true
become_method: sudo
loop: "{{ data[0].Disk|
json_query('[].{dev: DiskFs, id: ID}')|
product(
ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}'))|
list }}"
when: item.1.ids|map('search', item.0.id) is any
- name: Creating logical volumes
lvol:
vg: "{{ item.0.dev | basename }}-vg"
lv: "{{ item.0.dev | basename }}-vol"
size: 100%FREE
become: true
become_method: sudo
loop: "{{ data[0].Disk|
json_query('[].{dev: DiskFs, id: ID}')|
product(
ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}'))|
list }}"
when: item.1.ids|map('search', item.0.id) is any
- name: Creating filesystems
filesystem:
fstype: xfs
dev: "/dev/{{ item.0.dev | basename }}-vg/{{ item.0.dev | basename }}-vol"
become: true
become_method: sudo
loop: "{{ data[0].Disk|
json_query('[].{dev: DiskFs, id: ID}')|
product(
ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}'))|
list }}"
when: item.1.ids|map('search', item.0.id) is any
- name: Mounting filesystems
mount:
path: "{{ item.0.dev }}"
src: "/dev/{{ item.0.dev | basename }}-vg/{{ item.0.dev | basename }}-vol"
fstype: xfs
state: mounted
become: true
become_method: sudo
loop: "{{ data[0].Disk|
json_query('[].{dev: DiskFs, id: ID}')|
product(
ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}'))|
list }}"
when: item.1.ids|map('search', item.0.id) is any
关于data[0].Disk[i].Scope
的条件如何写?当磁盘ID匹配时,仅在该磁盘的范围为“新建”时才需要执行所有操作。
谢谢大家
答案 0 :(得分:0)
为了进行测试,我已将此项目添加到磁盘data[0].Disk
的列表中。
{
"ID": "5cd2e42981b06cef",
"Scope": "New",
"DiskFs": "/pippo2"
}
下面的任务
- debug:
msg: "{{ item.0.dev }} mounted to device
{{ item.1.dev }} scope
{{ item.0.scope }}"
loop: "{{ data.0.Disk|
json_query('[].{dev: DiskFs, id: ID, scope: Scope}')|
product(
ansible_facts.devices|dict2items|
json_query('[].{dev: key, ids: value.links.ids}'))|
list
}}"
when:
- item.1.ids|map('search', item.0.id) is any
- item.0.scope == "New"
给予
msg: /pippo2 mounted to device nvme0n1 scope New
详细说明,剧本
- hosts: localhost
vars:
data: "{{ lookup('file', 'test4-data.json') }}"
tasks:
- set_fact:
list_fs: "{{ data.0.Disk|
json_query('[].{dev: DiskFs,
id: ID,
scope: Scope}') }}"
- debug:
var: list_fs
- set_fact:
list_disk: "{{ ansible_facts.devices|
dict2items|
json_query('[].{dev: key,
ids: value.links.ids}') }}"
- debug:
var: list_disk
- debug:
msg: "{{ item.0.dev }} mounted to device
{{ item.1.dev }} scope
{{ item.0.scope }}"
loop: "{{ list_fs|product(list_disk)|list }}"
when:
- item.1.ids|map('search', item.0.id) is any
- item.0.scope == "New"
给予
list_fs:
- dev: /prova
id: 36000c29ccb2f18976786181535e88772
scope: New
- dev: /pippo
id: 36000c29ccb2f18976786181535e86553
scope: New
- dev: /pippo2
id: 5cd2e42981b06cef
scope: New
list_disk:
- dev: mmcblk0
ids:
- mmc-SU16G_0x1ccbfa6f
- dev: nvme0n1
ids:
- nvme-SSDPEKKF256G8_NVMe_INTEL_256GB_BTHH832111P1256B
- nvme-eui.5cd2e42981b06cef
- dev: loop3
ids: []
- dev: loop2
...
msg: /pippo2 mounted to device nvme0n1 scope New