多引号的多行字符串

问题描述 投票:2回答:1

我正在尝试使用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}}"

运行此剧本时,结果如下:

# 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 "{{的序列时,引号就消失了...

我的问题是:

  1. 谁能告诉我为什么会这样吗?
  2. 有人可以建议我如何避免这个问题吗?我尝试了多行字符串(带双引号,单引号,>折叠等)-相同的结果...

[编辑:在经历可能导致此问题的所有最近更改时,我记得启用jinja2_native。确实如此,在jinja2_native中将ansible.cfg设置为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配置Mikrotik路由器-作为该任务的一部分,我需要生成发送到路由器的实际命令。而且不知何故,有些引号只是...消失...

ansible yaml jinja2 multiline multilinestring
1个回答
2
投票

我能够在github上得到答案-非常感谢sivel!

© www.soinside.com 2019 - 2024. All rights reserved.