From d93a94543aca4cce397db250b7dd561bf4729aa4 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 14 Jul 2019 08:17:10 +0300 Subject: [PATCH] Feat(debian-phd): Test if the user was already created Actually test if the home was already initialized. --- playbooks/roles/debian-phd/tasks/create.yml | 18 ++++++++ playbooks/roles/debian-phd/tasks/main.yml | 46 +++++++------------ .../roles/debian-phd/tasks/setup-ssh.yml | 15 ++++++ playbooks/roles/debian-sudo/tasks/main.yml | 2 + 4 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 playbooks/roles/debian-phd/tasks/create.yml create mode 100644 playbooks/roles/debian-phd/tasks/setup-ssh.yml diff --git a/playbooks/roles/debian-phd/tasks/create.yml b/playbooks/roles/debian-phd/tasks/create.yml new file mode 100644 index 0000000..dbd4209 --- /dev/null +++ b/playbooks/roles/debian-phd/tasks/create.yml @@ -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 }}" diff --git a/playbooks/roles/debian-phd/tasks/main.yml b/playbooks/roles/debian-phd/tasks/main.yml index cea3f9f..52d1590 100644 --- a/playbooks/roles/debian-phd/tasks/main.yml +++ b/playbooks/roles/debian-phd/tasks/main.yml @@ -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 index 0000000..d6157dc --- /dev/null +++ b/playbooks/roles/debian-phd/tasks/setup-ssh.yml @@ -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 diff --git a/playbooks/roles/debian-sudo/tasks/main.yml b/playbooks/roles/debian-sudo/tasks/main.yml index 05544b1..242a373 100644 --- a/playbooks/roles/debian-sudo/tasks/main.yml +++ b/playbooks/roles/debian-sudo/tasks/main.yml @@ -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: -- 2.39.5