Browse Source

import ssh

master
Vladimir Smagin 1 year ago
parent
commit
63243cc40d
17 changed files with 155 additions and 11 deletions
  1. +1
    -4
      .gitignore
  2. +4
    -0
      ansible.cfg
  3. +4
    -0
      group_vars/hetzner-nodes.yml
  4. +6
    -0
      group_vars/hetzner.yml
  5. +3
    -0
      host_vars/monitor.yml
  6. +15
    -0
      inventory.ini
  7. +35
    -7
      readme.md
  8. +1
    -0
      roles/master_key/meta/main.yml
  9. +7
    -0
      roles/master_key/tasks/main.yml
  10. +1
    -0
      roles/preinstall/meta/main.yml
  11. +18
    -0
      roles/preinstall/tasks/main.yml
  12. +1
    -0
      roles/ssh_access/meta/main.yml
  13. +21
    -0
      roles/ssh_access/tasks/main.yml
  14. +27
    -0
      secrets/superkey.key
  15. +1
    -0
      secrets/superkey.pub
  16. +3
    -0
      ssh-keys.sh
  17. +7
    -0
      ssh-keys.yml

+ 1
- 4
.gitignore View File

@@ -1,5 +1,2 @@
*.retry
*.log
*.pyc
__PYCACHE__

*.retry

+ 4
- 0
ansible.cfg View File

@@ -0,0 +1,4 @@
[defaults]
log_path=ansible.log
nocows = 1
hash_behaviour = merge

+ 4
- 0
group_vars/hetzner-nodes.yml View File

@@ -0,0 +1,4 @@
access_list:
git: { keypath: "../keys/project-git.pub", username: "root" }
dev1: { keypath: "../keys/developer1.pub", username: "root" }
dev2: { keypath: "../keys/developer2.pub", username: "root" }

+ 6
- 0
group_vars/hetzner.yml View File

@@ -0,0 +1,6 @@
access_list:
admin: { keypath: "../keys/admin.pub", username: "root" }
techguy: { keypath: "../keys/techguy.pub", username: "root" }
interserver: { keypath: "../keys/interserver.pub", username: "root" }
secret_list:
interserver: { keypath: "../keys/interserver.pem", username: "root" }

+ 3
- 0
host_vars/monitor.yml View File

@@ -0,0 +1,3 @@
access_list:
admin: { keypath: "../keys/admin.pub", username: "root" }
techguy: { keypath: "../keys/techguy.pub", username: "root" }

+ 15
- 0
inventory.ini View File

@@ -0,0 +1,15 @@
[monitoring]
monitor ansible_host=44.165.225.144 ansible_user=root ansible_ssh_private_key_file="secrets/superkey.pem"

[hetzner:children]
hetzner-balancers
hetzner-nodes

[hetzner-nodes]
hetzner-node0 ansible_host=145.251.216.112 ansible_user=root ansible_ssh_private_key_file="secrets/superkey.pem"
hetzner-node1 ansible_host=154.64.4.185 ansible_user=root ansible_ssh_private_key_file="secrets/superkey.pem"
hetzner-node2 ansible_host=168.251.172.244 ansible_user=root ansible_ssh_private_key_file="secrets/superkey.pem"

[hetzner-balancers]
hetzner-balancer0 ansible_host=145.251.216.154 ansible_user=root ansible_ssh_private_key_file="secrets/superkey.pem"
hetzner-balancer1 ansible_host=78.46.246.78 ansible_user=root ansible_ssh_private_key_file="secrets/superkey.pem"

+ 35
- 7
readme.md View File

@@ -1,13 +1,41 @@
# My useful ansible playbooks
# Ansible centralized keys management

Security management:
Dicts in parent group merges with dicts in child groups.

- [Centralized SSH keys management](https://git.blindage.org/21h/ansible-library/src/branch/centralized-keys)
Warning! Playbook will delete all public keys in root's ```.ssh/authorized_keys``` file and setup own **super** key, only after this public keys in group_vars and host_vars will be added into assigned users ```.ssh/authorized_keys``` files.

System and monitoring:
**Security issue!** You need to generate your own super key! Run ```ssh-keygen -f superkey``` and replace keys in ```secret/``` directory.

- [Create systemd and upstart scripts](https://git.blindage.org/21h/ansible-library/src/branch/sys-systemd-and-upstart)
Infrastructure:

Web deployment:
```
--[ hetzner ]--\
|- hetzner-balancers
\- hetzner-nodes
```

- [PHP 7.1 FPM + Nginx](https://git.blindage.org/21h/ansible-library/src/branch/web-nginx-php-fpm7.1)
All servers will be available to the administrator and assistant, servers must have own internal key for files copying. Configuring parent ```hetzner```group:

```
access_list:
admin: { keypath: "../keys/admin.pub", username: "root" }
techguy: { keypath: "../keys/techguy.pub", username: "root" }
interserver: { keypath: "../keys/interserver.pub", username: "root" }

secret_list:
interserver: { keypath: "../keys/interserver.pem", username: "root" }
```

You want to make additional access to all nodes for developer guy. Configuring child ```hetzner-nodes``` group:

```
access_list:
developer: { keypath: "../keys/developer.pub", username: "dev" }
```

No need to make additional config for ```hetzner-balancers``` because access list will be inherited, only admins accessible.

---
Copyright by Vladimir Smagin, 2018
http://blindage.org
21h@blindage.org

+ 1
- 0
roles/master_key/meta/main.yml View File

@@ -0,0 +1 @@
allow_duplicates: true

+ 7
- 0
roles/master_key/tasks/main.yml View File

@@ -0,0 +1,7 @@
---
- name: Set primary ansible key and remove others
authorized_key:
user: root
state: present
exclusive: True
key: "{{ lookup('file', 'secrets/superkey.pub') }}"

+ 1
- 0
roles/preinstall/meta/main.yml View File

@@ -0,0 +1 @@
allow_duplicates: true

+ 18
- 0
roles/preinstall/tasks/main.yml View File

@@ -0,0 +1,18 @@
---
- name: Minimum packages for Debian-like
apt: name="{{ item }}" state=present update_cache=yes
become: yes
with_items:
- mc
- htop
when: (ansible_distribution == 'Ubuntu' or ansible_distribution == 'Debian') and preinstall is defined

- name: Minimum packages for RedHat-like
become: yes
yum: name="{{ item }}" state=present update_cache=yes
with_items:
- mc
- htop
when: (ansible_distribution == 'Red Hat' or ansible_distribution == 'Centos') and preinstall is defined



+ 1
- 0
roles/ssh_access/meta/main.yml View File

@@ -0,0 +1 @@
allow_duplicates: true

+ 21
- 0
roles/ssh_access/tasks/main.yml View File

@@ -0,0 +1,21 @@
---
#- debug:
# msg: "{{ access_list }}"
- name: Set public keys
become: true
become_user: "{{ item.value.username }}"
authorized_key: user="{{ item.value.username }}" key="{{ lookup('file', item.value.keypath) }}" state=present
with_dict: "{{ access_list }}"
when: access_list != None and access_list is defined

#- debug:
# msg: "{{ secret_list }}"

- name: Upload secret keys
become: true
become_user: "{{ item.value.username }}"
copy: src="{{ item.value.keypath }}" dest="~/.ssh/{{ item.value.keypath | basename }}" mode=0600
with_dict: "{{ secret_list }}"
when: secret_list != None and secret_list is defined


+ 27
- 0
secrets/superkey.key View File

@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAwNv29GRDfroj6Ue5iPsj/E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5TL12mNtJ1EkSTW6suAJkOpnV43ru1VjTm
94AKuVciL1V/KDlWnQ3yMZLoNaftwB516W2NUPjBTMDRIhOVUVj3v72hxCljTg+y
fB2IvBC3HsB68PVEEthxpAcCgYA+3N39xFojBGvWX1RhkcJHwgwH3pAh03dNGXlI
H70R7VIQ7rwCIJgDygllGbzqHHlb4vFuapgzvUnSfaWYw21U8Sv0+tCL4dY1LhCZ
FAA7q5bDIwiGC1JyzAONpQuRnwmNLMln4xCreAjMOl2IP5cOKn6LleOGcilK/+/6
TJVs8wKBgQDSOzTch2lQViWQhSFO2nnQ5Os7nLhQGhWLQP+L6JJiTIeAv0oITyQC
IVOzsysepQYnm/bSHDXRHpzYR/Cq2FJIIPKvBIHuh60zqhpfpG97+fCibRFfWcoe
DFR+2w5mcReEHjwAT5dVBfYVlLb75Zmu7P0/C4KG6DGtRNxEGSjUSw==
-----END RSA PRIVATE KEY-----

+ 1
- 0
secrets/superkey.pub View File

@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA2/b0ZEN+uiPpR7mI+yP8TmdftBZE/N8ciPykyazf5hj+u3p6bTvsBcUZAhOvzvhjm2eNLuOg7Uuc5TvUP9jLzS3LiSovnpTbQ4EYusXG/+zAmB9DB5K/2cWSP6wQ42q7+QzsCKW7iw6RwDMf9oDy0tWkDILnF/7BiG1fe3BSRtI+CkY3qFX9No0y3buahbA4y6LQeboRiuI9gEPtzvAfCAvCSSyr5IsscOomqJasFJGfAK8oEAikzKrCGMsgKTsvK4Bl1cX4t0leyxcueenCXUeymRG39CFvOdGrIup0Pql9VG/JFqVnErvG/xqRiULPNaAIcZvD3wp7FQEtzDyh demo key. do not use it!

+ 3
- 0
ssh-keys.sh View File

@@ -0,0 +1,3 @@
#!/bin/bash

ansible-playbook ssh-keys.yml -i inventory.ini

+ 7
- 0
ssh-keys.yml View File

@@ -0,0 +1,7 @@
---
- hosts: all
gather_facts: True
roles:
- master_key
- preinstall
- ssh_access

Loading…
Cancel
Save