最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - Parsing a multi-line YML array into a loop - Stack Overflow

programmeradmin1浏览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.

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
Add a comment  | 

1 Answer 1

Reset to default 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 }}"
发布评论

评论列表(0)

  1. 暂无评论