Skip to content

Commit

Permalink
Merge pull request #31 from Oefenweb/cs-fixes
Browse files Browse the repository at this point in the history
Make use of fully-qualified collection name
  • Loading branch information
tersmitten authored May 20, 2022
2 parents 82ab756 + 4b15ae5 commit e7fc3af
Show file tree
Hide file tree
Showing 18 changed files with 73 additions and 57 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
python-version: '3.x'

- name: Install test dependencies
run: pip install ansible-lint[community,yamllint]
run: |
pip install ansible-lint
ansible-galaxy install -r requirements.yml
- name: Lint code
run: |
Expand All @@ -43,11 +45,8 @@ jobs:
matrix:
include:
- distro: debian8
ansible-version: '<2.10'
- distro: debian9
- distro: debian10
- distro: ubuntu1604
ansible-version: '>=2.9, <2.10'
- distro: ubuntu1604
ansible-version: '>=2.10, <2.11'
- distro: ubuntu1604
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ ansible.cfg
# Other files #
###############
!empty

files/id_rsa*
13 changes: 8 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
FROM ubuntu:16.04
FROM ubuntu:18.04
MAINTAINER Mischa ter Smitten <mtersmitten@oefenweb.nl>

ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

# python
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y python-minimal python-dev curl && \
DEBIAN_FRONTEND=noninteractive apt-get install -y python3-minimal python3-dev curl && \
apt-get clean
RUN curl -sL https://bootstrap.pypa.io/pip/2.7/get-pip.py | python -
RUN curl -sL https://bootstrap.pypa.io/pip/3.6/get-pip.py | python3 -
RUN rm -rf $HOME/.cache

# ansible
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y gcc libffi-dev libssl-dev && \
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y python3-apt && \
apt-get clean
RUN pip install ansible==2.9.15
RUN pip3 install ansible==2.10.7
RUN rm -rf $HOME/.cache

# provision
Expand Down
5 changes: 2 additions & 3 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# meta file
---
galaxy_info:
namespace: oefenweb
author: oefenweb
role_name: ssh_keys
author: Mischa ter Smitten
company: Oefenweb.nl B.V.
description: Manage ssh public key authentication (public / private / authorized keys and known hosts) in Debian-like systems
license: MIT
min_ansible_version: 2.9.0
min_ansible_version: 2.10.0
platforms:
- name: Ubuntu
versions:
Expand Down
6 changes: 6 additions & 0 deletions molecule/default/collections.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
collections:
- name: community.docker
version: '>=1.2.0,<2'
- name: community.general
version: '>=2,<3'
5 changes: 5 additions & 0 deletions requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# requirements file
---
collections:
- name: ansible.posix
- name: community.crypto
2 changes: 1 addition & 1 deletion tasks/authorized-keys.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tasks file
---
- name: authorized-keys | set up for users
authorized_key:
ansible.posix.authorized_key:
user: "{{ item.owner }}"
key: "{{ lookup('file', item.src) }}"
state: "{{ item.state | default('present') }}"
Expand Down
4 changes: 2 additions & 2 deletions tasks/general.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tasks file
---
- name: general | create ssh directory
file:
ansible.builtin.file:
path: "{{ (item.dest_absolute | default('~' + item.owner + '/' + ssh_keys_sshdir + '/' + (item.dest | default(ssh_keys_private_key_filename)))) | dirname }}"
state: directory
owner: "{{ item.owner }}"
Expand All @@ -14,7 +14,7 @@
- ssh-keys-general-create-ssh-directories-private-keys

- name: general | create ssh directory
file:
ansible.builtin.file:
path: "~{{ item.owner }}/{{ ssh_keys_sshdir }}"
state: directory
owner: "{{ item.owner }}"
Expand Down
20 changes: 11 additions & 9 deletions tasks/generate.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# tasks file
---
- block:
- name: generate
block:

- name: generate | create ssh key generation directory
file:
ansible.builtin.file:
path: "{{ item.path | dirname }}"
owner: "{{ item.owner }}"
group: "{{ item.group | default(item.owner) }}"
Expand All @@ -14,17 +15,18 @@
- ssh-keys-generate-directories

- name: generate | generate private ssh key
shell: "{{ ssh_keys_generate_keys_command | default(_ssh_keys_generate_keys_command) }}"
tags:
- ssh-keys-generate-private-keys
- skip_ansible_lint
ansible.builtin.shell: >
{{ ssh_keys_generate_keys_command | default(_ssh_keys_generate_keys_command) }}
with_items: "{{ ssh_keys_generate_keys }}"
when: >-
item.force | default(false) | bool or
item.path is not file
with_items: "{{ ssh_keys_generate_keys }}"
tags:
- ssh-keys-generate-private-keys
- skip_ansible_lint

- name: generate | set permissions on keys
file:
ansible.builtin.file:
path: "{{ item.path }}"
owner: "{{ item.owner }}"
group: "{{ item.group | default(item.owner) }}"
Expand All @@ -34,7 +36,7 @@
- ssh-keys-generate-permissions

- name: generate | generate public ssh key
openssl_publickey:
community.crypto.openssl_publickey:
path: "{{ item.path }}.pub"
force: "{{ item.force | default(false) }}"
format: "{{ item.format | default('OpenSSH') }}"
Expand Down
6 changes: 3 additions & 3 deletions tasks/known-hosts.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tasks file
---
- name: known-hosts | stat directories
stat:
ansible.builtin.stat:
path: "{{ item }}/"
register: _stat_directories
with_items:
Expand All @@ -11,7 +11,7 @@
- ssh-keys-known-hosts-directories-stat

- name: known-hosts | create directories
file:
ansible.builtin.file:
path: "{{ item.item }}"
state: directory
owner: root
Expand All @@ -24,7 +24,7 @@
- ssh-keys-known-hosts-directories-create

- name: known-hosts | update file
template:
ansible.builtin.template:
src: "{{ ssh_keys_known_hosts_path.lstrip('/') }}.j2"
dest: "{{ ssh_keys_known_hosts_path }}"
owner: root
Expand Down
13 changes: 6 additions & 7 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
# tasks file
---

- import_tasks: generate.yml
- ansible.builtin.import_tasks: generate.yml
tags:
- configuration
- ssh-keys
- ssh-keys-generate

- import_tasks: general.yml
- ansible.builtin.import_tasks: general.yml
tags:
- configuration
- ssh-keys
- ssh-keys-general

- import_tasks: private-keys.yml
- ansible.builtin.import_tasks: private-keys.yml
tags:
- configuration
- ssh-keys
- ssh-keys-private-keys

- import_tasks: public-keys.yml
- ansible.builtin.import_tasks: public-keys.yml
tags:
- configuration
- ssh-keys
- ssh-keys-public-keys

- import_tasks: authorized-keys.yml
- ansible.builtin.import_tasks: authorized-keys.yml
tags:
- configuration
- ssh-keys
- ssh-keys-authorized-keys

- import_tasks: known-hosts.yml
- ansible.builtin.import_tasks: known-hosts.yml
tags:
- configuration
- ssh-keys
Expand Down
4 changes: 2 additions & 2 deletions tasks/private-keys.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tasks file
---
- name: private-keys | add
copy:
ansible.builtin.copy:
src: "{{ item.src }}"
dest: "{{ item.dest_absolute | default('~' + item.owner + '/' + ssh_keys_sshdir + '/' + (item.dest | default(ssh_keys_private_key_filename))) }}"
owner: "{{ item.owner }}"
Expand All @@ -13,7 +13,7 @@
- ssh-keys-private-keys-add

- name: private-keys | remove
file:
ansible.builtin.file:
path: "{{ item.dest_absolute | default('~' + item.owner + '/' + ssh_keys_sshdir + '/' + (item.dest | default(ssh_keys_private_key_filename))) }}"
state: absent
with_items: "{{ ssh_keys_private_keys }}"
Expand Down
4 changes: 2 additions & 2 deletions tasks/public-keys.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tasks file
---
- name: public-keys | add
copy:
ansible.builtin.copy:
src: "{{ item.src }}"
dest: "{{ item.dest_absolute | default('~' + item.owner + '/' + ssh_keys_sshdir + '/' + (item.dest | default(ssh_keys_public_key_filename))) }}"
owner: "{{ item.owner }}"
Expand All @@ -13,7 +13,7 @@
- ssh-keys-public-keys-add

- name: public-keys | remove
file:
ansible.builtin.file:
path: "{{ item.dest_absolute | default('~' + item.owner + '/' + ssh_keys_sshdir + '/' + (item.dest | default(ssh_keys_public_key_filename))) }}"
state: absent
with_items: "{{ ssh_keys_public_keys }}"
Expand Down
12 changes: 6 additions & 6 deletions tests/tasks/post.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# post test file
---
- name: set fact
command: >
ansible.builtin.command: >
echo "{{ ansible_connection }}"
register: _ansible_connection
changed_when: false

- block:

- name: test presence of private key
shell: >
diff {{ ssh_keys_private_keys[0]['src'] }} ~dummy/.ssh/{{ ssh_keys_private_keys[0]['src'] | basename }}
ansible.builtin.shell: >
diff {{ ssh_keys_private_keys[0]['src'] }} ~{{ ssh_keys_owner }}/.ssh/{{ ssh_keys_private_keys[0]['src'] | basename }}
&& (echo 'Private key test: pass' && exit 0)
|| (echo 'Private key test: fail' && exit 1)
args:
Expand All @@ -20,8 +20,8 @@
- skip_ansible_lint

- name: test presence of public key
shell: >
diff {{ ssh_keys_public_keys[0]['src'] }} ~dummy/.ssh/{{ ssh_keys_public_keys[0]['src'] | basename }}
ansible.builtin.shell: >
diff {{ ssh_keys_public_keys[0]['src'] }} ~{{ ssh_keys_owner }}/.ssh/{{ ssh_keys_public_keys[0]['src'] | basename }}
&& (echo 'Public key test: pass' && exit 0)
|| (echo 'Public key test: fail' && exit 1)
args:
Expand All @@ -33,7 +33,7 @@
- skip_ansible_lint

- name: test presence of known hosts
shell: >
ansible.builtin.shell: >
grep -q 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa' /etc/ssh/ssh_known_hosts
&& (echo 'Known hosts test: pass' && exit 0)
|| (echo 'Known hosts test: fail' && exit 1)
Expand Down
8 changes: 4 additions & 4 deletions tests/tasks/pre.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# pre test file
---
- name: install dependencies
apt:
ansible.builtin.apt:
name:
- openssh-client
state: "{{ apt_install_state | default('latest') }}"
update_cache: true
cache_valid_time: "{{ apt_update_cache_valid_time | default(3600) }}"

- name: generate key pair
command: >
ansible.builtin.command: >
ssh-keygen -t rsa -b 2048 -C '' -P '' -f {{ ssh_keys_private_key }} -q
args:
creates: "{{ ssh_keys_private_key }}"
connection: local
become: false

- name: create user (if needed)
user:
name: dummy
ansible.builtin.user:
name: "{{ ssh_keys_owner }}"
6 changes: 3 additions & 3 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
become: true
pre_tasks:
- name: include vars
include_vars: "{{ playbook_dir }}/vars/main.yml"
ansible.builtin.include_vars: "{{ playbook_dir }}/vars/main.yml"
- name: include tasks
include: "{{ playbook_dir }}/tasks/pre.yml"
ansible.builtin.include: "{{ playbook_dir }}/tasks/pre.yml"
roles:
- ../../
post_tasks:
- name: include tasks
include: "{{ playbook_dir }}/tasks/post.yml"
ansible.builtin.include: "{{ playbook_dir }}/tasks/post.yml"
6 changes: 3 additions & 3 deletions tests/vagrant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
become: true
pre_tasks:
- name: include vars
include_vars: "{{ playbook_dir }}/vars/main.yml"
ansible.builtin.include_vars: "{{ playbook_dir }}/vars/main.yml"
- name: include tasks
include: "{{ playbook_dir }}/tasks/pre.yml"
ansible.builtin.include: "{{ playbook_dir }}/tasks/pre.yml"
roles:
- ../../
post_tasks:
- name: include tasks
include: "{{ playbook_dir }}/tasks/post.yml"
ansible.builtin.include: "{{ playbook_dir }}/tasks/post.yml"
7 changes: 4 additions & 3 deletions tests/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# vars file
---
ssh_keys_owner: dummy
ssh_keys_private_keys:
- owner: dummy
- owner: "{{ ssh_keys_owner }}"
src: "{{ ssh_keys_private_key }}"
ssh_keys_public_keys:
- owner: dummy
- owner: "{{ ssh_keys_owner }}"
src: "{{ ssh_keys_private_key }}.pub"
ssh_keys_authorized_keys:
- owner: dummy
- owner: "{{ ssh_keys_owner }}"
src: "{{ ssh_keys_private_key }}.pub"
ssh_keys_known_hosts:
- hostname: github.com
Expand Down

0 comments on commit e7fc3af

Please sign in to comment.