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

时间:2018-11-14 19:36:14

标签: 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

有什么主意吗?谢谢!

3 个答案:

答案 0 :(得分:0)

您可以在ansible.cfg中进行设置: <Dropdown text='File'> <Dropdown.Menu> <Popup trigger={<Dropdown.Item text='Close' description="Close" value="Close" />} content="Hello there" /> <Popup trigger={<Dropdown.Item text='Open' description='Open' value="Open" />} content="Open me"/> {/* more options would go here... */} </Dropdown.Menu> </Dropdown>

答案 1 :(得分:0)

实际上,似乎不可能将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"
},
...

希望有帮助!

答案 2 :(得分:0)

这样创建变量:

    variableName:
      name: "demo"
      capacityUnits: 1

然后像这样的身体:

body: "{{ variableName | to_json }}"

然后int将继续存在:

"body": "{\"name\": \"demo\", \"capacityUnits\": 1}"