Ansible how to get output as variable without brackets and u

3,533 views
Skip to first unread message

wane...@gmail.com

unread,
Apr 3, 2019, 5:35:18 AM4/3/19
to Ansible Project

I have the result of a query from ansible and I tried to use the result as a variable

My playbook is like this

- name: Query
  oracle_sql:
    username: "{{ user }}"
    password: "{{ password }}"
    service_name: "{{ service_name }}"
    sql: "select smth  from table where smth like 'OK_%'"
  register: smth"

the result is formatted as below

"msg": [
    [
        "SMTH"
    ]
]

no stdout, no stderr

after this I want to use my variable

- name: echo
  shell: echo {{ smth.msg[0] }} > /tmp/test

and the outuput is like this:

[uSMTH]

How to remove brackets and 'u' , I know it s a list but I can t get rid of this

I tried to convert in yaml or json format no luck , this last add some retrun carriage with

| to_yaml 
| to_json
|to_nice_yaml
| to_nice_json

Any advice ?

Stefan Hornburg (Racke)

unread,
Apr 3, 2019, 5:42:01 AM4/3/19
to ansible...@googlegroups.com
On 4/3/19 11:35 AM, wane...@gmail.com wrote:
> I have the result of a query from ansible and I tried to use the result as a variable
>
> My playbook is like this
>
> |-name:Queryoracle_sql:username:"{{ user }}"password:"{{ password }}"service_name:"{{ service_name }}"sql:"select smth
> from table where smth like 'OK_%'"register:smth"|
>
> the result is formatted as below
>
> |"msg":[["SMTH"]]|
>
> no stdout, no stderr
>
> after this I want to use my variable
>
> |-name:echo shell:echo {{smth.msg[0]}}>/tmp/test|
>
> and the outuput is like this:
>
> [uSMTH]
>
> How to remove brackets and 'u' , I know it s a list but I can t get rid of this
>
> I tried to convert in yaml or json format no luck , this last add some retrun carriage with
>
> ||to_yaml |to_json |to_nice_yaml |to_nice_json|
>
> Any advice ?

Using smth[0][0] should do the trick (first field in the first sql record).

Regards
Racke

>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/737ba56a-896f-4506-b297-7d84458594b7%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/737ba56a-896f-4506-b297-7d84458594b7%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Jerome Denechaud

unread,
Apr 3, 2019, 5:47:48 AM4/3/19
to ansible...@googlegroups.com
it works thanks a lot can you explain to me please about the [0][0]
thanks in advance

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/00407cd6-33b6-9b4f-0779-ad22c877e524%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Apr 3, 2019, 5:53:44 AM4/3/19
to ansible...@googlegroups.com
On 4/3/19 11:47 AM, Jerome Denechaud wrote:
> it works thanks a lot can you explain to me please about the [0][0]
> thanks in advance
>

OK, say you have the following query:

select first_name, last_name from test_table

With two results:

Test | Tester
Foo | Bar

In your smth variable it will look like that:

[["Test", "Tester"]["Foo", "Bar"]]

smth[0][0] = Test
smth[0][1] = Tester
smth[1][0] = Foo
smth[1][1] = Bar

The first index is for the SQL record, the second index for the fields you put into the select statement.

Regards
Racke

> Le mer. 3 avr. 2019 à 11:42, Stefan Hornburg (Racke) <ra...@linuxia.de <mailto:ra...@linuxia.de>> a écrit :
>
> On 4/3/19 11:35 AM, wane...@gmail.com <mailto:wane...@gmail.com> wrote:
> > I have the result of a query from ansible and I tried to use the result as a variable
> >
> > My playbook is like this
> >
> > |-name:Queryoracle_sql:username:"{{ user }}"password:"{{ password }}"service_name:"{{ service_name }}"sql:"select smth
> > from table where smth like 'OK_%'"register:smth"|
> >
> > the result is formatted as below
> >
> > |"msg":[["SMTH"]]|
> >
> > no stdout, no stderr
> >
> > after this I want to use my variable
> >
> > |-name:echo shell:echo {{smth.msg[0]}}>/tmp/test|
> >
> > and the outuput is like this:
> >
> > [uSMTH]
> >
> > How to remove brackets and 'u' , I know it s a list but I can t get rid of this
> >
> > I tried to convert in yaml or json format no luck , this last add some retrun carriage with
> >
> > ||to_yaml |to_json |to_nice_yaml |to_nice_json|
> >
> > Any advice ?
>
> Using smth[0][0] should do the trick (first field in the first sql record).
>
> Regards
>          Racke
>
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>
> <mailto:ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>>.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/ansible-project/737ba56a-896f-4506-b297-7d84458594b7%40googlegroups.com
> >
> <https://groups.google.com/d/msgid/ansible-project/737ba56a-896f-4506-b297-7d84458594b7%40googlegroups.com?utm_medium=email&utm_source=footer>.
> > For more options, visit https://groups.google.com/d/optout.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CADC4zwwApAufpzEeDgnSA%2B72enXytqs_ToYBDLccVkK6im%2B2yg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CADC4zwwApAufpzEeDgnSA%2B72enXytqs_ToYBDLccVkK6im%2B2yg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc
Reply all
Reply to author
Forward
0 new messages