smc
smc

Reputation: 2345

set_fact not working when using along with json_query

I have following Ansible playbook

$ cat playbook.test
---
- hosts: localhost
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: Fetch groups
      k8s_info:
        api_version: v1
        kind: ConfigMap
        name: groups
        namespace: default
        verify_ssl: no
      register: groupsvalue
    - debug:
        msg: "{{ vals | json_query('resources[*].data') }}"
    - set_fact:
        groups: "{{ vals | json_query('resources[*].data') }}"
    - debug:
        msg: "{{ groups }}"

As you can see in the following output, the first debug perfectly shows the expected output, but when I assign it with a variable using set_fact, it is returning as ungrouped.

Output:


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

TASK [Gathering Facts] **************************************************************************
ok: [localhost]

TASK [Fetch LDAP whitelisting groups] ***********************************************************
ok: [localhost]

TASK [debug] ************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "groups.txt": "CN=foo1,OU=Groups,OU=.Common,DC=local\nCN=bar1,OU=Groups,OU=.Common,DC=local\n"
        }
    ]
}

TASK [set_fact] *********************************************************************************
ok: [localhost]

TASK [debug] ************************************************************************************
ok: [localhost] => {
    "msg": {
        "all": [],
        "ungrouped": []
    }
}

PLAY RECAP **************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

May I know the possible way to set the output of first debug to a variable? Expected output: The groups set_fact have following value:

"groups.txt": "CN=foo1,OU=Groups,OU=.Common,DC=local\nCN=bar1,OU=Groups,OU=.Common,DC=local\n"

Upvotes: 0

Views: 681

Answers (1)

George Shuklin
George Shuklin

Reputation: 7887

groups is a magic variable in ansible (it's a mapping with group names as keys and values as list of members). So use a different variable name.

    - set_fact:
        mygroups: "{{ vals | json_query('resources[*].data') }}"

Upvotes: 2

Related Questions