loops - ANSIBLE:循环环境变量
问题描述
我想在 Ansible 中使用多个环境变量播放 shell 模块。我想在我的变量中注册的列表上循环。剧本看起来像这样:
vars:
tcd_environment_variable:
- { abc_variable: "MSG" , abc_value: "HelloWorld" }
- { abc_variable: "REP_USER" , abc_value: "/home/user" }
tasks:
- name: "Test command with environment variables registered"
shell: "echo $MSG >> $REP_USER/test_env.log"
environment:
"{{ item.abc_variable }}": "{{ item.abc_value }}"
loop: "{{ abc_environment_variable }}"
become: yes
become_user: user
我不能让它工作,只有这个工作:
tasks:
- name: "Test command with environment variables registered"
shell: "echo $MSG >> $REP_USER/test_env.log"
environment:
REP_USER: /home/user
MSG: "HelloWorld"
become: yes
become_user: user
但我想循环 Ansible 变量。
谢谢你的帮助
解决方案
使用items2dict将列表转换为字典,例如
- hosts: localhost
vars:
abc_environment_variable:
- {abc_variable: "MSG", abc_value: "HelloWorld"}
- {abc_variable: "REP_USER", abc_value: "/tmp"}
tasks:
- name: "Test command with environment variables registered"
shell: "echo $MSG >> $REP_USER/test_env.log"
environment: "{{ abc_environment_variable|
items2dict(key_name='abc_variable',
value_name='abc_value') }}"
给
shell> cat /tmp/test_env.log
HelloWorld
请参阅“设置远程环境”。根据您的需要调整参数和升级。
推荐阅读
- react-native - 项目根目录之外的 React Native ios 构建文件夹
- ios - WidgetKit 配置,一个参数基于另一个参数的选择
- python - 如何使用 apscheduler CronTrigger 计算先前的触发时间
- database - 一对多关系的历史表
- python - 在 Python3 中将任意对象转换为字节
- r - 是否可以根据其绘图/预测曲线过滤数据?
- c++ - 如何模板容器?
- c# - BLE:如何在 Win10 的 BLE 写入和通知中识别大数据的最后一个数据包
- vba - 查询表达式“2020-12-01”中的语法错误(缺少运算符)
- kotlin - HMS Drive kit 查询和查询字符串