-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_user.yml
98 lines (85 loc) · 3.13 KB
/
create_user.yml
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
---
- name: Setup non-root User
gather_facts: no
hosts: undercloud
any_errors_fatal: true
# Ensure root access for this
become: yes
become_user: root
vars:
local_undercloud_pubkey: "{{ inventory_dir }}/id_rsa_undercloud.pub"
default_privatekey: "~/.ssh/id_rsa"
default_pubkey: "{{ default_privatekey }}.pub"
tasks:
- name: sudoers no tty
lineinfile:
dest: /etc/sudoers
state: absent
regexp: 'Defaults\s+requiretty'
- name: create default user
user:
name: "{{ install.user.name }}"
state: present
password: "{{ install.user.password | password_hash('sha512') }}"
- name: add user to sudoers
lineinfile:
dest: "/etc/sudoers"
line: "{{ install.user.name }} ALL=(root) NOPASSWD:ALL"
- name: create .ssh directory for non-root user
file:
path: "{{ default_privatekey | dirname }}"
state: directory
register: user_ssh_dir
become_user: "{{ install.user.name }}"
become: yes
- block:
- name: inject local private key to undercloud
copy:
src: "{{ ansible_ssh_private_key_file }}"
# Workaround due to https://github.com/ansible/ansible/issues/21145
dest: "{{ user_ssh_dir.path }}/{{default_privatekey | basename }}"
mode: 0600
owner: "{{ install.user.name }}"
group: "{{ install.user.name }}"
force: yes
register: key_injection
- name: Retrieve public key from private key
shell: "ssh-keygen -y -f {{ default_privatekey }} > {{ default_pubkey }}"
become_user: "{{ install.user.name }}"
become: yes
- name: Retrieve public key from private key to localhost
shell: "ssh-keygen -y -f {{ ansible_ssh_private_key_file }} > {{ local_undercloud_pubkey }}"
delegate_to: localhost
become: no
rescue:
- name: inject local SSH keys to undercloud
copy:
src: "{{ item }}"
dest: "{{ item }}"
mode: 0600
force: yes
with_items:
- "{{ default_privatekey }}"
- "{{ default_pubkey }}"
become_user: "{{ install.user.name }}"
become: yes
- set_fact:
src_publickey: "{{ (key_injection|success) | ternary(local_undercloud_pubkey, local_pubkey) }}"
- set_fact:
publickey: "{{ lookup('file', src_publickey) }}"
- name: set up authorized_keys for non root user
authorized_key:
user: "{{ install.user.name }}"
key: "{{ publickey }}"
- name: Update hosts user
add_host:
name: "{{ hostvars[item]['inventory_hostname'] }}"
ansible_user: "{{ install.user.name }}"
with_items: "{{ groups['undercloud'] }}"
- name: update inventory file
hosts: localhost
gather_facts: no
tags: always
roles:
- role: inventory-update
inventory_file_name: 'hosts-install'