-3

Please see my existing code and this is working fine as expected.

From below code you can see that I have statically defined the vars for list3 and list4:

- hosts: localhost
  gather_facts: false
  tasks:
  - name: "set fact for snow"
    set_fact:
      list2: "{{ hostvars['192.168.10.20']['list1'] }}"

  - include_tasks: loop1.yml
    vars:
      list3:
        - dev-cn-c1
        - dev-cn-c2
        - dev-cn-c3
        - dev-cn-c7
        - dev-cn-c8
        - dev-cn-c3
        - dev-cn-c10
    loop: "{{ list2 }}"
    loop_control:
      loop_var: outer_item
    when:
      - outer_item.type == 'CpmiGatewayCluster'
      - list3|intersect(outer_item.names)|length > 0
  - debug:
      msg: "{{ list2 }}"

  - include_tasks: loop2.yml
    vars:
      list4:
        - dev-cn-c1
        - dev-cn-c2
        - dev-cn-c3
        - dev-cn-c7
        - dev-cn-c8
        - dev-cn-c3
        - dev-cn-c10
    loop: "{{ list2 }}"
    loop_control:
      loop_var: outer_item
    when:
      - outer_item.type == "simple-gateway"
      - list4|intersect(outer_item.name)|length > 0

I want to dynamically read these vars from a CSV file, int.csv.

devicename,location
dev-cn-c1,32
dev-cn-c2,32
dev-cn-c3,56
dev-cn-c4,56

Is that possible?

U880D
  • 8,601
  • 6
  • 24
  • 40
mikeraj2019
  • 39
  • 2
  • 10
  • 1
    As of Ansible version 2.8 you may take advantage from module [read_csv](https://docs.ansible.com/ansible/latest/modules/read_csv_module.html). – U880D Sep 23 '19 at 11:50
  • 1
    The first result on the following page should help you: https://www.google.com/search?q=read+vars+from+csv+file+using+ansible. As you can see, I literally used the title of your above question. – Zeitounator Sep 23 '19 at 11:57

1 Answers1

1

As of Ansible version 2.8 you may achieve your goal by using the module read_csv. The documentation there shows already a slightly similar example to yours.

U880D
  • 8,601
  • 6
  • 24
  • 40