CSchantz Wiki

Inventory onboard bootstraping

Linux Server Bootstrap onboarding playbook

need files in folder

Example inventory file

[Main-DNS-Server]
192.168.1.1

[Zabbix-Monitoring]
192.168.1.5

Command to run playbook

ansible-playbook -i inventory onboarding.yml -u existing-admin-user --ask-pass --ask-become-pass

Full playbook

---
# File: onboarding.yml
- name: Bootstrap Ansible Management User
  hosts: all
  become: yes
  vars:
    ansible_user_name: ansible-management-user
    allowed_ip: "IP of Anisble server"
    ssh_public_key: "enter full public key here"

  tasks:
    - name: Create ansible management user
      user:
        name: "{{ ansible_user_name }}"
        state: present
        groups: wheel
        append: yes
        create_home: yes
        shell: /bin/bash

    - name: Create .ssh directory
      file:
        path: "/home/{{ ansible_user_name }}/.ssh"
        state: directory
        mode: '0700'
        owner: "{{ ansible_user_name }}"
        group: "{{ ansible_user_name }}"

    - name: Add authorized key
      authorized_key:
        user: "{{ ansible_user_name }}"
        state: present
        key: "{{ ssh_public_key }}"

    - name: Ensure sudo access without password
      lineinfile:
        path: /etc/sudoers.d/{{ ansible_user_name }}
        state: present
        regexp: "^{{ ansible_user_name }}"
        line: "{{ ansible_user_name }} ALL=(ALL) NOPASSWD: ALL"
        validate: 'visudo -cf %s'
        create: yes
        mode: '0440'

    - name: Create SSH configuration directory if it doesn't exist
      file:
        path: /etc/ssh/sshd_config.d
        state: directory
        mode: '0755'

    - name: Configure SSH restrictions for ansible user
      copy:
        dest: /etc/ssh/sshd_config.d/{{ ansible_user_name }}.conf
        content: |
          Match User {{ ansible_user_name }}
              PasswordAuthentication no
              PubkeyAuthentication yes

          Allowusers {{ ansible_user_name }}@{{ allowed_ip }}
        mode: '0644'
      notify: Restart sshd

    - name: Ensure main sshd_config includes our configs
      lineinfile:
        path: /etc/ssh/sshd_config
        line: "Include /etc/ssh/sshd_config.d/*.conf"
        state: present
      notify: Restart sshd

    - name: Verify ansible user can connect
      become: yes
      become_user: "{{ ansible_user_name }}"
      command: whoami
      register: verify_result
      changed_when: false

    - name: Display verification result
      debug:
        msg: "User verification successful: {{ verify_result.stdout }}"

  handlers:
    - name: Restart sshd
      service:
        name: sshd
        state: restarted