From 06017e09f5633bb116cf59e8d36bde51602fb00e Mon Sep 17 00:00:00 2001 From: Thomas Nyambati Date: Wed, 15 Feb 2017 22:41:30 +0300 Subject: [PATCH] ft(ssh-rotation): Rotate ssh keys - add task to generate new keys - add task to rotate new keys - add new key to inventory --- .travis.yml | 29 +++++++++++++++++++++++++++++ README.md | 40 ++++++++++++++++++++++++++++++++++++++-- defaults/main.yml | 6 ++++++ meta/main.yml | 22 ++++++++++++++++++++++ tasks/main.yml | 26 ++++++++++++++++++++++++++ tests/inventory | 1 + tests/test.yml | 5 +++++ vars/main.yml | 2 ++ 8 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 .travis.yml create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..36bbf62 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,29 @@ +--- +language: python +python: "2.7" + +# Use the new container infrastructure +sudo: false + +# Install ansible +addons: + apt: + packages: + - python-pip + +install: + # Install ansible + - pip install ansible + + # Check ansible version + - ansible --version + + # Create ansible.cfg with correct roles_path + - printf '[defaults]\nroles_path=../' >ansible.cfg + +script: + # Basic role syntax check + - ansible-playbook tests/test.yml -i tests/inventory --syntax-check + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ \ No newline at end of file diff --git a/README.md b/README.md index 118ebba..225dd44 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,38 @@ -# ssh-key-rotation -An ansible role that enables you to rotate ssh keys to your servers +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..05363a1 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,6 @@ +--- +# defaugalts file for ssh-key-rotation +is_exclusive: no +should_manage_dir: no +authorized_keys_path: '{{ ansible_env.HOME }}/.ssh/authorized_keys' +passphrase: 83g!8bfu5M5yy84x diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..0664b94 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,22 @@ +galaxy_info: + author: Thomas Nyambati + description: Ansible role that enables you to rotate ssh keys on your remote servers + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Some suggested licenses: + # - BSD (default) + # - MIT + # - GPLv2 + # - GPLv3 + # - Apache + # - CC-BY + license: license (GPLv2, CC-BY, etc) + + min_ansible_version: 2.2 + galaxy_tags: [] +dependencies: [] + diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..27286b2 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,26 @@ +--- +# tasks file for ssh-key-rotation +- name: Generate New ssh Keys + command: + ssh-keygen -t rsa + -N "{{ passphrase }}" -q + -f {{ lookup('env','HOME') + private_key_path }} -y + when: inventory_hostname == play_hosts[0] + delegate_to: localhost + +- name: Set Authorized key(s) to the authorized keys file + become: yes + become_user: root + authorized_key: + exclusive: '{{ is_exclusive }}' + user: '{{ host_user }}' + state: present + path: '{{ authorized_keys_path }}' + manage_dir: '{{ should_manage_dir }}' + key: "{{ lookup('file', lookup('env','HOME') + public_key_path)}}" + +- debug: + msg: "{{ play_hosts }}" + +- name: Test if the new ssh key is allowed to make connections + set_fact: ansible_private_ssh_key={{ lookup('file', lookup('env','HOME') + private_key_path)}} diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..d18580b --- /dev/null +++ b/tests/inventory @@ -0,0 +1 @@ +localhost \ No newline at end of file diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..fca18f5 --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - ssh-key-rotation \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..8566aa6 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for ssh-key-rotation