有人可以为我解释一下这段代码:
- loop_var 函数是什么
- 什么将作为 regex.path、regex.regex 和 regex.replace 返回
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
回答1
如果您重命名 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
的每个元素都包含三个部分:path
、regex
和 replace
。
回答2
变量.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('') }}"