以哈希值作为条件之一的可循环



我有两个剧本-外部。Yml和inner。下面是不能工作的代码。

outer.yml:


- name: outer
hosts: localhost

vars:
volume_names: ["abcd", "efgh"]

tasks:

- name: create hash values and call inner.yml
set_fact:
hash: "{{ 60000 | random(seed=item_name) }}"
include_tasks: inner.yml
loop: "{{ volume_names }}"
loop_control:
loop_var: item_name

inner.yml:

- name: inner
collection.snapshot:
gateway_host: "{{IP}}"
username: admin
password: pass
snapshot_name: "{{ hash }}"
vol_name: "{{ item_name }}"

我需要的是创建卷快照,但每个卷都有唯一的快照名称(这就是我使用哈希的原因),并在变量"volume_names"中对所有卷执行此操作。

上面的代码不工作,但我不确定为什么。任何想法?

谢谢!

不需要include、set_fact和loop_control。例如,

- hosts: localhost

vars:
volume_names: ["abcd", "efgh"]
tasks:
- debug:
msg: |
gateway_host: ip
username: admin
password: pass
snapshot_name: "{{ 60000|random(seed=item) }}"
vol_name: "{{ item }}"
loop: "{{ volume_names }}"

PLAY [localhost] ******************************************************************************
TASK [debug] **********************************************************************************
ok: [localhost] => (item=abcd) => 
msg: |-
gateway_host: ip
username: admin
password: pass
snapshot_name: "20203"
vol_name: "abcd"
ok: [localhost] => (item=efgh) => 
msg: |-
gateway_host: ip
username: admin
password: pass
snapshot_name: "39290"
vol_name: "efgh"
PLAY RECAP ************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

- collection.snapshot:
gateway_host: "{{ IP }}"
username: admin
password: pass
snapshot_name: "{{ 60000|random(seed=item) }}"
vol_name: "{{ item }}"
loop: "{{ volume_names }}"

最新更新