按空格拆分字符串,然后在 ansible/jinja2 中再次加入
splitting string by whitespace and then joining it again in ansible/jinja2
我正在尝试 "cleanup" Ansible (ansible-2.1.1.0-1.fc24.noarch) 剧本中变量中的空格,但我会先 split () 它,然后再次 join(' ')。由于某种原因,这种方法给我以下错误:-/
---
- hosts: all
remote_user: root
vars:
mytext: |
hello
there how are
you?
tasks:
- debug:
msg: "{{ mytext }}"
- debug:
msg: "{{ mytext.split() }}"
- debug:
msg: "{{ mytext.split().join(' ') }}"
...
给我:
TASK [debug] *******************************************************************
ok: [192.168.122.193] => {
"msg": "hello\nthere how are\nyou?\n"
}
TASK [debug] *******************************************************************
ok: [192.168.122.193] => {
"msg": [
"hello",
"there",
"how",
"are",
"you?"
]
}
TASK [debug] *******************************************************************
fatal: [192.168.122.193]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute 'join'\n\nThe error appears to have been in '.../tests.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n msg: \"{{ mytext.split() }}\"\n - debug:\n ^ here\n"}
知道我做错了什么吗?它说 字段 'args' 有一个无效值,它似乎包含一个未定义的变量。错误是:'list object' 没有属性 'join',但根据 useful filters 文档,它应该有效。
您应该使用管道来应用过滤器:
- debug:
msg: "{{ mytext.split() | join(' ') }}"
在这个例子中 split()
是字符串对象的 Python 方法。所以有点黑客。
join(' ')
是一个 Jinja2 过滤器,它将列表连接成字符串。
调用 mytext.split().join(' ')
会出错,因为 Python.
中的列表没有 join
方法
在 Python 中有 join
字符串方法,您可以调用 ' '.join(mytext.split())
,但这将是一个双重 hack。
我正在尝试 "cleanup" Ansible (ansible-2.1.1.0-1.fc24.noarch) 剧本中变量中的空格,但我会先 split () 它,然后再次 join(' ')。由于某种原因,这种方法给我以下错误:-/
---
- hosts: all
remote_user: root
vars:
mytext: |
hello
there how are
you?
tasks:
- debug:
msg: "{{ mytext }}"
- debug:
msg: "{{ mytext.split() }}"
- debug:
msg: "{{ mytext.split().join(' ') }}"
...
给我:
TASK [debug] *******************************************************************
ok: [192.168.122.193] => {
"msg": "hello\nthere how are\nyou?\n"
}
TASK [debug] *******************************************************************
ok: [192.168.122.193] => {
"msg": [
"hello",
"there",
"how",
"are",
"you?"
]
}
TASK [debug] *******************************************************************
fatal: [192.168.122.193]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute 'join'\n\nThe error appears to have been in '.../tests.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n msg: \"{{ mytext.split() }}\"\n - debug:\n ^ here\n"}
知道我做错了什么吗?它说 字段 'args' 有一个无效值,它似乎包含一个未定义的变量。错误是:'list object' 没有属性 'join',但根据 useful filters 文档,它应该有效。
您应该使用管道来应用过滤器:
- debug:
msg: "{{ mytext.split() | join(' ') }}"
在这个例子中 split()
是字符串对象的 Python 方法。所以有点黑客。
join(' ')
是一个 Jinja2 过滤器,它将列表连接成字符串。
调用 mytext.split().join(' ')
会出错,因为 Python.
中的列表没有 join
方法
在 Python 中有 join
字符串方法,您可以调用 ' '.join(mytext.split())
,但这将是一个双重 hack。