aboutsummaryrefslogtreecommitdiffstats
path: root/roles/bootstrap/tasks/main.yml
blob: 806902d753f3c742fb2aa0af607ca497245fb982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
- debug: var=ansible_play_hosts
- debug: var=deploy_user
- debug: var=ansible_ssh_host
- debug: var=ansible_ssh_port
- debug: var=ansible_ssh_private_key_file

- name: group - check deployment group
  command: pw groupshow "{{ deploy_user }}"
  register: pw_cmd
  ignore_errors: true
  changed_when: false

- name: group - create deployment group
  command: pw groupadd "{{ deploy_user }}" -g 999
  when: pw_cmd.rc != 0

- name: user - check deployment user
  command: pw usershow "{{ deploy_user }}"
  register: pw_cmd
  ignore_errors: true
  changed_when: false

- name: user - create deployment user
  command: >
    pw useradd "{{ deploy_user }}"
    -u 999 -g "{{ deploy_user }}"
    -m -d "/var/{{ deploy_user }}"
    -c "Ansible Deployment"
  when: pw_cmd.rc != 0

- name: SSH - authorized_keys for the deployment user
  authorized_key:
    user: "{{ deploy_user }}"
    state: present
    key: "{{ lookup('file', item+'.pub') }}"
  with_items:
    - "{{ ansible_ssh_private_key_file }}"

- name: sudo - install package
  pkgng:
    name: sudo
    state: present

- name: sudo - no password for the deployment user
  template:
    src: sudoers.d_ansible.j2
    dest: /usr/local/etc/sudoers.d/ansible
    mode: 0440
    validate: "visudo -cf %s"

- name: SSH - disable password auth for the deployment user
  blockinfile:
    path: /etc/ssh/sshd_config
    marker: "# {mark} ANSIBLE MANAGED - ansible"
    block: |
      Match User {{ deploy_user }}
          PasswordAuthentication no
    validate: "sshd -t -f %s"
  notify: restart-sshd

- name: SSH - disable password login
  lineinfile:
    path: /etc/ssh/sshd_config
    regexp: "^#?PasswordAuthentication"
    line: "PasswordAuthentication no"
    validate: "sshd -t -f %s"
  notify: restart-sshd

- name: SSH - disable empty password login
  lineinfile:
    path: /etc/ssh/sshd_config
    regexp: "^#?PermitEmptyPasswords"
    line: "PermitEmptyPasswords no"
    validate: "sshd -t -f %s"
  notify: restart-sshd

- name: SSH - disable root login
  lineinfile:
    path: /etc/ssh/sshd_config
    regexp: "^#?PermitRootLogin"
    line: "PermitRootLogin no"
    validate: "sshd -t -f %s"
  notify: restart-sshd