I would do the following:
- create a role (something like 'base') where you (amongst other things), create a suitable user (and sudo rules) for ansible to use
- create or adapt your role for SSH, to manage
sshd_config
(I would tend to recommend you manage the entire file, using a template
, but that is up to you), and disable root logins
- make your SSH role depend on the base role, e.g. using meta.
For the first role (the base one), I tend to use something like:
name: base | local ansible user | create user
user:
name: "{{ local_ansible_user }}"
group: "{{ local_ansible_group }}"
home: "/home/{{ local_ansible_user }}"
state: present
generate_ssh_key: "{{ local_ansible_generate_key }}"
ssh_key_bits: 4096
ssh_key_type: rsa
tags:
- ansible
- local_user
- name: base | local ansible user | provision authorised keys
authorized_key:
user: "{{ local_ansible_user }}"
state: present
key: "{{ item }}"
with_items: "{{ local_ansible_authorised_keys }}"
tags:
- ansible
- authorised_keys
For the SSH config, I would use:
- name: openssh | server | create configuration
template:
src: sshd_config.j2
dest: /etc/ssh/sshd_config
owner: root
group: root
mode: "0640"
validate: "/usr/sbin/sshd -tf %s"
notify:
- openssh | server | restart
tags:
- ssh
- openssh
Ansible's role dependencies are documented here.
You could also just use ordering within your playbook to do this.
I have some ansible stuff on github (from which the above is taken), if you want to see it in context