【发布时间】:2020-09-29 20:00:10
【问题描述】:
所以我有这个 bash 脚本:
#!/bin/bash
echo -ne "Enter stack name: "
read -r STACK
echo -ne "Enter node type (Compute/Storage): "
read -r NODE_TYPE
export STACK
export NODE_TYPE
ansible-playbook -i inventory -l "$AC_STACK" node-exporter-install.yml -e "stack=$STACK"
库存文件就像定义一个组一样简单:
[SERVERS]
ip-address-1
ip-address-2
...
然后我有这个由 ansible playbook 使用的 Jinja2 模板:
{% for node in groups.getenv('STACK') -%}
- job_name: '{{ lookup('env', 'STACK') }}-{{ lookup('env', 'NODE_TYPE') }}-{{ node }}'
static_configs:
- targets: ['{{ node }}:9100']
{% endfor %}
如何获取模板内 bash 脚本中定义的 ENV 变量 STACK?!
如果我在 jinja2 模板 {{ for noe in groups.SERVERS %} 中手动定义它就可以了。
所以基本上我需要 groups.SERVERS 是我在执行 bash 脚本时定义的任何 ENV var....
【问题讨论】:
标签: bash ansible jinja2 ansible-template