我创建了一个角色来创建LVM VG并使其完全幂等和故障证明我想验证PV是否存在以及VG是否已经定义。
角色/ LVM /瓦尔/ main.yml
---
lvm_vgs:
- vg_name: drbdpool
vg_pvs: "{{ vg_drbdpool_pvs }}"
host_vars /主机名
---
vg_drbdpool_pvs: ['sdc1', 'sdd1']
角色/ LVM /任务/ main.yml
- name: Create LVM VG(s)
lvg:
vg: "{{ item.vg_name }}"
pvs: "{% for disk in item.vg_pvs %}/dev/{{ disk }}{% if not loop.last %},{% endif %}{% endfor %}"
state: present
when:
- item.vg_name not in ansible_lvm.vgs
- "{% for disk in item.vg_pvs %}ansible_devices[{{ disk | truncate(-1) }}]['partitions']{{ disk }} is defined{% endfor %}"
with_items: "{{ lvm_vgs }}"
为实现此目的,我添加了条件"{% for disk in item.vg_pvs %}ansible_devices[{{ disk | truncate(-1) }}]['partitions']{{ disk }} is defined{% endfor %}"
,但它不起作用,我总是收到以下错误:
任务[lvm:创建LVM VG] ************************************ **************
致命:[主机名]:失败! => {“failed”:true,“msg”:“条件检查'{%for disk in item.vg_pvs%} ansible_devices [{{disk | truncate(-1)}}] ['partitions'] {{disk}}定义{%endfor%}'失败。错误是:意外'。'\ n第1行\ n \ n错误似乎出现在'/etc/ansible/roles/lvm/tasks/main.yml'中:line 80,第3列,但可能在文件的其他位置,具体取决于确切的语法问题。\ n \ n违规行似乎是:\ n \ n \ n-名称:创建LVM VG \ n ^此处\ N“}
如何验证PV(分区)是否存在?
答案 0 :(得分:0)
Ansible假设when
的参数是一个简单的Jinja2表达式(它隐含地添加了{{
}}
括号),所以你不能在里面使用语句{% ... %}
。 / p>
解决方法是在任务中定义变量并使用变量名作为条件:
- name: Create LVM VG(s)
lvg:
vg: "{{ item.vg_name }}"
pvs: "{% for disk in item.vg_pvs %}/dev/{{ disk }}{% if not loop.last %},{% endif %}{% endfor %}"
state: present
when:
- item.vg_name not in ansible_lvm.vgs
- partition_exists
with_items: "{{ lvm_vgs }}"
vars:
partition_exists: "{% for disk in item.vg_pvs %}ansible_devices[{{ disk | truncate(-1) }}]['partitions']{{ disk }} is defined{% endfor %}"
我无法测试你的实际情况,所以我保持原样。
答案 1 :(得分:0)
- name: Create LVM VG(s)
lvg:
vg: "{{ item.vg_name }}"
pvs: "{% for disk in item.vg_pvs %}/dev/{{ disk }}{% if not loop.last %},{% endif %}{% endfor %}"
state: present
when:
- item.vg_name not in ansible_lvm.vgs
- partition_exists.split(';')
with_items: "{{ lvm_vgs }}"
vars:
partition_exists: "{% for disk in item.vg_pvs %}ansible_devices[{{ disk | truncate(-1) }}]['partitions']{{ disk }} is defined{% if not loop.last %};{% endif %}{% endfor %}"
tags: ['storage', 'lvm']
由于item.vg_pvs
可能包含多个元素,因此需要将变量partition_exists
创建为列表。