csvansiblecheckpoint

Passing Values from a CSV file into an Ansible playbook


The files below are my playbook, inventory, and a CSV file ( my dictionary)

network_objects.yml

---
- hosts: all
  connection: httpapi
  gather_facts: False
  vars_files:
    - 'my_var.yml'

  tasks:
  - name: add-host
    check_point.mgmt.cp_mgmt_host:
      name: New Host 1
      ip_address: 192.0.2.1
      state: present
      auto_publish_session: yes

  - name: add-group
    check_point.mgmt.cp_mgmt_group:
      name: New Group 1
      members:
        - New Host 1
      state: present
      auto_publish_session: yes

my_var.yml

ansible_httpapi_validate_certs: False
ansible_httpapi_use_ssl: True
ansible_network_os: check_point.mgmt.checkpoint
ansible_python_interpreter: "python3"
ansible_user: admin
ansible_password: password

hosts

[check_point]
checkpoint ansible_host=#IP address of checkpoint

users.csv

Name,IP Address,Comments
PCUSER1,10.11.92.44, Created by User Feb 19 2021
PCUSER2,10.11.12.13, Created by User 02/19/2012

readCSV.yml           ---> This  reads a csv file called users.csv

- name: "Check for CSV File"
  hosts: localhost
  gather_facts: no
  tasks:
  - name: Read CSV File and Return a dictionary
    read_csv:
      path: users.csv
      key: name
    register: user

  - ansible.builtin.debug:
      msg: '{{ user }} 

What I want to achieve,

I want to be able to read my CSV and pass those variables in the order the CSV data are listed. Then I can publish the New Host ( with all the Names, IP Addresses, Comments ) to Checkpoint

  - name: add-host
    check_point.mgmt.cp_mgmt_host:
      name: {{ Get all Names  from CSV File }}
      ip_address: {{ Get all IP Addresses from CSV File }}
      comments: {{ Get all Comments from CSV File }}

Solution

  •  - name: add-host
        check_point.mgmt.cp_mgmt_host:
          name: {{ item.key }}
          ip_address: {{ item.value[0]}} 
          comments: {{ item.value[1] }}
       loop: "{{ user |dict2items}}"
    

    I'm not sure for value. i think the ouput of user is { user:[ip_address,comments],...} Look your's structure dictonnaries, i think my purpose is good but adapt it.

    source https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-dictionary