ansible - Ansible - 当数组已经有变量时,无法为数组中的每个项目添加前缀“ansible_hostname”
问题描述
我正在尝试将“ansible_hostname”作为前缀添加到数组中的每个项目,但得到两个不同的结果。
- 当数组是静态的而没有任何变量时,就会添加主机名。
- 当数组声明了一个变量时,“ansible_hostname”这一事实没有得到解决,而是作为字符串传递。
我有用变量声明的数组,需要帮助在不使用循环的情况下传递主机名。
场景一:
- name: test_array
set_fact:
test_array: ["This is test1 {{ansible_hostname}}", "This is test2"]
- set_fact:
test_fact: "{{ test_array | map('regex_replace', '^(.*)$', ' {{ansible_hostname}}, \\1') | join('\n') }}"
output:
"test_fact": " {{ansible_hostname}}, This is test1 control\n {{ansible_hostname}}, This is test2"
场景二:
- name: test_array
set_fact:
test_array: ["This is test1", "This is test2"]
- set_fact:
test_fact: "{{ test_array | map('regex_replace', '^(.*)$', ' {{ansible_hostname}}, \\1') | join('\n') }}"
output:
"test_fact": " host, This is test1 control\n host, This is test2"
解决方案
您不能在 jinja2 扩展中使用 jinja2 扩展。您必须使用运算符将主机名与其余的正则表达式替换连接起来+
:
- set_fact:
test_fact: "{{ test_array | map('regex_replace', '^(.*)$', ansible_hostname + ', \\1') | join('\n') }}"
推荐阅读
- c++ - static_cast 没有达到我的预期
- f# - 为什么 F# 编译器更喜欢生成 FSharpFunc 类型的封闭实现?
- selenium-chromedriver - 使用无头镀铬数字格式搞砸了
- ios - ARSKView 在关闭 ViewController 时未释放内存
- java - 如何在 Java 中调用具有动态值的枚举方法
- git - 防止 git 跟踪到文件夹的符号链接
- c# - 将字符串日期转换为特定的日期时间格式
- .net - 如何从 .NET 应用程序中删除 SeDebugPrivilege 权限
- c++ - 为什么 boost::hana::overload_t 不能成为类的成员
- java - 我如何从 UUID 类型 1 中获取日期/时间