Filter out text from a json object created by an ansible register

238 views
Skip to first unread message

Anand Solomon

unread,
Jan 30, 2020, 8:38:17 AM1/30/20
to Ansible Project
Hi,
How do I take only the text from the below from json created by an ansible register

I have this 
    - name: Display the query results
      debug:
        msg: "{{ query_result }}"

and I get [["TESTDBA"]], I want the value to be TESTDBA. Basically I am passing this value to another parameter in the same playbook like the below

roles: "{{query_result.msg }}"



Vladimir Botka

unread,
Jan 30, 2020, 9:02:02 AM1/30/20
to Anand Solomon, ansible...@googlegroups.com
On Thu, 30 Jan 2020 05:38:17 -0800 (PST)
Anand Solomon <anand.v...@gmail.com> wrote:

> - name: Display the query results
> debug:
> msg: "{{ query_result }}"
>
> and I get [["TESTDBA"]], I want the value to be TESTDBA.

The value you're looking for is the first item of the first list

- debug:
msg: "{{ query_result.0.0 }}"

gives

"msg": "TESTDBA"

HTH,

-vlado
Message has been deleted

Anand Solomon

unread,
Jan 30, 2020, 2:52:43 PM1/30/20
to Ansible Project
Hi,
Thanks for your help. I may be asking a simple question. I am new to ansible, so trying to learn. 

This is what I need to understand. 

1. Run the below
        sql: select grantee FROM DBA_SYS_PRIVS where PRIVILEGE='INSERT ANY TABLE' and grantee not like 'SYS' and grantee != 'WMSYS' and grantee != 'DBA' and grantee != 'IMP_FULL_DATABASE';
      register: query_result

The query result will be the users in the DB has DBA_SYS_PRIVS and it can be more than one.I want to iterate the below task passing the username dynamically.

2. The query_result have to be iterated in the below

    - name: This playbook will grant or revoke privilges in the user.
      oracle_privs:
        user: "{{user}}"
        password: "{{password}}"
        service_name: "{{sname}}"
        port: "{{prt}}"
        hostname: "{{hostname}}"
        privs:
          - INSERT ANY TABLE
        state: absent
        roles: "{{ query_result }}"
        quiet: false
      environment: "{{oracle_env}}"
      register: user_privs
      delegate_to: localhost

Anand Solomon

unread,
Jan 30, 2020, 3:20:38 PM1/30/20
to Ansible Project
The result I get from the query_result is something like this [["TESTDBA"], ["TESTDBA2"]]

I want to know how to pass this variable removing the square brackets and the roles in the task

Anand Solomon

unread,
Jan 30, 2020, 11:27:24 PM1/30/20
to Ansible Project
Just this made the trick. Thanks very much

        roles: "{{item}}"
        quiet: false
      environment: "{{oracle_env}}"
      register: user_privs
      with_items : "{{query_result.msg}}"
Reply all
Reply to author
Forward
0 new messages