This is a step-by-step of what I'm trying to accomplish via Ansible:
- SSH as root
- Install python(not present in ubuntu), and other basic packages.
- Create new
deploy
user and config/etc/ssh/sshd_config
such thatPasswordAuhentication no
andPermitRootLogin no
. - Restart ssh service.
Later on I am updating my playbook with new tasks, roles, etc. So I want to re-run the playbook against the same server(which has root
access blocked), just this time accessing as the newly created user.
I'm expectedly being returned a Permission denied
access, since Ansible is attempting to access as root.
Question:
- How can I just do this first pass as root and then jump over the root tasks(pre_tasks in this case) on the next playbook runs?
One option is to just make it into two separate playbooks: one for provisioninig, one for the rest.
# playbook.yml
---
- name: Prepare server
hosts: webserver
gather_facts: False
pre_tasks:
- name: Install python for Ansible
remote_user: root
raw: type /usr/bin/python || (apt -y update && apt install -y python)
- name: Create user
remote_user: root
include_role:
name: deploy-user
roles:
# Future roles here
#roles/deploy-user/tasks/main.yml
---
- group:
name: deploy
state: present
- name: Create Deploy user
user:
name={{ deploy_user }}
comment="Deploy User"
groups="sudo,deploy"
password="{{ deploy_password | password_hash('sha512') }}"
shell=/bin/bash
update_password=on_create
- name: Set authorized key took from files
authorized_key:
user: "{{ deploy_user }}"
state: present
key: "{{ lookup('file', item) }}"
with_items:
- '{{ ssh_authorized_keys }}'
- name: Disallow password authentication
lineinfile:
dest: /etc/ssh/sshd_config
regexp: "^PasswordAuthentication"
line: "PasswordAuthentication no"
state: present
- name: Disallow root SSH access
lineinfile:
dest: /etc/ssh/sshd_config
regexp: "^PermitRootLogin"
line: "PermitRootLogin no"
state: present
- name: restart-sshd
remote_user: root
service: name=ssh state=restarted