Skip to content

Commit

Permalink
Set custom facts persistently
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m4rcu5nl committed Dec 3, 2018
1 parent b5cca4b commit c03c2b5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
35 changes: 35 additions & 0 deletions files/set_facts.sh
Original file line number Diff line number Diff line change
@@ -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
21 changes: 2 additions & 19 deletions tasks/authorize_node.yml
Original file line number Diff line number Diff line change
@@ -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 }}
Expand All @@ -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 }}
Expand All @@ -46,7 +30,6 @@
delegate_to: "{{ zerotier_api_delegate }}"

when:
- zerotier_accesstoken is defined
- not ansible_check_mode
tags:
- configuration
Expand Down
10 changes: 10 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c03c2b5

Please sign in to comment.