无法在ansible中的单引号之间添加单引号
Unable to add single quotes between single quotes in ansible
下面是我的剧本:
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' + item | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
- debug:
msg: "exfolderr is {{ excludefolders }}"
我得到以下输出
ok: [10.0.17.113] => { "msg": "exfolderr is -o -name custom -o -name tree -o -name log"
但是我希望变量在项目周围有单引号,如下所示:
预期输出:
ok: [10.0.17.113] => { "msg": "exfolderr is -o -name 'custom' -o -name 'tree' -o -name 'log'"
我尝试使用转义字符和引号过滤器来添加单引号,但 none 成功了。以下是尝试过的内容。
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' + item | quote | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' + \' + item + \' | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' ~ \' + item ~ \' | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name \'' + item + '\'' | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
有点难看,但它有效:
---
- hosts: localhost
tasks:
- set_fact:
excludefolders: ""
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' + quote + item|default('') + quote }}"
with_items:
- custom
- tree
- log
vars:
- quote: "'"
- debug:
msg: "exfolderr is {{ excludefolders }}"
给出以下内容:
TASK [debug] ********************************************************************************
ok: [localhost] =>
msg: exfolderr is -o -name 'custom' -o -name 'tree' -o -name 'log'
下面是我的剧本:
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' + item | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
- debug:
msg: "exfolderr is {{ excludefolders }}"
我得到以下输出
ok: [10.0.17.113] => { "msg": "exfolderr is -o -name custom -o -name tree -o -name log"
但是我希望变量在项目周围有单引号,如下所示:
预期输出:
ok: [10.0.17.113] => { "msg": "exfolderr is -o -name 'custom' -o -name 'tree' -o -name 'log'"
我尝试使用转义字符和引号过滤器来添加单引号,但 none 成功了。以下是尝试过的内容。
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' + item | quote | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' + \' + item + \' | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' ~ \' + item ~ \' | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name \'' + item + '\'' | default('') }}"
with_items: "{{ lookup('vars', 'MY_' + Layer).split(',') }}"
有点难看,但它有效:
---
- hosts: localhost
tasks:
- set_fact:
excludefolders: ""
- set_fact:
excludefolders: "{{ excludefolders + ' -o -name ' + quote + item|default('') + quote }}"
with_items:
- custom
- tree
- log
vars:
- quote: "'"
- debug:
msg: "exfolderr is {{ excludefolders }}"
给出以下内容:
TASK [debug] ********************************************************************************
ok: [localhost] =>
msg: exfolderr is -o -name 'custom' -o -name 'tree' -o -name 'log'