Importing list file ?

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

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’) }}”

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

Actually, it didn’t quite work :frowning: 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

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?

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

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() }}"