这个 ansible 运行 如何使用正则表达式

How this ansible run with regex

有人可以为我解释这段代码吗:

block:
  - name: Replace text on file
    replace:
      path: "{{APP_HOME}}/{{ALIAS_NAME}}/{{regex.path}}"
      regexp: "{{regex.regex}}"
      replace: "{{regex.replace}}"
    with_items: "{{APP_REPLACE}}"
    loop_control:
       loop_var: regex

如果您重命名 loop_var:

,您可能会清楚
- name: Replace text on file
  replace:
    path: "{{APP_HOME}}/{{ALIAS_NAME}}/{{fred.path}}"
    regexp: "{{fred.regex}}"
    replace: "{{fred.replace}}"
  with_items: "{{APP_REPLACE}}"
  loop_control:
    loop_var: fred

然后试试这个:

- name: Show APP_REPLACE
  debug:
    var: APP_REPLACE

您会看到 APP_REPLACE 的每个元素都包含三个部分:pathregexreplace

vars.yml

APP_NAME: False
SERVICE: "{{hostvars[inventory_hostname][APP_NAME] | default('')}}"
APP_HOME: "{{hostvars[inventory_hostname][APP_NAME]['variables']['app_home'] | default('') }}"
ALIAS_NAME: "{{hostvars[inventory_hostname][APP_NAME]['variables']['alias_name'] | default(APP_NAME)  | default('') }}"
PROCESS_NAME: "{{hostvars[inventory_hostname][APP_NAME]['variables']['process_name'] | default('') }}"
ORIGIN_PROCESS_NAME: "{{hostvars[inventory_hostname][APP_NAME]['variables']['original_process_name'] | default(PROCESS_NAME)  | default('') }}"
JAVA_HOME: "{{hostvars[inventory_hostname][APP_NAME]['variables']['java_home'] | default('')}}"
APP_REPLACE: "{{ SERVICE['replace'] | default('')  }}"