bswe
bswe

Reputation: 120

How I can use Variables in Ansible json_query?

I want to use a ansible variable inside the json query filter. This is my Code:

Playbook execution:

ansible-playbook debug.yml -e "project_environment=live"
- debug:
    msg: "{{ project_environment }}"

- debug:
    msg: "{{ check_objects | json_query('`{{project_environment}}`.current') }}"

This is my dictionary:

check_objects:
  live:
    current:
    - example.com
    next:
    - next.example.com

This is what i got:

TASK [debug : debug] 
ok: [sample-hostname] => {
    "msg": "live"
}

TASK [debug  : debug]
ok: [sample-hostname] => {
    "msg": ""
}

When i replace the variable by using the expected value, the output it is working fine:

- debug:
    msg: "{{ check_objects | json_query('live.current') }}"
TASK [typo3-deployment/check : debug] 
ok: [sample-hostname] => {
    "msg": [
        "example.com"
    ]
}

I think it runs in trouble while interpolate the variable.

I have tried this solution but it doesn't work too: Ansible : pass a variable in a json_query filter

Upvotes: 8

Views: 13636

Answers (4)

scrow
scrow

Reputation: 447

A solution that worked when I needed to use a variable inside a jsonquery filter. I could not get any of the solutions mentioned to work, except the below:

 - name: Package the we are looking for
  debug:
    var: final_package

- name: Get final link and sha for package
  set_fact:
    sentinel_link: "{{ json_data | json_query('data[?fileName==`' + final_package + '`].link') | first }}"

Backticks around the quotations are important or it will fail

Upvotes: 1

Damian
Damian

Reputation: 11

I came here because I had a similar issue (although with a different query plugin). Hopefully this solution will help people who search for it. See my playbook for example. Text which is commented out is the one that doesn't work, while the one not commented works just fine

- name: get vlan
  hosts: localhost
  gather_facts: false
  vars:
    token: "mytoken"
    url: https://example.com
    group: vlan_group
  tasks:
    - name: Obtain vlans
      set_fact:
        #vlans: "{{ query('networktocode.nautobot.lookup', 'vlans', api_filter='status=active group={{ group }}', api_endpoint={{ url }}, api_version='1.3', token= {{ token }}, validate_certs=False) }}"
        vlans: "{{ query('networktocode.nautobot.lookup', 'vlans', api_filter='status=active group=\"' + group + '\"', api_endpoint=vars['url'], api_version='1.3', token=vars['token'], validate_certs=False) }}"

Inside string \"' + variable_name + '\" worked, while if I want to perform whole variable substitution, then I had to use vars['variable_name']

Upvotes: 1

bswe
bswe

Reputation: 120

For two variables this works fine for me.

- debug:
    msg: "{{ check_objects | json_query(query) }}"
  vars:
    query: "{{ project_environment }}.{{ project_status}}"

Upvotes: 1

Vladimir Botka
Vladimir Botka

Reputation: 68384

The task with json_query below

  vars:
    project_environment: live
  tasks:
    - debug:
        msg: "{{ check_objects|
                 dict2items|
                 json_query(query)|
                 flatten }}"
      vars:
        query: "[?key=='{{ project_environment }}'].value.current"

gives

"msg": [
    "example.com"
]

The same result can be achieved also with the task

- debug:
    var: check_objects[project_environment].current

Upvotes: 5

Related Questions