Ansible-无法添加前缀';ansible_主机名';当数组已经有变量时,返回到数组中的每个项

Ansible-无法添加前缀';ansible_主机名';当数组已经有变量时,返回到数组中的每个项,ansible,ansible-facts,Ansible,Ansible Facts,我试图将'ansible_hostname'作为前缀添加到数组中的每个项,但得到两个不同的结果 当数组是静态的,没有任何变量时,就会添加主机名 当数组声明了一个变量时,“ansible_hostname”这一事实没有得到解析,而是作为字符串传递 我用变量声明了数组,需要帮助在不使用循环的情况下传递主机名 情景1: - name: test_array set_fact: test_array: ["This is test1 {{ansible_hostname}}",

我试图将'ansible_hostname'作为前缀添加到数组中的每个项,但得到两个不同的结果

  • 当数组是静态的,没有任何变量时,就会添加主机名
  • 当数组声明了一个变量时,“ansible_hostname”这一事实没有得到解析,而是作为字符串传递
我用变量声明了数组,需要帮助在不使用循环的情况下传递主机名

情景1:

  - 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"
情景2:

 - 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扩展。您必须使用
+
运算符将主机名与剩余的regexp替换项连接起来:

-设置事实:
test_事实:“{test_数组|映射('regex_replace','^(.*)$',ansible_hostname+',\\1')|连接('\n')}”