ansible when 条件语句 - 关于 jinja2 模板定界符的警告
ansible when conditional statement - warning about jinja2 templating delimiters
所以我正在使用这个 ansible 剧本:
# Check if /backup folder is mounted
- name: "Check if /backup folder is mounted"
command: /bin/mountpoint {{ do_backup_folder }}
register: mount_status
failed_when: false
changed_when: false
tags: ['digitalocean', 'spaces', 'mount']
# Mount digitalocean spaces to /backup
- name: "Mount digitalocean spaces to /backup as {{ litecoin_user }} user"
shell: |
s3fs {{ do_spaces_name }} {{ do_backup_folder }} -o url={{ do_spaces_url }} -o use_cache=/tmp -o allow_other -o use_path_request_style -o nonempty -o uid={{ getent_passwd.litecoin[1] }} -o gid={{ getent_passwd.litecoin[1] }}
when: mount_status.stdout == "{{ do_backup_folder }} is not a mountpoint"
tags: ['digitalocean', 'spaces', 'mount']
其中 do_backup_folder 在 group_vars 中定义为 /backup。
剧本运行得很好,但是当 运行 我得到这个:
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: mount_status.stdout == "{{ do_backup_folder }} is not a mountpoint"
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: mount_status.stdout == "{{ do_backup_folder }} is not a mountpoint"
我应该如何重写 when 条件以消除此警告?我尝试了几种方法,但没有用。我想在 when 条件下使用 {{ do_backup_folder }}
变量。
在 when:
上下文中,整个事情都隐含在 {{ ... }}
中,这就是为什么他们抱怨 {{
的后续使用——嵌套 jinja2 胡须是一个严重的反模式.
出于同样的原因,人们不需要将 mount_status.stdout
包裹在胡须中,也不必将 do_backup_folder
包裹在胡须中。唯一的后续决定是使用 +
还是 ~
(前者是 pythonic 运算符,后者是 jinja2 string concat operator 并且当人们希望将参数强制转换为字符串)
- name: "Mount digitalocean spaces to /backup as {{ litecoin_user }} user"
shell: |
s3fs {{ do_spaces_name }} {{ do_backup_folder }} -o url={{ do_spaces_url }} -o use_cache=/tmp -o allow_other -o use_path_request_style -o nonempty -o uid={{ getent_passwd.litecoin[1] }} -o gid={{ getent_passwd.litecoin[1] }}
when: mount_status.stdout == do_backup_folder + " is not a mountpoint"
tags: ['digitalocean', 'spaces', 'mount']
所以我正在使用这个 ansible 剧本:
# Check if /backup folder is mounted
- name: "Check if /backup folder is mounted"
command: /bin/mountpoint {{ do_backup_folder }}
register: mount_status
failed_when: false
changed_when: false
tags: ['digitalocean', 'spaces', 'mount']
# Mount digitalocean spaces to /backup
- name: "Mount digitalocean spaces to /backup as {{ litecoin_user }} user"
shell: |
s3fs {{ do_spaces_name }} {{ do_backup_folder }} -o url={{ do_spaces_url }} -o use_cache=/tmp -o allow_other -o use_path_request_style -o nonempty -o uid={{ getent_passwd.litecoin[1] }} -o gid={{ getent_passwd.litecoin[1] }}
when: mount_status.stdout == "{{ do_backup_folder }} is not a mountpoint"
tags: ['digitalocean', 'spaces', 'mount']
其中 do_backup_folder 在 group_vars 中定义为 /backup。
剧本运行得很好,但是当 运行 我得到这个:
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: mount_status.stdout == "{{ do_backup_folder }} is not a mountpoint"
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: mount_status.stdout == "{{ do_backup_folder }} is not a mountpoint"
我应该如何重写 when 条件以消除此警告?我尝试了几种方法,但没有用。我想在 when 条件下使用 {{ do_backup_folder }}
变量。
在 when:
上下文中,整个事情都隐含在 {{ ... }}
中,这就是为什么他们抱怨 {{
的后续使用——嵌套 jinja2 胡须是一个严重的反模式.
出于同样的原因,人们不需要将 mount_status.stdout
包裹在胡须中,也不必将 do_backup_folder
包裹在胡须中。唯一的后续决定是使用 +
还是 ~
(前者是 pythonic 运算符,后者是 jinja2 string concat operator 并且当人们希望将参数强制转换为字符串)
- name: "Mount digitalocean spaces to /backup as {{ litecoin_user }} user"
shell: |
s3fs {{ do_spaces_name }} {{ do_backup_folder }} -o url={{ do_spaces_url }} -o use_cache=/tmp -o allow_other -o use_path_request_style -o nonempty -o uid={{ getent_passwd.litecoin[1] }} -o gid={{ getent_passwd.litecoin[1] }}
when: mount_status.stdout == do_backup_folder + " is not a mountpoint"
tags: ['digitalocean', 'spaces', 'mount']