json - Ansible:如何在 JSON 中打印数字而不是字符串 - 模块 uri

标签 json ansible uri

你好,
我需要将变量打印为数字而不是字符串。
例子:

- name: Create input
  uri:
    url: "https://{{ url_graylog }}/api/system/inputs"
    ...
    body_format: json
    body:
      title: "{{ name }}"
      configuration:
      bind_address: "0.0.0.0"
      port: "{{ port }}"  <-- its print as string, I need number
    global: true

我试过
port: {{ port }}          <-- not work
port: "{{ port | int }}"  <-- not work

任何的想法?谢谢!

最佳答案

实际上似乎不可能将 jinja 模板转换为整数,因为它总是将字符串返回给 Ansible。详细解释在这里:
https://github.com/ansible/ansible/issues/9362#issuecomment-302432118
但是,我找到了一种解决方法,包括在 yaml 中使用折叠字符串块。在您的情况下,Ansible 任务应如下所示:

- name: Create inputenter code here
  uri:
    url: "https://{{ url_graylog }}/api/system/inputs"
    ...
    body_format: json
    body: >
      {
        "title": "{{ name }}",
        "configuration": {
          "bind_address": "0.0.0.0",
          "port": {{ port | int }}
        }
      }
    global: true
它的可读性稍差,但会为端口生成一个未引用的值。发送的正文如下所示:
...
"body": {
    "configuration": {
        "bind_address": "0.0.0.0",
        "port": 25565
    },
    "title": "My title"
},
...
希望有帮助!

关于json - Ansible:如何在 JSON 中打印数字而不是字符串 - 模块 uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53307595/

相关文章:

json - MongoDB Compass 导出包含不需要的元数据的集合

javascript - 为什么它在数组之前显示未定义?

.htaccess - 你如何确保一行存在并且被 Ansible LineinFile 取消注释?

Docker 将 phpmyadmin 连接到我的 MySQL 服务器

tomcat - ansible jar 检查是否已经安装

web-services - RESTful 服务中的自定义操作

javascript - 需要比特币校验地址和 json 帮助

javascript - 使用ajax开发登录页面失败

javascript - 获取用于在页面上加载 div 的 URI?

java - 包含路径的 URI 模板变量?