yaml Archives - ISbyR https://isbyr.com/tag/yaml/ Infrequent Smarts by Reshetnikov Fri, 24 Jun 2022 11:43:09 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.2 Ansible – loop over netsted variables https://isbyr.com/ansible-loop-over-netsted-variables/ https://isbyr.com/ansible-loop-over-netsted-variables/#respond Tue, 03 Dec 2019 05:51:04 +0000 http://isbyr.com/?p=565 I’ve started using Ansible at my work, where we use it to deploy Splunk environments. One of the things I needed to do is to provide a list of tcp ports to a “with_items” statement in a form of list. I have this vars file and I needed to filter out only the TCP ports … Continue reading Ansible – loop over netsted variables

The post Ansible – loop over netsted variables appeared first on ISbyR.

]]>
I’ve started using Ansible at my work, where we use it to deploy Splunk environments.

One of the things I needed to do is to provide a list of tcp ports to a “with_items” statement in a form of list.

I have this vars file and I needed to filter out only the TCP ports

port:
  dns:
    tcp: 5001
    udp: 5002
  dual:
    tcp: 6001
    udp: 6002
  relay:
    tcp: 7001
    udp: 7002

So here is how you can do it (in 2 steps) in Ansible.

    - name: set_fact
      set_fact:
        tcp_ports: "{% for feed in port %}{{ port[feed]['tcp'] }},{% endfor %}"

    - name: print tcp ports
      debug:
        msg: "{{ item }}"
      with_items:
      - "{{ (tcp_ports| regex_replace(',', '')).split (',') | list }}"

Of course you can replace the debug msg with whatever other action you need.
By the way, if you know how to do it in a single step please let me know.

The post Ansible – loop over netsted variables appeared first on ISbyR.

]]>
https://isbyr.com/ansible-loop-over-netsted-variables/feed/ 0