Re: [ansible-project] Ansible set_fact with with_items always prints last item

2020-09-20 Thread Stefan Hornburg (Racke)
On 9/21/20 7:12 AM, kumare...@gmail.com wrote:
> Hi Brae,
> 
> Thanks for the help was able to get the desired output.But the output are in 
> same line is there way to print new line
> for each service?
> 
> TASK [debug]
> ***
> ok: [test] => {
>     "msg": "httpd.service Running\nntpd.service Running\n"
> }
> 
> Expected output:
> 
> httpd.service Running 
> ntpd.service Running 
> 
> Regards
> Kumar
> 

Hello Kumar,

use the YAML callback plugin, e.g in ansible.cfg:

# Use the YAML callback plugin.
stdout_callback = yaml

This changes the output to look like as follows:

ok: [buster-test-box] =>
  elk_app1: |-
sympa.service Running
wwsympa.service Running

Regards
 Racke


> On Saturday, 19 September 2020 at 19:28:58 UTC+5:30 brae...@gmail.com wrote:
> 
> On 9/19/20 12:29 PM, kumare...@gmail.com wrote:
> > Hi Experts,
> >
> > I am looking to capture certain service status using ansible the 
> problem is debug prints only last item.Below is the
> > expected output.
> >
> > Hostname : test
> >
> > httpd --> Running
> > ntpd  --> Running
> >
> > Below are the playbook.
> >
> > ---
> > - hosts: test
> >   become: true
> >   gather_facts: false
> >   tasks:
> >     - name: Service facts
> >       service_facts:
> >
> >     - set_fact:
> >        elk_app1: "{{ ansible_facts.services[item + '.service'].name}} 
> Running"
> >       when: ansible_facts.services[item + '.service'].state == 'running'
> >       with_items:
> >         - httpd
> >         - ntpd
> >       register: foo_result
> >
> >     - debug: msg="{{ foo_result }}"
> >
> >
> > Regards
> > Kumar
> 
> Hello Kumar,
> 
> if you use set_fact with a loop, you need to reference the variable on 
> the right hand side:
> 
> set_fact:
> elk_app1: "{{ elk_app1 | default('') + ansible_facts.services[item + 
> '.service'].name + ' Running\n' }}"
> 
> The default filter is necessary as the variable is undefined on the first 
> iteration of the loop.
> 
> 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 
> .
> > To view this discussion on the web visit
> > 
> https://groups.google.com/d/msgid/ansible-project/cbd4fd36-e141-48a1-b306-0a9629ec8e31n%40googlegroups.com
> >
> 
> .
> 
> 
> 
> -- 
> 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-project+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/48b4d375-f1bf-46fb-a4a5-84e8f89c88een%40googlegroups.com
> .


-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2d8f667c-2633-94bf-9b6c-b3f8f8517eb9%40linuxia.de.


signature.asc
Description: OpenPGP digital signature


Re: [ansible-project] Ansible set_fact with with_items always prints last item

2020-09-20 Thread kumare...@gmail.com
Hi Brae,

Thanks for the help was able to get the desired output.But the output are 
in same line is there way to print new line for each service?

TASK [debug] 
***
ok: [test] => {
"msg": "httpd.service Running\nntpd.service Running\n"
}

Expected output:

httpd.service Running 
ntpd.service Running 

Regards
Kumar

On Saturday, 19 September 2020 at 19:28:58 UTC+5:30 brae...@gmail.com wrote:

> On 9/19/20 12:29 PM, kumare...@gmail.com wrote:
> > Hi Experts,
> > 
> > I am looking to capture certain service status using ansible the problem 
> is debug prints only last item.Below is the
> > expected output.
> > 
> > Hostname : test
> > 
> > httpd --> Running
> > ntpd  --> Running
> > 
> > Below are the playbook.
> > 
> > ---
> > - hosts: test
> >   become: true
> >   gather_facts: false
> >   tasks:
> > - name: Service facts
> >   service_facts:
> > 
> > - set_fact:
> >elk_app1: "{{ ansible_facts.services[item + '.service'].name}} 
> Running"
> >   when: ansible_facts.services[item + '.service'].state == 'running'
> >   with_items:
> > - httpd
> > - ntpd
> >   register: foo_result
> > 
> > - debug: msg="{{ foo_result }}"
> > 
> > 
> > Regards
> > Kumar
>
> Hello Kumar,
>
> if you use set_fact with a loop, you need to reference the variable on the 
> right hand side:
>
> set_fact:
> elk_app1: "{{ elk_app1 | default('') + ansible_facts.services[item + 
> '.service'].name + ' Running\n' }}"
>
> The default filter is necessary as the variable is undefined on the first 
> iteration of the loop.
>
> 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  ansible-proje...@googlegroups.com>.
> > To view this discussion on the web visit
> > 
> https://groups.google.com/d/msgid/ansible-project/cbd4fd36-e141-48a1-b306-0a9629ec8e31n%40googlegroups.com
> > <
> https://groups.google.com/d/msgid/ansible-project/cbd4fd36-e141-48a1-b306-0a9629ec8e31n%40googlegroups.com?utm_medium=email&utm_source=footer
> >.
>
>
> -- 
> 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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/48b4d375-f1bf-46fb-a4a5-84e8f89c88een%40googlegroups.com.


Re: [ansible-project] Ansible set_fact with with_items always prints last item

2020-09-19 Thread Stefan Hornburg (Racke)
On 9/19/20 12:29 PM, kumare...@gmail.com wrote:
> Hi Experts,
> 
> I am looking to capture certain service status using ansible the problem is 
> debug prints only last item.Below is the
> expected output.
> 
> Hostname : test
> 
> httpd --> Running
> ntpd  --> Running
> 
> Below are the playbook.
> 
> ---
> - hosts: test
>   become: true
>   gather_facts: false
>   tasks:
>     - name: Service facts
>       service_facts:
> 
>     - set_fact:
>        elk_app1: "{{ ansible_facts.services[item + '.service'].name}} Running"
>       when: ansible_facts.services[item + '.service'].state == 'running'
>       with_items:
>         - httpd
>         - ntpd
>       register: foo_result
> 
>     - debug: msg="{{ foo_result }}"
> 
> 
> Regards
> Kumar

Hello Kumar,

if you use set_fact with a loop, you need to reference the variable on the 
right hand side:

set_fact:
  elk_app1: "{{ elk_app1 | default('') + ansible_facts.services[item + 
'.service'].name + ' Running\n' }}"

The default filter is necessary as the variable is undefined on the first 
iteration of the loop.

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-project+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/cbd4fd36-e141-48a1-b306-0a9629ec8e31n%40googlegroups.com
> .


-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6637b6e6-a2ec-14d2-9ed6-f76a86a62de1%40linuxia.de.


signature.asc
Description: OpenPGP digital signature


[ansible-project] Ansible set_fact with with_items always prints last item

2020-09-19 Thread kumare...@gmail.com
Hi Experts,

I am looking to capture certain service status using ansible the problem is 
debug prints only last item.Below is the expected output.

Hostname : test

httpd --> Running
ntpd  --> Running

Below are the playbook.

---
- hosts: test
  become: true
  gather_facts: false
  tasks:
- name: Service facts
  service_facts:

- set_fact:
   elk_app1: "{{ ansible_facts.services[item + '.service'].name}} 
Running"
  when: ansible_facts.services[item + '.service'].state == 'running'
  with_items:
- httpd
- ntpd
  register: foo_result

- debug: msg="{{ foo_result }}"


Regards
Kumar

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/cbd4fd36-e141-48a1-b306-0a9629ec8e31n%40googlegroups.com.