0

I've been scouring the web to help point me in the right direction but to no avail.

I'm trying to take multi-line array from one YML file and parse the values into a different ansible playbook:

services.yml array:

---
Groups:
  services:
    Columns: [service_name, port, protocol, destination]
    Rows: 
    - ['service1', '1234', 'TCP', 'Inbound']
    - ['service2', '5678', 'TCP', 'Inbound']
    - ['service3', '9012', 'TCP', 'Inbound']

Which gets parsed into this powershell task:

    - name: set the services yml file as a var
      ansible.builtin.include_vars:
        file: service.yml
        name: services

    - name: Apply local FW rules
      ansible.windows.win_powershell:
        script: |
          New-NetFirewallRule -DisplayName='{{ item.service_name }}' -LocalPort '{{ item.port }}' -Direction '{{ item.direction }} -Protocol '{{ item.protocol }} -Action Allow
      loop: "{{ services }}"

Looking for a pointer or what kind of data pre-processing we need to do in order to break out this array into a dictonary for use within the loop

So far we've tried a mix of various differnt combinations with limited success. at one point we were able to pass the entire line as an object to POwershell, but that dosen't work becsue we need each comma seperated item as it's own independent value.

1

1 Answer 1

2

Looking at your data structure I think you need to iterate not over the whole object but over the nested list services.groups.services.Rows. Its items are also lists, just written in a different syntax:

- name: set the services yml file as a var
  ansible.builtin.include_vars:
    file: service.yml
    name: services

- name: Apply local FW rules
  ansible.windows.win_powershell:
    script: |
      New-NetFirewallRule -DisplayName='{{ item[0] }}' -LocalPort '{{ item[1] }}' -Direction '{{ item[3] }} -Protocol '{{ item[2] }} -Action Allow
  loop: "{{ services.groups.services.Rows }}"
1
  • This worked! Thanks for the assist!
    – Ben Young
    Commented Feb 3 at 21:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.