Compare commits

...

27 Commits

Author SHA1 Message Date
Marcus ca3cc077d6
Merge pull request #35 from m4rcu5nl/develop
Release v1.2.3
2020-03-13 01:35:01 +01:00
Marcus 904dcb399d Merge branch 'feature/gpg_fingerprint_variable' into develop 2020-03-09 23:19:28 +01:00
Marcus 0b9f100aa5 Use variable for gpg fingerprint
Using the fingerprint in the rpm_key module requires Ansible 2.9. Updated meta file to reflect this requirement.
2020-03-09 02:44:38 +01:00
Marcus 90167ae771
Merge pull request #30 from ClashTheBunny/pgp_key_id
Add PGP key `id` for added security
2020-03-09 02:33:09 +01:00
Marcus a84ea36b37
Merge pull request #31 from quivalen/set_fact.sh-patch-1
Update set_fact.sh to support multiple network
2020-03-09 02:28:44 +01:00
Marcus dc68166c27
Merge pull request #32 from dsteinkopf/master
fix usage of register variable function
2020-03-09 02:16:00 +01:00
Dirk Steinkopf c9339c1f3b fix usage of register variable function 2019-11-16 18:23:45 +01:00
Andrea Lora 0755fa30b2
Update set_fact.sh to support multiple network
Currently the role will fail to regather fact if $network_count > 1

That's because without the quotes echo will suppress the \n writing everything in one line. Thus wc -l will return `1`, confusing the file_content function that will lead to an incorrect json being produced
2019-07-08 11:40:05 +01:00
Randall Mason 76b57fb097 Add PGP key `id` for added security 2019-06-09 12:50:53 -05:00
Marcus 49d61b875e
Merge pull request #29 from papanito/papanito/correct-readme
Papanito/correct readme
2019-04-21 04:04:09 +02:00
papanito ffba32f382 Fix some minor typos in text 2019-04-20 20:59:35 +02:00
papanito 2b56aaf46c Correct typo in example playbook 2019-04-20 20:53:09 +02:00
Marcus Meurs 154899586e Use the same fact syntax throughout the role
Create consistency by using the ansible_facts['somefact'] syntax everywhere.
2018-12-10 23:47:16 +01:00
Marcus Meurs c7aae63e16 Merge branch 'hotfix/v1.2.2' 2018-12-08 02:26:57 +01:00
Marcus Meurs a9fa164ebe Merge branch 'hotfix/v1.2.2' into develop 2018-12-08 02:26:57 +01:00
Marcus Meurs 5431276c8e Missed a pretty important comma 2018-12-08 02:25:12 +01:00
Marcus Meurs 88b61fdcea Merge branch 'release/v1.2.1' 2018-12-07 23:48:31 +01:00
Marcus Meurs a0228786d3 Merge branch 'release/v1.2.1' into develop 2018-12-07 23:48:30 +01:00
Marcus Meurs fd555f86ac Update Galaxy meta and readme
Readme mostly formatting changes. Meta file now includes supported Ubuntu releases.
2018-12-07 23:44:16 +01:00
Marcus Meurs 95d9213746 Adds network device to ansible_local.zerotier
Also removes the conditional for skipping installation based on ansible_local.zerotier value. Skipping tasks like that should be up to the user, not the role.
2018-12-07 23:40:48 +01:00
Marcus Meurs b86bc4c73d Rearrange conditionals
release_repo.status needs to be checked after distribution.
2018-12-07 23:35:32 +01:00
Marcus Meurs fd68894bad Possible fix for #26
If ansible_distribution is Ubuntu the role will check if there is a dedicated repo matching the ansible_distribution_release. If this is not the case but the ansible_distribution_major_version is 18, the repo for the bionic release will be used.
2018-12-06 05:35:32 +01:00
Marcus Meurs 4dac8000fc Variable naming convention
Updated 2 variable names to follow naming convention. For backwards compatibility the old names are rewritten to the new ones in the role's defaults.
2018-12-04 05:00:38 +01:00
Marcus Meurs 618ae55bd7 Merge branch 'feature/persistent_custom_facts' into develop 2018-12-04 02:50:01 +01:00
Marcus Meurs 046415b168 Fix for loop generating invalid json 2018-12-04 02:48:29 +01:00
Marcus Meurs 672c67e087 Reduce unnecessary API calls
The role will no longer make API calls to authorize already authorized members to a network.
2018-12-04 01:34:39 +01:00
Marcus Meurs c03c2b5a3f 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.
2018-12-03 07:05:37 +01:00
10 changed files with 135 additions and 52 deletions

View File

@ -3,38 +3,53 @@
ZeroTier
=========
This Ansible role installs the `zerotier-one` package, adds and authorizes new members to (existing) ZeroTier networks, and tells the new member to join the network.
This Ansible role adds the ZeroTier repository and installs the `zerotier-one` package using your system's package manager. Depending on the provided variables this role can also add and authorize new members to (existing) ZeroTier networks, and tell the new member to join the network.
Requirements
------------
This role has an optional access token variable to authorize the member using the ZeroTier API. The role also takes the ID of the ZeroTier network to automatically join the new member.
Technically this role has no requirements. If it's ran without any variables set it will only run the installation tasks. The following variables impact the role's behavior:
[**zerotier_network_id**](#zerotier_network_id): when set hosts are told to join this network.
[**zerotier_api_accesstoken**](#zerotier_api_accesstoken): when set the role can handle member authentication and configuration using the ZeroTier API.
Role Variables
--------------
### zerotier_api_url
The url where the Zerotier API lives. Must use HTTPS protocol.
Default: https://my.zerotier.com
### zerotier_accesstoken
The access token needed to authorize with the ZeroTier API. You can generate one in your account settings at https://my.zerotier.com/. If this is left out then the newly joined member will not be automatically authorized.
### zerotier_network_id
The 16 character network ID of the network the new members should join. The node will not join any network if omitted.
*Type*: string
*Default value*:
*Description*: The 16 character network ID of the network the new members should join. The node will not join any network if omitted.
### zerotier_register_short_hostname
Used to register the short hostname (without the FQDN) on the network instead of the long one.
Default: `false`
### zerotier_member_register_short_hostname
*Type*: boolean
*Default value*: `false`
*Description*: By default `inventory_hostname` will be used to name a member in a network. If set to `true`, `inventory_hostname_short` will be used instead.
### zerotier_member_ip_assignments
A list of IP addresses to assign this member. The member will be automatically assigned an address on the network if left out.
*Type*: list
*Default value*: `[]`
*Description*: A list of IP addresses to assign this member. The member will be automatically assigned an address on the network if left out.
### zerotier_member_description
Optional desription for a member.
*Type*: string
*Default value*: `""`
*Description*: Optional description for a member.
### zerotier_api_accesstoken
*Type*: string
*Default value*: `""`
*Description*: The access token needed to authorize with the ZeroTier API. You can generate one in your account settings at https://my.zerotier.com/. If this is left out then the newly joined member will not be automatically authorized.
### zerotier_api_url
*Type*: string
*Default value*: `https://my.zerotier.com`
*Description*: The url where the Zerotier API lives. Must use HTTPS protocol.
### zerotier_api_delegate
Option to delegate tasks for Zerotier API calls. By default the API calls are made from the machine running the role.
*Type*: string
*Default value*: `localhost`
*Description*: Option to delegate tasks for Zerotier API calls. This is useful in a situation where API calls can only be made from a white-listed management server, for example.
Example Playbook
----------------
@ -43,7 +58,7 @@ Example Playbook
- hosts: servers
vars:
zerotier_network_id: 1234567890qwerty
zerotier_accesstoken: "{{ vault_zerotier_accesstoken }}"
zerotier_api_accesstoken: "{{ vault_zerotier_accesstoken }}"
zerotier_register_short_hostname: true
roles:
@ -75,4 +90,4 @@ Example Inventory
[dbservers:vars]
zerotier_member_description='<AppName> db cluster node'
```
```

View File

@ -1,7 +1,8 @@
---
# defaults file for ansible-role-zerotier
zerotier_api_accesstoken: "{{ zerotier_accesstoken | default() }}" # For backwards compatibility
zerotier_api_url: https://my.zerotier.com
zerotier_api_delegate: localhost
zerotier_apt_state: present
zerotier_register_short_hostname: false
zerotier_member_register_short_hostname: "{{ zerotier_register_short_hostname | default(false) }}" # For backwards compatibility
zerotier_authorize_member: true

43
files/set_facts.sh Normal file
View File

@ -0,0 +1,43 @@
#!/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.

View File

@ -16,7 +16,7 @@ galaxy_info:
# - CC-BY
license: GPLv3
min_ansible_version: 2.4
min_ansible_version: 2.9
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
@ -27,7 +27,7 @@ galaxy_info:
# this branch. If Travis integration is configured, only notifications for this
# branch will be accepted. Otherwise, in all cases, the repo's default branch
# (usually master) will be used.
#github_branch:
github_branch: master
#
# platforms is a list of platforms, and each platform has a name and a list of versions.
@ -39,6 +39,10 @@ galaxy_info:
- name: Debian
versions:
- stretch
- name: Ubuntu
versions:
- Bionic
- Cosmic
- name: Fedora
versions:
- 28

View File

@ -1,52 +1,38 @@
---
- 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
- name: Authorize new 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 }}
Authorization: bearer {{ zerotier_api_accesstoken }}
body:
hidden: false
config:
authorized: "{{ zerotier_authorize_member }}"
body_format: json
register: auth_apiresult
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:
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 }}
Authorization: bearer {{ zerotier_api_accesstoken }}
body:
name: "{{ zerotier_register_short_hostname | ternary(inventory_hostname_short, inventory_hostname) }}"
name: "{{ zerotier_member_register_short_hostname | ternary(inventory_hostname_short, inventory_hostname) }}"
description: "{{ zerotier_member_description | default() }}"
config:
ipAssignments: "{{ zerotier_member_ip_assignments | default([]) | list }}"
body_format: json
register: conf_apiresult
register: conf_apiresult
delegate_to: "{{ zerotier_api_delegate }}"
when:
- zerotier_accesstoken is defined
- not ansible_check_mode
tags:
- configuration

View File

@ -1,6 +1,6 @@
---
- include_tasks: install/{{ ansible_os_family }}.yml
- include_tasks: install/{{ ansible_facts['os_family'] }}.yml
tags:
- installation
- repositories

View File

@ -1,9 +1,31 @@
- name: Add ZeroTier PGP key
apt_key:
url: "{{ zerotier_gpg_url }}"
id: "{{ zerotier_gpg_fingerprint }}"
- name: Check if Ubuntu release has dedicated repo
uri:
url: "{{ zerotier_download_base_url }}/debian/{{ zerotier_deb_release_repo }}"
failed_when: false
when:
- ansible_facts['distribution'] == "Ubuntu"
register: release_repo
- block:
- name: Overwrite Ubuntu release repo name
set_fact:
zerotier_deb_release_repo: bionic
- name: Re-gather facts
setup: ~
when:
- ansible_facts['distribution'] == "Ubuntu"
- ansible_facts['distribution_major_version'] == "18"
- release_repo.status == 404
- name: Add ZeroTier APT repository
apt_repository:
repo: deb {{ zerotier_download_base_url }}/debian/{{ ansible_distribution_release }} {{ ansible_distribution_release }} main
repo: deb {{ zerotier_download_base_url }}/debian/{{ zerotier_deb_release_repo }} {{ zerotier_deb_release_repo }} main
filename: zerotier
register: zerotier_repo

View File

@ -2,6 +2,7 @@
rpm_key:
state: present
key: "{{ zerotier_gpg_url }}"
fingerprint: "{{ zerotier_gpg_fingerprint }}"
- name: Add ZeroTier repo for RHEL/CentOS
yum_repository:
@ -11,7 +12,7 @@
gpgcheck: yes
enabled: yes
register: zerotier_repo
when: ansible_distribution != "Fedora"
when: ansible_facts['distribution'] != "Fedora"
- name: Add zerotier repo for Fedora
yum_repository:
@ -21,4 +22,4 @@
gpgcheck: yes
enabled: yes
register: zerotier_repo
when: ansible_distribution == "Fedora"
when: ansible_facts['distribution'] == "Fedora"

View File

@ -2,11 +2,20 @@
# tasks file for ansible-role-zerotier
- import_tasks: install.yml
when:
- not skip_install|default(false)|bool
- 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_accesstoken is defined
- zerotier_api_accesstoken | length > 0
- ansible_local['zerotier']['node_id'] is defined
- import_tasks: join_network.yml
when:

View File

@ -1,4 +1,6 @@
---
# vars file for ansible-role-zerotier
zerotier_download_base_url: http://download.zerotier.com
zerotier_deb_release_repo: "{{ ansible_facts['distribution_release'] }}"
zerotier_gpg_url: https://download.zerotier.com/contact@zerotier.com.gpg
zerotier_gpg_fingerprint: 74A5E9C458E1A431F1DA57A71657198823E52A61