【发布时间】:2020-08-14 01:51:56
【问题描述】:
我正在尝试使用 Ansible 配置 Mikrotik 路由器 - 作为该任务的一部分,我需要生成发送到路由器的实际命令。不知何故,一些引号只是......在 Ansible 解析后从我的字符串中消失了。
[ 编辑 - 这似乎与jinja2_native 有关。详情见问题结尾]
我能够构建的展示问题的最小示例是这样的:
- hosts: localhost
gather_facts: false
tasks:
- vars:
port: 20200
cmd: >
add chain=dstnat && dst-port="{{port}}"
comment="%TR% - {{ansible_host}} - SSH for {{inventory_hostname}}"
dst-port="{{ port }}" protocol=tcp
}'
debug:
msg: "{{cmd}}"
运行此 playbook 时,结果如下:
# ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *****************************************************************************************************************
TASK [debug] *********************************************************************************************************************
Wednesday 29 April 2020 23:07:52 +0300 (0:00:00.052) 0:00:00.052 *******
ok: [localhost] => {
"msg": "add chain=dstnat && dst-port=\"20200\" comment=\"%TR% - 127.0.0.1 - SSH for localhost dst-port=20200\" protocol=tcp }'\n"
}
请注意一些引号是如何消失的,因此命令中的参数数量已经完全改变。 (当然,我在花了几个小时进行故障排除并想知道为什么命令失败后才注意到这一点......)
有趣的是,如果我稍微改变一下文字,其他引号就会消失:
- hosts: localhost
gather_facts: false
tasks:
- vars:
port: 20200
cmd: >
add chain=dstnat && dst-port="{{port}}"
comment="{{ansible_host}} - SSH for {{inventory_hostname}}"
dst-port="{{ port }}" protocol=tcp
}'
debug:
msg: "{{cmd}}"
tags:
- networking
# ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *****************************************************************************************************************
TASK [debug] *********************************************************************************************************************
Wednesday 29 April 2020 23:12:12 +0300 (0:00:00.031) 0:00:00.031 *******
ok: [localhost] => {
"msg": "add chain=dstnat && dst-port=\"20200 comment=127.0.0.1 - SSH for localhost dst-port=20200\" protocol=tcp }'\n"
}
似乎每当我有一个像}}" text_here "{{ 这样的序列时,引号就消失了......
我的问题是:
- 谁能告诉我为什么会这样?
- 谁能告诉我如何避免这个问题?我尝试过多行字符串(双引号、单引号、> 折叠等) - 结果相同...
[ 编辑:在查看可能导致此问题的所有最近更改时,我记得启用 jinja2_native 。确实如此,在 ansible.cfg 中将 jinja2_native 设置回 False 后,问题就消失了……
# ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *****************************************************************************************************************
TASK [debug] *********************************************************************************************************************
Wednesday 29 April 2020 23:55:29 +0300 (0:00:00.029) 0:00:00.029 *******
ok: [localhost] => {
"msg": "add chain=dstnat && dst-port=\"20200\" comment=\"127.0.0.1 - SSH for localhost \" dst-port=\"20200\" protocol=tcp }'\n"
}
]
【问题讨论】:
-
ansible v2.9.5 / python 3.6.9 => 我无法重现您的问题。字符串完全按照预期输出。
-
@Zeitounator - 我还有一项可能相关的配置更改。请参阅我稍后关于
jinja2_native的编辑
标签: ansible yaml jinja2 multiline multilinestring