Importing list file ?

33 views
Skip to first unread message

Tuyen Nguyen

unread,
Aug 22, 2019, 3:34:54 PM8/22/19
to Ansible Project
Hi

I am using the win_updates module and I want to include a list of approved patches in the whitelist option.  I have the list of all my approved patches in a file already, and I would like to know if and how I can import this file to use?

As in example, can I import the list somehow to be a variable called patchlist ?

And then in the win_updates module, I could do

win_updates:
  category_name:
    - SecurityUpdates
    - CriticalUpdates
    - UpdatesRollups
  state: installed
  whitelist: {{ patchlist }
  reboot: yes




Zolvaring

unread,
Aug 22, 2019, 3:37:34 PM8/22/19
to ansible...@googlegroups.com
I'm not able to test this at the moment, but I think the file lookup is what you want: 

Mylist: "{{ lookup('file', '/path/to/my/yaml/list') }}"

--
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/96e2dda9-3c74-4894-aed6-cf350c18c7b6%40googlegroups.com.

Tuyen Nguyen

unread,
Aug 26, 2019, 8:47:40 AM8/26/19
to Ansible Project

HI

Thank you for directing me in the right direction!

I did the following and it worked


win_updates:
  category_name:
    - SecurityUpdates
    - CriticalUpdates
    - UpdatesRollups
  state: installed
  whitelist:
    - "{{lookup('file','patchlist.txt')}}"
  reboot: yes



On Thursday, August 22, 2019 at 3:37:34 PM UTC-4, Zolvaring wrote:
I'm not able to test this at the moment, but I think the file lookup is what you want: 

Mylist: "{{ lookup('file', '/path/to/my/yaml/list') }}"

On Thu, Aug 22, 2019, 12:34 PM Tuyen Nguyen <daica...@gmail.com> wrote:
Hi

I am using the win_updates module and I want to include a list of approved patches in the whitelist option.  I have the list of all my approved patches in a file already, and I would like to know if and how I can import this file to use?

As in example, can I import the list somehow to be a variable called patchlist ?

And then in the win_updates module, I could do

win_updates:
  category_name:
    - SecurityUpdates
    - CriticalUpdates
    - UpdatesRollups
  state: installed
  whitelist: {{ patchlist }
  reboot: yes




--
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...@googlegroups.com.

Tuyen Nguyen

unread,
Aug 26, 2019, 8:53:03 AM8/26/19
to Ansible Project
Actually, it didn't quite work :(  The task was able to run through but I just realized it was not importing the file in correct as a list for the whitelist.  

Vladimir Botka

unread,
Aug 26, 2019, 10:15:51 AM8/26/19
to Tuyen Nguyen, ansible...@googlegroups.com
On Mon, 26 Aug 2019 05:53:03 -0700 (PDT)
Tuyen Nguyen <daica...@gmail.com> wrote:

> Actually, it didn't quite work :( The task was able to run through but I
> just realized it was not importing the file in correct as a list for the
> whitelist.

Use "from_yaml" filter and take a look what you get

- set_fact:
my_data: "{{ lookup('file','patchlist.txt')|from_yaml }}"
- debug:
var: my_data

Then use "my_data" in the update. This should be something similar to the
line below

whitelist: "{{ my_data.<name_of_the_list_inside_patchlist_txt> }}"

Cheers,

-vlado

Tuyen Nguyen

unread,
Aug 29, 2019, 1:04:55 PM8/29/19
to Ansible Project
Thank you again.  I was able to add it with the |from_yaml like you suggested

- set_fact:
    my_data: "{{lookup('file','patchlist.txt')|from_yaml}}"

and then in the whitelist option of win_updates:
      whitelist: "{{my_data}}"

The only caveat is that I need to make my patchlist.txt file to be in the form of

- patch1
- patch2

instead of it normally being
patch1
patch2

Is there an easy way as part of the Ansible playbook or in the lookup function itself to read the normal file and then convert it to having the - automatically?  Or should I add a new play in the playbook to add the "- " in front of the file?

Vladimir Botka

unread,
Aug 29, 2019, 1:47:25 PM8/29/19
to Tuyen Nguyen, ansible...@googlegroups.com
On Thu, 29 Aug 2019 10:04:55 -0700 (PDT)
Tuyen Nguyen <daica...@gmail.com> wrote:

> - set_fact:
> my_data: "{{lookup('file','patchlist.txt')|from_yaml}}"
> and then in the whitelist option of win_updates:
> whitelist: "{{my_data}}"
>
> The only caveat is that I need to make my patchlist.txt file to be in the
> form of
>
> - patch1
> - patch2
>
> instead of it normally being
> patch1
> patch2
>
> Is there an easy way as part of the Ansible playbook or in the lookup
> function itself to read the normal file and then convert it to having the -
> automatically? Or should I add a new play in the playbook to add the "- "
> in front of the file?

If you want to get list of lines try this

- set_fact:
my_patch_list: "{{ my_patch_list|default([]) + [item] }}"
loop: "{{ lookup('file','patchlist.txt').splitlines() }}"

Cheers,

-vlado

Vladimir Botka

unread,
Aug 29, 2019, 1:55:58 PM8/29/19
to Tuyen Nguyen, ansible...@googlegroups.com
On Thu, 29 Aug 2019 19:47:10 +0200
Vladimir Botka <vbo...@gmail.com> wrote:

> - set_fact:
> my_patch_list: "{{ my_patch_list|default([]) + [item] }}"
> loop: "{{ lookup('file','patchlist.txt').splitlines() }}"

Actually this is the same. It doesn't make sense do decompose/compose a list.

- set_fact:
my_patch_list: "{{ lookup('file','patchlist.txt').splitlines() }}"
Reply all
Reply to author
Forward
0 new messages