Looping contruct - linode.cloud.nodebalancer

28 views
Skip to first unread message

John Stoffel

unread,
Mar 28, 2022, 4:25:57 PM3/28/22
to Ansible Project
Hi,
I'm looking for some help in setting up a Linode Nodebalancer, but I think my question is more about how to build a more dynamic playbook using the 'linode.cloud.nodebalancer' module from Ansible Galaxy.  

So I could hardcode my playbook to use two nodes (hosts) and thier private IPs for the setup.  But what if I want to have three hosts or more behind the nodebalancer?  An example playbook looks like this:

---
- name: Create single linode nodebalancer
  hosts: localhost
  tasks:

    - name: Create a Linode NodeBalancer
      linode.cloud.nodebalancer:
        label: "{{ nb_label }}"
        region: "{{ nb_region }}"
        tags: nb_tags
        state: present
        configs:
          - port: 80
            protocol: http
            algorithm: roundrobin
            nodes:
              - label: 'test-node1'
                address: 192.168.10.3:80
               - label: 'test-node2'
                 address: 192.168.175.221:80

So I think could do do a loop over the nodes: block, but the role doesn't seem to support a 'state: present' or 'state:absent' flag, so I *think* that I have to run my task with all my definitions setup ahead of time.  Which kind of ruins the ideal of idempotency.  

So would this be the way to do a loop, starting with creating the linodes and setting facts: 
   
---
- name: Create linode using linode.cloud.instance
  linode.cloud.instance:
    label: "{{ linode.name }}"
    type: "{{ linode.type | default(def_type) }}"
    region: "{{ linode.region | default(def_region) }}"
    image: "{{ linode.image | default(def_image) }}"
    authorized_keys: "{{ ssh_keys }}"
    root_pass: "{{ password }}"
    state: present
    private_ip: true
    wait: true
  register: my_linode

- name: "Found one or more linodes"
  debug:
    msg="Name={{ my_linode.instance.label }}, ID={{ my_linode.instance.id }}, IP(s)= {{ my_linode.i\
nstance.ipv4 }}"
  when: my_linode|length > 0


- name: "Set a fact of private IPs"
  set_fact:
    private_ips: "{{ private_ips }} + [ { label: {{ my_linode.instance.label }} , IP: {{ my_linode.\
instance.ipv4[1] }} } ]"



- name: Create a Linode NodeBalancer using loop
      linode.cloud.nodebalancer:
        label: "{{ nb_label }}"
        region: "{{ nb_region }}"
        tags: nb_tags
        state: present
        configs:
          - port: 80
            protocol: http
            algorithm: roundrobin
            nodes:
              - label: {{ item.label }}
                address: {{ item.IP}}
             loop:
                 private_ips

Or do I need to do something funkier?  This is honestly where my ansible-fu fails me...

Reply all
Reply to author
Forward
0 new messages