LLI Wiki — My Digital Knowledge Base

Ansible: bootstrap a service account (user, sudoers, SSH key) via a bastion

Create a dedicated Ansible account with ad-hoc commands (user, copy, authorized_key), then test with ping and an SSH bastion.

Procedure to bootstrap a dedicated automation account for Ansible on a Linux host, using ad-hoc Ansible commands run with a one-time administrative account, then verify access directly and through a bastion. Host names, inventory and accounts are generic examples.

Principle

For each target server, three ad-hoc commands create the Ansible account, give it sudo rights and install its public SSH key. They run as an existing privileged account (admin_user) with --become, asking for the SSH and sudo passwords interactively.

export server=linuxhost01

1. Create the user

The password must be given as a SHA-512 crypt hash ($6$...), never in clear. Generate it beforehand, for example with mkpasswd --method=sha-512:

ansible -i env/dev/inventory.yml -m user \
  -a 'name=svc_ansible password=<PASSWORD_HASH>' \
  --user admin_user --become --ask-pass --ask-become-pass ${server}

2. Deploy the sudoers file

ansible -i env/dev/inventory.yml -m ansible.builtin.copy \
  -a "src=/home/svc_ansible/deploy/etc/sudoers.d/11-svc_ansible dest=/etc/sudoers.d/10-svc_ansible mode=644 owner=root group=root" \
  --user admin_user --become --ask-pass --ask-become-pass ${server}

3. Install the public SSH key

ansible -i env/dev/inventory.yml -m authorized_key \
  -a 'user=svc_ansible state=present key="{{lookup("file", "/home/svc_ansible/keys/svc_ansible/id_ed25519.pub") }}"' \
  --user admin_user --become --ask-pass --ask-become-pass ${server}

Verify

Test the connection with the ping module:

ansible -i env/dev/inventory.yml -m ping linuxhost01

Test SSH through a bastion (jump host) with ProxyCommand:

ssh svc_ansible@linuxhost01 -oProxyCommand="ssh -i ~/.ssh/id_rsa bastion01 -W %h:%p" -i ~/.ssh/id_ed25519

Run a playbook through the bastion

ansible-playbook -i env/dev/inventory.yml playbooks/deployments/playbook_deploy_ansible_viaBastion.yml -vvv

Notes

Mastodon LLIMastodon SocialFosstodon