๐Ÿ“— Ansible playbooks and roles for building an idempotent, interconnected and scalable infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
ansible-playbooks/tasks/select_node.yml

85 lines
2.5 KiB

# select a cluster node that can host a role
- name: ensure there is at least one cluster node
fail:
msg: no cluster nodes are defined
when: (groups['nodes'] | d([]) | length) == 0
- name: clear selected_node
set_fact:
selected_node: ""
- name: preference-based node determination
block:
- name: determine first cluster node which prefers to host role
set_fact:
selected_node: "{{ item }}"
when:
- hostvars[item] is defined
- hostvars[item].can_host is defined
- hostvars[item].can_host is iterable
- hostvars[item].can_host is not mapping
- hostvars[item].can_host is not string
- hostvars[item].can_host[primary_role | d(inventory_hostname)] is defined
- (hostvars[item].never_host is not defined) or
(hostvars[item].never_host[primary_role | d(inventory_hostname)] is not defined)
- not selected_node
loop: "{{ groups['nodes'] }}"
when: (hosted_on is not defined) and (inventory_hostname is defined)
- name: pin-based node determination
block:
- name: fail if pinned node is not a string
fail:
msg: 'pinned node for "{{ inventory_hostname }}" must be a string'
when: hosted_on is not string
- name: fail if pinned node does not exist
fail:
msg: '"{{ inventory_hostname }}" has a nonexistent pinned node "{{ hosted_on }}"'
when: hostvars[hosted_on] is not defined
- name: select pinned cluster node
set_fact:
selected_node: "{{ hosted_on }}"
when: (hosted_on is defined) and (inventory_hostname is defined)
- name: fallback to first cluster node
set_fact:
selected_node: "{{ groups['nodes'][0] }}"
when: not selected_node
- name: determine actual node in a cluster configuration
block:
- set_fact:
is_node_found: no
- name: determine on which node this container is currently hosted on
include_tasks: tasks/query_node.yml
loop: "{{ groups['nodes'] }}"
loop_control:
loop_var: node
when: groups['nodes'] | length >= 2
rescue:
- name: report that cluster cannot be queried
debug:
msg: 'cluster cannot be queried or all nodes are inaccessible, will keep cluster mode: "{{ selected_node }}"'
- name: report which node was selected
debug:
msg: 'using cluster node: "{{ selected_node }}"'
- name: ensure this node exists and can be connected to
wait_for_connection:
timeout: 10
delegate_to: "{{ selected_node }}"