Get item with file

38 views
Skip to first unread message

ryad9...@gmail.com

unread,
Mar 4, 2019, 7:24:13 AM3/4/19
to Ansible Project
Hy all,

I have one file with this content:

1,user1,host1,appli1
2,user2,host2,appli2
3,user1,host1.appli3

This task works :

- set_fact:
      indice: "{{ item.split(',')[0] }}"
  with_lines: cat /home/ansible/ssh_manager/clef_applicatives.csv
  when: item.split(',')[1] == ansible_user and item.split(',')[2] == inventory_hostname
  delegate_to: localhost

- name: "Affichage de l'indice associe avec user et hostname"
  debug:
      var: indice

BUT my variable indice get only indice 3 but indice must get 1 and 3.

How make for that my variable indice get 1,2 or [1,2]

my variable indice get only last line !

Thank you very much guy !!

Regards,

Jean-Yves LENHOF

unread,
Mar 4, 2019, 10:28:49 AM3/4/19
to ansible...@googlegroups.com


Le 04/03/2019 à 13:24, ryad9...@gmail.com a écrit :
Hy all,

I have one file with this content:

1,user1,host1,appli1
2,user2,host2,appli2
3,user1,host1.appli3

This task works :

- set_fact:
      indice: "{{ item.split(',')[0] }}"


Change this :

      indice: "{{ item.split(',')[0] }}"

To something like this (not tested, but you should have the idea) :

      indice: "{{ indice|default([])|union(item.split(',')[0]) }}"


  with_lines: cat /home/ansible/ssh_manager/clef_applicatives.csv
  when: item.split(',')[1] == ansible_user and item.split(',')[2] == inventory_hostname
  delegate_to: localhost

- name: "Affichage de l'indice associe avec user et hostname"
  debug:
      var: indice

BUT my variable indice get only indice 3 but indice must get 1 and 3.

How make for that my variable indice get 1,2 or [1,2]

my variable indice get only last line !

Thank you very much guy !!

Regards,

--
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 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/a6de934b-e9fd-4dc0-bf88-b6267669d66c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ryad9...@gmail.com

unread,
Mar 4, 2019, 10:55:43 AM3/4/19
to Ansible Project
Thanks for answer, for information i have this error :

fatal: [rebebsr6cweb01]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ indice|default([])|union(item.split(',')[0]) }}): can only concatenate list (not \"unicode\") to list"}

Regards,

Jean-Yves LENHOF

unread,
Mar 4, 2019, 10:59:53 AM3/4/19
to ansible...@googlegroups.com


Le 04/03/2019 à 16:28, Jean-Yves LENHOF a écrit :


Le 04/03/2019 à 13:24, ryad9...@gmail.com a écrit :
Hy all,

I have one file with this content:

1,user1,host1,appli1
2,user2,host2,appli2
3,user1,host1.appli3

This task works :

- set_fact:
      indice: "{{ item.split(',')[0] }}"


Change this :

      indice: "{{ item.split(',')[0] }}"

To something like this (not tested, but you should have the idea) :

      indice: "{{ indice|default([])|union(item.split(',')[0]) }}"


Like as said not tested, try to learn a little bit by yourself... It's a string so you have to turn it into a list.

Try this (not tested again)

indice: "{{ indice|default([])|union(item.split(',')[0]|list) }}"



  with_lines: cat /home/ansible/ssh_manager/clef_applicatives.csv
  when: item.split(',')[1] == ansible_user and item.split(',')[2] == inventory_hostname
  delegate_to: localhost

- name: "Affichage de l'indice associe avec user et hostname"
  debug:
      var: indice

BUT my variable indice get only indice 3 but indice must get 1 and 3.

How make for that my variable indice get 1,2 or [1,2]

my variable indice get only last line !

Thank you very much guy !!

Regards,

--
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 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/a6de934b-e9fd-4dc0-bf88-b6267669d66c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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 post to this group, send email to ansible...@googlegroups.com.

ryad9...@gmail.com

unread,
Mar 5, 2019, 5:37:30 AM3/5/19
to Ansible Project
Hy Jean-Yves,

It's works perfectly, you are the very the best i want become as you very !! :)

Thanks,

Regards
Reply all
Reply to author
Forward
0 new messages