如何在Ansible中过滤JSON数据?

时间:2017-08-30 09:52:55

标签: json ansible jinja2

我使用Ansible command的命令查询API。 API返回带有网络信息的JSON对象。

我想根据公共IP获取服务器的私有IP。我知道这可以通过JSON query filter实现,但我无法弄清楚如何。

代码:

- name: Get RPN topology
  command: 'curl -X GET -H "Authorization: Bearer {{ onlineApiToken }}" "https://api.online.net/api/v1/rpn/group"'
  register: RPN
  delegate_to: 127.0.0.1

这是RPN.stdout输出的样子:

    TASK [debug] ****************************************************************************************
ok: [ps1] => {
    "changed": false, 
    "msg": [
        {
            "id": 7406, 
            "members": [
                {
                    "id": 0000, 
                    "ip": "x.x.x.x", 
                    "owner": "buzut", 
                    "private_ip": "10.91.154.39", 
                    "speed": 100, 
                    "status": "active"
                }, 
                {
                    "id": 1111, 
                    "ip": "y.y.y.y", 
                    "owner": "buzut", 
                    "private_ip": "10.91.120.148", 
                    "speed": 100, 
                    "status": "active"
                }, 
                {
                    "id": 2222, 
                    "ip": "z.z.z.z", 
                    "owner": "buzut", 
                    "private_ip": "10.91.165.215", 
                    "speed": 1000, 
                    "status": "active"
                }
            ], 
            "name": "MySQL", 
            "owner": "buzut", 
            "shared": false, 
            "status": "updating"
        }
    ]
}

问题:我如何获得服务器的私有IP,基于其公共IP?

2 个答案:

答案 0 :(得分:1)

您可能需要花些时间阅读JMESPath Examples以熟悉json_query过滤器支持的语法。以下为您提供所需的结果:

- hosts: localhost
  gather_facts: false
  vars:
    RPN:
      stdout: >-
        [
          {
            "id": 7406,
            "members": [
              {
                "id": 0,
                "ip": "x.x.x.x",
                "owner": "buzut",
                "private_ip": "10.91.154.39",
                "speed": 100,
                "status": "active"
              },
              {
                "id": 1111,
                "ip": "y.y.y.y",
                "owner": "buzut",
                "private_ip": "10.91.120.148",
                "speed": 100,
                "status": "active"
              },
              {
                "id": 2222,
                "ip": "z.z.z.z",
                "owner": "buzut",
                "private_ip": "10.91.165.215",
                "speed": 1000,
                "status": "active"
              }
            ],
            "name": "MySQL",
            "owner": "buzut",
            "shared": false,
            "status": "updating"
          }
        ]

  tasks:
    - name: lookup server based on public ip
      debug:
        var: item
      with_items: "{{RPN.stdout|from_json|json_query(public_to_private_ip)}}"
      vars:
        public_to_private_ip: >-
          [].members[?ip=='{{ public_ip }}'].private_ip

如果我这样称呼:

ansible-playbook playbook.yml -e public_ip=y.y.y.y

我明白了:

TASK [lookup server based on public ip] ****************************************
ok: [localhost] => (item=10.91.120.148) => {
    "item": "10.91.120.148"
}

如果我把它称为:

ansible-playbook playbook.yml -e public_ip=z.z.z.z

我明白了:

TASK [lookup server based on public ip] ****************************************
ok: [localhost] => (item=10.91.165.215) => {
    "item": "10.91.165.215"
}

答案 1 :(得分:0)

---
- name: play1
  hosts: localhost
  gather_facts: false
  vars:
    public_ip: "x.x.x.x"

  tasks:

  - name: Print data
    debug: var=RPN

  - name: Lookup value in json var
    debug: var=item
    with_items: "{{RPN|json_query(public_to_private_ip)}}"
    vars:
      public_to_private_ip: "members[?ip=='{{ public_ip }}'].private_ip"

给你:

PLAY [play1] *******************************************************************

TASK [Print data] **************************************************************
ok: [localhost] => {
    "RPN": {
        "id": 7406,
        "members": [
            {
                "id": "0000",
                "ip": "x.x.x.x",
                "owner": "buzut",
                "private_ip": "10.91.154.39",
                "speed": 100,
                "status": "active"
            },
            {
                "id": 1111,
                "ip": "y.y.y.y",
                "owner": "buzut",
                "private_ip": "10.91.120.148",
                "speed": 100,
                "status": "active"
            },
            {
                "id": 2222,
                "ip": "z.z.z.z",
                "owner": "buzut",
                "private_ip": "10.91.165.215",
                "speed": 1000,
                "status": "active"
            }
        ],
        "name": "MySQL",
        "owner": "buzut",
        "shared": false,
        "status": "updating"
    }
}

TASK [Lookup value in json var] ************************************************
ok: [localhost] => (item=10.91.154.39) => {
    "item": "10.91.154.39"
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

members[?ip=='{{ public_ip }}'].private_ip的神奇之处。