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.
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.
Share Improve this question asked Jan 31 at 23:24 Ben YoungBen Young 11 silver badge 1- Parsing YAML has been asked here – Walter Mitty Commented Feb 1 at 13:08
1 Answer
Reset to default 2Looking 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 }}"