From a2063b191c531f91f0004c7713d5eb7dc1484635 Mon Sep 17 00:00:00 2001 From: Hannu Teulahti Date: Mon, 8 Feb 2021 10:58:24 +0200 Subject: [PATCH] Use python script for facts possibly fixes https://github.com/m4rcu5nl/ansible-role-zerotier/issues/28 --- files/set_facts.sh | 43 ------------------------------------------ files/zerotier.fact.py | 42 +++++++++++++++++++++++++++++++++++++++++ tasks/install.yml | 15 +++++++++++++++ tasks/main.yml | 8 -------- 4 files changed, 57 insertions(+), 51 deletions(-) delete mode 100644 files/set_facts.sh create mode 100644 files/zerotier.fact.py diff --git a/files/set_facts.sh b/files/set_facts.sh deleted file mode 100644 index a111d59..0000000 --- a/files/set_facts.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/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 - network_count=$(echo "$NETWORKS" |wc -l) - counter=1 - - echo "{" - echo " \"node_id\":\"${NODE_STATUS[2]}\"," - echo " \"networks\": {" - while read -r; do - network=($REPLY) - echo " \"${network[2]}\": {" - echo " \"status\":\"${network[5]}\"," - echo " \"device\":\"${network[7]}\"" - - if [ "$counter" -eq "$network_count" ]; then - echo " }" - else - echo " }," - fi - ((counter++)) - done <<< $NETWORKS - echo " }" - echo "}" - else - echo "{\"node_id\":\"${NODE_STATUS[2]}\",\"networks\":{}}" - fi -} - -if [ ! -d "$FACTS_DIR" ]; then - mkdir -p $FACTS_DIR -fi - -file_content > $FACT_FILE - - -# TO-DO -# Handle different states than "OK". Other statuses can mess up positions. diff --git a/files/zerotier.fact.py b/files/zerotier.fact.py new file mode 100644 index 0000000..b820561 --- /dev/null +++ b/files/zerotier.fact.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import json +import subprocess + +out = subprocess.Popen(['/usr/sbin/zerotier-cli', '-j', 'info'], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + +stdout, stderr = out.communicate() + +try: + info = json.loads(stdout) +except: + print('zerotier-cli error. Are you sure you are running this as root?') + exit(1) + +j = { + 'node_id': info['address'] +} + +out = subprocess.Popen(['/usr/sbin/zerotier-cli', '-j', 'listnetworks'], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + +stdout, stderr = out.communicate() + +try: + networks = json.loads(stdout) +except: + print('zerotier-cli error. Are you sure you are running this as root?') + exit(2) + +n = {} + +for network in networks: + n[network['id']] = { + 'status': network['status'], + 'device': network['portDeviceName'], + } + +j['networks'] = n + +print(json.dumps(j, indent=2)) diff --git a/tasks/install.yml b/tasks/install.yml index 8d07188..b752c41 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -22,6 +22,21 @@ notify: - enable zerotier-one + - name: create facts.d + file: + path: /etc/ansible/facts.d + recurse: true + state: directory + + - name: Install zerotier custom facts + copy: + src: zerotier.fact.py + dest: /etc/ansible/facts.d/zerotier.fact + mode: 0755 + + - name: Re-gather facts + setup: ~ + when: - zerotier_repo is not defined or zerotier_repo is succeeded - not ansible_check_mode diff --git a/tasks/main.yml b/tasks/main.yml index 29183f9..910abea 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -4,14 +4,6 @@ when: - not skip_install | default(false) | bool -- block: - - name: Update ansible_local facts - script: set_facts.sh - - - name: Re-gather ansible_local facts - setup: filter=ansible_local - - - import_tasks: authorize_node.yml when: - zerotier_api_accesstoken | length > 0