From c03c2b5a3f9c40f01a60a57b69e4195874790f51 Mon Sep 17 00:00:00 2001 From: Marcus Meurs Date: Mon, 3 Dec 2018 07:05:37 +0100 Subject: [PATCH 1/3] Set custom facts persistently Create /etc/ansible/facts.d/zerotier.fact on each node containing custom facts in json format. This can then be used to prevent pointless reconfiguration of existing nodes whenever a new one is added to the inventory. In this commit it merely skips the installation tasks. --- files/set_facts.sh | 35 +++++++++++++++++++++++++++++++++++ tasks/authorize_node.yml | 21 ++------------------- tasks/main.yml | 10 ++++++++++ 3 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 files/set_facts.sh diff --git a/files/set_facts.sh b/files/set_facts.sh new file mode 100644 index 0000000..ffbef4c --- /dev/null +++ b/files/set_facts.sh @@ -0,0 +1,35 @@ +#!/bin/bash +FACTS_DIR='/etc/ansible/facts.d' +FACT_FILE="${FACTS_DIR}/zerotier.fact" +NODE_STATUS=($(zerotier-cli status)) +NETWORKS=$(zerotier-cli listnetworks | tail -n+2) + +function file_content { + if [ ! -z "$NETWORKS" ]; then + echo "{" + echo " \"node_id\":\"${NODE_STATUS[2]}\"," + echo " \"networks\": [" + while read -r; do + network=($REPLY) + echo " {" + echo " \"id\":\"${network[2]}\"," + echo " \"status\":\"${network[5]}\"" + echo " }" + done <<< $NETWORKS + echo " ]" + echo "}" + else + echo "{\"node_id\":\"${NODE_STATUS[2]}\"}" + fi +} + +if [ ! -d "$FACTS_DIR" ]; then + mkdir -p $FACTS_DIR +fi + +file_content > $FACT_FILE + + +# TO-DO +# Consider something that hadles JSON better than Bash does +# The above will fail when it runs in to more than 1 network diff --git a/tasks/authorize_node.yml b/tasks/authorize_node.yml index e291655..a4cd763 100644 --- a/tasks/authorize_node.yml +++ b/tasks/authorize_node.yml @@ -1,24 +1,8 @@ --- -- block: - - name: Get Zerotier NodeID - shell: zerotier-cli info | awk '{print $3}' - register: nodeid - changed_when: false - - - name: Set NodeID as fact - set_fact: - zerotier_node_id: "{{ nodeid.stdout }}" - - when: - - zerotier_accesstoken is defined - - not ansible_check_mode - tags: - - configuration - - block: - name: Authorize members to network uri: - url: "{{ zerotier_api_url }}/api/network/{{ zerotier_network_id }}/member/{{ zerotier_node_id }}" + url: "{{ zerotier_api_url }}/api/network/{{ zerotier_network_id }}/member/{{ ansible_local.zerotier.node_id }}" method: POST headers: Authorization: bearer {{ zerotier_accesstoken }} @@ -32,7 +16,7 @@ - name: Configure members in network uri: - url: "{{ zerotier_api_url }}/api/network/{{ zerotier_network_id }}/member/{{ zerotier_node_id }}" + url: "{{ zerotier_api_url }}/api/network/{{ zerotier_network_id }}/member/{{ ansible_local.zerotier.node_id }}" method: POST headers: Authorization: bearer {{ zerotier_accesstoken }} @@ -46,7 +30,6 @@ delegate_to: "{{ zerotier_api_delegate }}" when: - - zerotier_accesstoken is defined - not ansible_check_mode tags: - configuration diff --git a/tasks/main.yml b/tasks/main.yml index 0b2aa25..a498ec8 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -3,10 +3,20 @@ - import_tasks: install.yml when: - not skip_install|default(false)|bool + - ansible_local.zerotier is not defined + +- block: + - name: Update ansible_local facts + script: set_facts.sh + + - name: Re-gather facts + setup: ~ + - import_tasks: authorize_node.yml when: - zerotier_accesstoken is defined + - ansible_local.zerotier.node_id is defined - import_tasks: join_network.yml when: From 672c67e087fd2a4da6e262bbe5db956c6666b382 Mon Sep 17 00:00:00 2001 From: Marcus Meurs Date: Tue, 4 Dec 2018 01:06:35 +0100 Subject: [PATCH 2/3] Reduce unnecessary API calls The role will no longer make API calls to authorize already authorized members to a network. --- files/set_facts.sh | 13 ++++++------- tasks/authorize_node.yml | 4 +++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/files/set_facts.sh b/files/set_facts.sh index ffbef4c..fed1f2f 100644 --- a/files/set_facts.sh +++ b/files/set_facts.sh @@ -8,18 +8,17 @@ function file_content { if [ ! -z "$NETWORKS" ]; then echo "{" echo " \"node_id\":\"${NODE_STATUS[2]}\"," - echo " \"networks\": [" + echo " \"networks\": {" while read -r; do network=($REPLY) - echo " {" - echo " \"id\":\"${network[2]}\"," - echo " \"status\":\"${network[5]}\"" - echo " }" + echo " \"${network[2]}\": {" + echo " \"status\":\"${network[5]}\"" + echo " }" done <<< $NETWORKS - echo " ]" + echo " }" echo "}" else - echo "{\"node_id\":\"${NODE_STATUS[2]}\"}" + echo "{\"node_id\":\"${NODE_STATUS[2]}\",\"networks\":{}}" fi } diff --git a/tasks/authorize_node.yml b/tasks/authorize_node.yml index a4cd763..d114e25 100644 --- a/tasks/authorize_node.yml +++ b/tasks/authorize_node.yml @@ -1,6 +1,6 @@ --- - block: - - name: Authorize members to network + - name: Authorize new members to network uri: url: "{{ zerotier_api_url }}/api/network/{{ zerotier_network_id }}/member/{{ ansible_local.zerotier.node_id }}" method: POST @@ -13,6 +13,8 @@ body_format: json register: auth_apiresult delegate_to: "{{ zerotier_api_delegate }}" + when: + - ansible_local.zerotier.networks[zerotier_network_id] is not defined or ansible_local.zerotier.networks[zerotier_network_id].status != 'OK' - name: Configure members in network uri: From 046415b1686f9a744c581d2de2363e0b1f03ba1b Mon Sep 17 00:00:00 2001 From: Marcus Meurs Date: Tue, 4 Dec 2018 02:48:29 +0100 Subject: [PATCH 3/3] Fix for loop generating invalid json --- files/set_facts.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/files/set_facts.sh b/files/set_facts.sh index fed1f2f..bc895d4 100644 --- a/files/set_facts.sh +++ b/files/set_facts.sh @@ -6,6 +6,9 @@ NETWORKS=$(zerotier-cli listnetworks | tail -n+2) function file_content { if [ ! -z "$NETWORKS" ]; then + network_count=$(echo $NETWORKS |wc -l) + counter=1 + echo "{" echo " \"node_id\":\"${NODE_STATUS[2]}\"," echo " \"networks\": {" @@ -13,7 +16,13 @@ function file_content { network=($REPLY) echo " \"${network[2]}\": {" echo " \"status\":\"${network[5]}\"" - echo " }" + + if [ "$counter" -eq "$network_count" ]; then + echo " }" + else + echo " }," + fi + ((counter++)) done <<< $NETWORKS echo " }" echo "}" @@ -31,4 +40,3 @@ file_content > $FACT_FILE # TO-DO # Consider something that hadles JSON better than Bash does -# The above will fail when it runs in to more than 1 network