loops - 带有数字增量的 Ansible 双循环
问题描述
我有以下将 .p12 证书转换为 Java Keystore 的 ansible 任务。目前我正在遍历名为“java”的库存主机,并且还希望在遍历“java”主机时增加每个循环的 cert_alias 值(如 1、2 等)。我没有这个双循环的经验......有人可以建议如何实现这一点。
- name: Import a pkcs12 Keystore with a specified alias, create it if it doesn't exist
java_cert:
pkcs12_path: {{item}}_cert.p12
pkcs12_alias: test
pkcs12_password: test@123
cert_alias: 1 >>>>>>> Need this also to be looped
keystore_path: tlsKeyStore
keystore_type: "JKS"
keystore_pass: test@123
keystore_create: yes
loop: "{{ query('inventory_hostnames', 'java') }}"
解决方案
问:“增加每个循环的 cert_alias 值。 ”
A:使用扩展循环变量。例如,给定库存
shell> cat hosts
[java]
srv1
srv2
srv3
剧本
- hosts: all
gather_facts: false
tasks:
- debug:
msg: "[{{ item }}] cert_alias: {{ ansible_loop.index }}"
loop: "{{ query('inventory_hostnames', 'java') }}"
loop_control:
extended: true
run_once: true
给
msg: '[srv1] cert_alias: 1'
msg: '[srv2] cert_alias: 2'
msg: '[srv3] cert_alias: 3'
推荐阅读
- c# - 为什么将单个实体保存到我的 EF Core 数据库上下文会导致多次插入?
- c# - 最小化表单时,Numericupdown 箭头变黑
- c++ - 为什么将 unique_ptr 的 std::move() 到 void 函数来重置指针比调用显式 unique_ptr.reset() 花费的时间更少
- node.js - 从我的数据库中提取数据并将其(ById)填充到我的 Vue 视图(使用 Axios)时出现问题。后端是 Node/Express
- javascript - 如何获取命令发送到的频道名称
- python - driver.find_elements_by_xpath 随机失败
- android - 使用 Observable Boolean 设置 android 视图的可见性
- firebase - 保护多个用户需要写入的 firebase 表
- r - 跨两个一致数据帧的成对图?
- python - 如何循环分组数据框