Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在regex_replace的replace字符串中使用ansible变量_Ansible - Fatal编程技术网

在regex_replace的replace字符串中使用ansible变量

在regex_replace的replace字符串中使用ansible变量,ansible,Ansible,我想通过在/auto前面加上\u sw\u table来从文件生成一个新列表。虽然我可以通过在map中显式地将新字符串作为args提到regex\u replace来实现这一点,但我希望使用变量实现同样的效果。以下工作: - hosts: localhost gather_facts: no tasks: - set_fact: my_lst: [] top_dir: /auto extra: sw_table files: ['/et

我想通过在
/auto
前面加上
\u sw\u table
来从
文件生成一个新列表。虽然我可以通过在
map
中显式地将新字符串作为args提到
regex\u replace
来实现这一点,但我希望使用变量实现同样的效果。以下工作:

- hosts: localhost
  gather_facts: no
  tasks:
  - set_fact:
      my_lst: []
      top_dir: /auto
      extra: sw_table
      files: ['/etc/passwd', '/etc/group']

  - set_fact:
      my_lst: "{{ files | map('regex_replace', '(.*)', '/auto\\1_sw_table') | list }}"

  - debug: var=my_lst
输出:

$ ansible-playbook -i localhost, ./test.yaml 

PLAY [localhost] **************************************************************************************************

TASK [set_fact] ***************************************************************************************************
ok: [localhost]

TASK [set_fact] ***************************************************************************************************
ok: [localhost]

TASK [debug] ******************************************************************************************************
ok: [localhost] => {
    "my_lst": [
        "/auto/etc/passwd_sw_table", 
        "/auto/etc/group_sw_table"
    ]
}

如何在调用
map
时使用变量
top\u dir
extra
来获得相同的结果?

您可以在Jinja2中使用字符串连接:

  - set_fact:
      my_lst: "{{ files | map('regex_replace', '(^.*$)', top_dir + '\\1' + extra) | list }}"

请注意我使用的更新的正则表达式模式。我对上面的输出有不同的结果,需要更新模式。

您可以在Jinja2中使用字符串连接:

  - set_fact:
      my_lst: "{{ files | map('regex_replace', '(^.*$)', top_dir + '\\1' + extra) | list }}"

请注意我使用的更新的正则表达式模式。我对上面的输出有不同的结果,需要更新模式。

谢谢,解决方案看起来很琐碎,但我花了太多时间:(谢谢,解决方案看起来很琐碎,但我花了太多时间:(