Nexus490
Nexus490

Reputation: 329

Ansible parse json and read result into different variables

I've set up a task which queries the github api meta endpoint and returns the following

{
  "verifiable_password_authentication": true,
  "github_services_sha": "f9e3a6b98d76d9964a6613d581164039b8d54d89",
  "hooks": [
    "192.30.252.0/22",
    "185.199.108.0/22",
    "140.82.112.0/20"
  ],
  "git": [
    "192.30.252.0/22",
    "185.199.108.0/22",
    "140.82.112.0/20",
    "13.229.188.59/32",
    "13.250.177.223/32",
    "18.194.104.89/32",
    "18.195.85.27/32",
    "35.159.8.160/32",
    "52.74.223.119/32"
  ],
  "pages": [
    "192.30.252.153/32",
    "192.30.252.154/32",
    "185.199.108.153/32",
    "185.199.109.153/32",
    "185.199.110.153/32",
    "185.199.111.153/32"
  ],
  "importer": [
    "54.87.5.173",
    "54.166.52.62",
    "23.20.92.3"
  ]
}

What I need to do is get the 3 hook IPs and read them each into their own variable.

I've tried a couple of solutions i've found around but nothing is seeming to work for me.

I've got as far as drilling down into the json so i'm being returned only the 3 IPs, but how do I get them out and into variables individually?

Upvotes: 2

Views: 5425

Answers (3)

Oleksandr Tretiak
Oleksandr Tretiak

Reputation: 1

Works with GitHub Webhook IPs in a loop

- name: get request to github
  uri:
    url: "https://api.github.com/meta"
    method: GET
    return_content: yes
    status_code: 200
    headers:
      Content-Type: "application/json"
      #X-Auth-Token: "0010101010"
    body_format: json
  register: json_response

- name:  GitHub webhook IPs
  debug:
    msg: "{{ item }}"
  with_items: "{{ (json_response.content | from_json).hooks }}"


Upvotes: 0

ilias-sp
ilias-sp

Reputation: 6705

i gave it a shot using j2 syntax in the variable name part, and - TIL - looks like the jinja2 syntax is allowed in that part as well!

please see playbook to process the hooks list variable and assign to variables variable_1, variable_2, variable_3 and so on:

- hosts: localhost
  gather_facts: false
  vars:
    counter: 1
    hooks:
    - 192.30.252.0/22
    - 185.199.108.0/22
    - 140.82.112.0/20

  tasks:
    - name: populate vars
      set_fact:
        variable_{{counter}}: "{{ item }}"
        counter: "{{ counter | int + 1 }}"
      with_items:
        - "{{ hooks }}"

    - name: print vars
      debug:
        msg: "variable_1: {{variable_1}}, variable_2: {{variable_2}}, variable_3: {{variable_3}}"

and the output:

[root@optima-ansible ILIAS]# ansible-playbook 50257063.yml 

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************

TASK [populate vars] *******************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=192.30.252.0/22)
ok: [localhost] => (item=185.199.108.0/22)
ok: [localhost] => (item=140.82.112.0/20)

TASK [print vars] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "variable_1: 192.30.252.0/22, variable_2: 185.199.108.0/22, variable_3: 140.82.112.0/20"
}

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

[root@optima-ansible ILIAS]# 

hope it helps

UPDATE:

something weird i noticed - also TIL - is that if you reverse the lines:

variable_{{counter}}: "{{ item }}"
counter: "{{ counter | int + 1 }}"

to:

counter: "{{ counter | int + 1 }}"
variable_{{counter}}: "{{ item }}"

you still end up with the same variable names, _1 to _3, while i would expect to get _2 to _4.

I guess ansible loops behave differently than expected from other programming languages.

Upvotes: 1

Nexus490
Nexus490

Reputation: 329

---

- name: Query Github Meta API and get Hook Ips
  hosts: local
  connection: local
  vars:
    counter: 1
  tasks:

    - name: Query API
      uri:
        url: https://api.github.com/meta
        return_content: yes
      register: response

    - name: Populate Hook Variables
      set_fact:
        webhook_ip_{{counter}}: "{{ item }}"
        counter: "{{ counter | int + 1 }}"
      with_items:
        - "{{ response['json']['hooks'] }}"

    - name: print vars
      debug:
        msg: "Variable_1: {{ webhook_ip_1 }}, Variable_2: {{ webhook_ip_2 }}, Variable_3: {{ webhook_ip_3 }}"

Upvotes: 1

Related Questions