]> git.phdru.name Git - ansible.git/commitdiff
Feat(debian-phd): Test if the user was already created
authorOleg Broytman <phd@phdru.name>
Sun, 14 Jul 2019 05:17:10 +0000 (08:17 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 16 Jul 2019 18:28:41 +0000 (21:28 +0300)
Actually test if the home was already initialized.

playbooks/roles/debian-phd/tasks/create.yml [new file with mode: 0644]
playbooks/roles/debian-phd/tasks/main.yml
playbooks/roles/debian-phd/tasks/setup-ssh.yml [new file with mode: 0644]
playbooks/roles/debian-sudo/tasks/main.yml

diff --git a/playbooks/roles/debian-phd/tasks/create.yml b/playbooks/roles/debian-phd/tasks/create.yml
new file mode 100644 (file)
index 0000000..dbd4209
--- /dev/null
@@ -0,0 +1,18 @@
+- name: Create system groups
+  become: true
+  group:
+    name: "{{ item }}"
+    system: true
+  loop: "{{ system_groups.split(',') }}"
+
+- name: Create group phd
+  become: true
+  group:
+    name: phd
+
+- name: Add user phd
+  become: true
+  user:
+    name: phd
+    group: phd
+    groups: "{{ system_groups }}"
index cea3f9fa3c89a6553e14ca81d64920fd5b25d16c..52d15906070d741725539d00f91943a7fb4f573a 100644 (file)
@@ -1,30 +1,16 @@
-- name: Create system groups
-  become: true
-  group:
-    name: "{{ item }}"
-    system: true
-  loop: "{{ system_groups.split(',') }}"
-- name: Create group phd
-  become: true
-  group:
-    name: phd
-- name: Add user phd
-  become: true
-  user:
-    name: phd
-    group: phd
-    groups: "{{ system_groups }}"
-- name: Create folder ~phd/.ssh
-  file:
-    path: ~phd/.ssh
-    state: directory
-    owner: phd
-    group: phd
-    mode: 0700
-- name: Upload SSH key
-  copy:
-    src: ~/.ssh/phd@nb3.pub
-    dest: ~phd/.ssh/authorized_keys
-    owner: phd
-    group: phd
-    mode: 0600
+- name: Test if user phd already exists
+  command: test -f ~/.profile -a -f ~/.shellrc
+  ignore_errors: true
+  register: phd_exist
+
+- debug:
+    msg: "User phd has already been created"
+  when: phd_exist.rc == 0
+
+- name: "Create and setup user phd"
+  block:
+    - name: "Create user phd"
+      import_tasks: create.yml
+    - name: "Setup SSH: copy SSH public key"
+      import_tasks: setup-ssh.yml
+  when: phd_exist.rc != 0
diff --git a/playbooks/roles/debian-phd/tasks/setup-ssh.yml b/playbooks/roles/debian-phd/tasks/setup-ssh.yml
new file mode 100644 (file)
index 0000000..d6157dc
--- /dev/null
@@ -0,0 +1,15 @@
+- name: Create folder ~phd/.ssh
+  file:
+    path: ~phd/.ssh
+    state: directory
+    owner: phd
+    group: phd
+    mode: 0700
+
+- name: Upload SSH key
+  copy:
+    src: ~/.ssh/phd@nb3.pub
+    dest: ~phd/.ssh/authorized_keys
+    owner: phd
+    group: phd
+    mode: 0600
index 05544b1a7c1d7d528bfdec4e55c7100da6617326..242a373244d2595d310e4d479d70ea1128bc907d 100644 (file)
@@ -5,12 +5,14 @@
     name: sudo
     state: latest
     update_cache: yes
+
 - name: Add user phd to group sudo
   become: true
   user:
     name: phd
     append: yes
     groups: sudo
+
 - name: Allow passwordless operations
   become: true
   copy: