|
| 1 | +--- |
| 2 | +- name: Ensure service account user {{ account.account_name }} exists |
| 3 | + ansible.builtin.user: |
| 4 | + name: "{{ account.account_name }}" |
| 5 | + comment: "{{ account.description | default('') }}" |
| 6 | + shell: "/bin/bash" |
| 7 | + create_home: true |
| 8 | + state: present |
| 9 | + |
| 10 | +- name: Ensure .ssh directory for {{ account.account_name }} exists with correct permissions |
| 11 | + ansible.builtin.file: |
| 12 | + path: "/home/{{ account.account_name }}/.ssh" |
| 13 | + state: directory |
| 14 | + owner: "{{ account.account_name }}" |
| 15 | + group: "{{ account.account_name }}" |
| 16 | + mode: "0700" |
| 17 | + |
| 18 | +- name: Build connections_from options for authorized_key for {{ account.account_name }} |
| 19 | + ansible.builtin.set_fact: |
| 20 | + account_connections_from_options: >- |
| 21 | + {{ |
| 22 | + ( |
| 23 | + 'from="' ~ ( |
| 24 | + account.allow_connections_from |
| 25 | + if account.allow_connections_from is string |
| 26 | + else (account.allow_connections_from | default([])) | join(',') |
| 27 | + ) ~ '"' |
| 28 | + ) |
| 29 | + if account.allow_connections_from is defined and ( |
| 30 | + (account.allow_connections_from is string and account.allow_connections_from | length > 0) or |
| 31 | + (account.allow_connections_from is iterable and (account.allow_connections_from | length) > 0) |
| 32 | + ) |
| 33 | + else omit |
| 34 | + }} |
| 35 | + when: |
| 36 | + - account.public_ssh_key is defined |
| 37 | + |
| 38 | +- name: "Install GitHub SSH public keys when public_ssh_key starts with `gh:` for {{ account.account_name }}" |
| 39 | + vars: |
| 40 | + gh_user: "{{ (account.public_ssh_key | string).split(':', 1)[1] }}" |
| 41 | + ansible.posix.authorized_key: |
| 42 | + user: "{{ account.account_name }}" |
| 43 | + key: "{{ lookup('community.general.github_keys', gh_user) }}" |
| 44 | + key_options: "{{ account_connections_from_options | default(omit) }}" |
| 45 | + manage_dir: false |
| 46 | + path: "/home/{{ account.account_name }}/.ssh/authorized_keys" |
| 47 | + when: |
| 48 | + - account.public_ssh_key is defined |
| 49 | + - (account.public_ssh_key | string).startswith('gh:') |
| 50 | + |
| 51 | +- name: Install provided SSH public keys for {{ account.account_name }} (non-GitHub keys) |
| 52 | + ansible.posix.authorized_key: |
| 53 | + user: "{{ account.account_name }}" |
| 54 | + key: "{{ account.public_ssh_key }}" |
| 55 | + key_options: "{{ account_connections_from_options | default(omit) }}" |
| 56 | + manage_dir: false |
| 57 | + path: "/home/{{ account.account_name }}/.ssh/authorized_keys" |
| 58 | + when: |
| 59 | + - account.public_ssh_key is defined |
| 60 | + - not (account.public_ssh_key | string).startswith('gh:') |
| 61 | + |
| 62 | +- name: Check if authorized_keys exists for {{ account.account_name }} |
| 63 | + ansible.builtin.stat: |
| 64 | + path: "/home/{{ account.account_name }}/.ssh/authorized_keys" |
| 65 | + register: account_authorized_keys_stat |
| 66 | + |
| 67 | +- name: Ensure authorized_keys for {{ account.account_name }} has correct permissions |
| 68 | + ansible.builtin.file: |
| 69 | + path: "/home/{{ account.account_name }}/.ssh/authorized_keys" |
| 70 | + state: file |
| 71 | + owner: "{{ account.account_name }}" |
| 72 | + group: "{{ account.account_name }}" |
| 73 | + mode: "0600" |
| 74 | + when: account_authorized_keys_stat.stat.exists | default(false) |
| 75 | + |
| 76 | +- name: Configure sudoers for {{ account.account_name }} with sudo_permissions |
| 77 | + ansible.builtin.copy: |
| 78 | + dest: "/etc/sudoers.d/{{ account.account_name }}" |
| 79 | + owner: root |
| 80 | + group: root |
| 81 | + mode: "0440" |
| 82 | + content: "{{ account.account_name }} ALL=(ALL) NOPASSWD: {{ (account.sudo_permissions | list) | join(',') }}\n" |
| 83 | + validate: "visudo -cf %s" |
| 84 | + when: |
| 85 | + - account.sudo_permissions is defined |
| 86 | + - (account.sudo_permissions | length) > 0 |
0 commit comments