0

I am writing an Ansible task where it installs a set of LDAP entries with the following objectClass and attributes and values.

dn: entry=101,cn=multicast,{{ ipa_base }}
objectClass: multicastAddress
objectClass: top
entry: 101
network: test
ipNumber: 224.0.0.1

I have multiple networks, and each network will have 62 IPs allocated, and each value of the entry and the ipNumber will increment.

Network A:
entry=101 ipNumber: 224.0.0.1
entry=102 ipNumber: 224.0.0.2

Network B:
entry=101 ipNumber: 224.0.1.1
entry=102 ipNumber: 224.0.1.2

Currently, I only have a task where it creates the entries statically, and I would like to write it efficiently where I don't have to create each of the static entries.

- name: Create entries
  ldap_entry:
    dn: entry=101,cn=multicast,{{ ipa_base }}
    state: present
    objectClass:
      - top
      - multicastAddress
    attributes:
      entry: 101
      network: test
      ipNumber: 224.0.0.1
    server_uri: localhost
    bind_dn: "cn=Directory Manager"
    bind_pw: '{{ dmgr_pass }}'

1 Answer 1

2

There are several ways to achieve your goal depending on your exact requirement. Here is one to put you on track in a self explanatory playbook with intermediate debug. Note: for the example and legibility I limited the output to 2 networks with 2 ips each. Just change those figures to fit your needs.

---
- hosts: localhost
  gather_facts: false

  vars:
    number_of_networks: 2
    ips_per_network: 2

    networks_list: "{{ range(0, number_of_networks) }}"
    ips_list: "{{ range(1, ips_per_network + 1) }}"

    networks_ips_product: "{{ networks_list | product(ips_list) }}"

    ipa_base: "dc=whatever,dc=local"

  tasks:
    - name: Create a list of networks starting from 0
      ansible.builtin.debug:
        var: networks_list

    - name: Create a list of ips starting from 1
      ansible.builtin.debug:
        var: ips_list

    - name: Combine every network with every ip in a single list
      ansible.builtin.debug:
        var: networks_ips_product

    - name: Loop on that list in a debug tasks which mimics your ldap task
      vars:
        network_it: "{{ item.0 }}"
        ip_it: "{{ item.1 }}"
        entry: "10{{ ip_it }}"
        ip: "224.0.{{ network_it }}.{{ ip_it }}"
        ldap_entry:
          dn: entry={{ entry }},cn=multicast,{{ ipa_base }}
          state: present
          objectClass:
            - multicastAddress
            - top
          attributes:
            entry: "{{ entry }}"
            network: "Network{{ network_it }}"
            ipNumber: "{{ ip }}"
          server_uri: localhost
          bind_dn: "cn=Directory Manager"
          bind_pw: "v3rys3cr3t"
      ansible.builtin.debug:
        var: ldap_entry
      loop: "{{ networks_ips_product }}"

Running the above playbook gives:

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************

TASK [Create a list of networks starting from 0] ***************************************************************************************************************************************************************************************
ok: [localhost] => {
    "networks_list": [
        0,
        1
    ]
}

TASK [Create a list of ips starting from 1] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "ips_list": [
        1,
        2
    ]
}

TASK [Combine every network with every ip in a single list] ***************************************************************************************************************************************************************************************
ok: [localhost] => {
    "networks_ips_product": [
        [
            0,
            1
        ],
        [
            0,
            2
        ],
        [
            1,
            1
        ],
        [
            1,
            2
        ]
    ]
}

TASK [Loop on that list in a debug tasks which mimics your ldap task] ******************************************************************************************************************************************************************
ok: [localhost] => (item=[0, 1]) => {
    "ansible_loop_var": "item",
    "item": [
        0,
        1
    ],
    "ldap_entry": {
        "attributes": {
            "entry": "101",
            "ipNumber": "224.0.0.1",
            "network": "Network0"
        },
        "bind_dn": "cn=Directory Manager",
        "bind_pw": "v3rys3cr3t",
        "dn": "entry=101,cn=multicast,dc=whatever,dc=local",
        "objectClass": [
            "multicastAddress",
            "top"
        ],
        "server_uri": "localhost",
        "state": "present"
    }
}
ok: [localhost] => (item=[0, 2]) => {
    "ansible_loop_var": "item",
    "item": [
        0,
        2
    ],
    "ldap_entry": {
        "attributes": {
            "entry": "102",
            "ipNumber": "224.0.0.2",
            "network": "Network0"
        },
        "bind_dn": "cn=Directory Manager",
        "bind_pw": "v3rys3cr3t",
        "dn": "entry=102,cn=multicast,dc=whatever,dc=local",
        "objectClass": [
            "multicastAddress",
            "top"
        ],
        "server_uri": "localhost",
        "state": "present"
    }
}
ok: [localhost] => (item=[1, 1]) => {
    "ansible_loop_var": "item",
    "item": [
        1,
        1
    ],
    "ldap_entry": {
        "attributes": {
            "entry": "101",
            "ipNumber": "224.0.1.1",
            "network": "Network1"
        },
        "bind_dn": "cn=Directory Manager",
        "bind_pw": "v3rys3cr3t",
        "dn": "entry=101,cn=multicast,dc=whatever,dc=local",
        "objectClass": [
            "multicastAddress",
            "top"
        ],
        "server_uri": "localhost",
        "state": "present"
    }
}
ok: [localhost] => (item=[1, 2]) => {
    "ansible_loop_var": "item",
    "item": [
        1,
        2
    ],
    "ldap_entry": {
        "attributes": {
            "entry": "102",
            "ipNumber": "224.0.1.2",
            "network": "Network1"
        },
        "bind_dn": "cn=Directory Manager",
        "bind_pw": "v3rys3cr3t",
        "dn": "entry=102,cn=multicast,dc=whatever,dc=local",
        "objectClass": [
            "multicastAddress",
            "top"
        ],
        "server_uri": "localhost",
        "state": "present"
    }
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.