name: Setup Ansible Environment description: Prepare the environment for A13labs CI/CD pipelines inputs: vault-master-password: description: "The master password for the vault" required: true ansible-user: description: "The user to use for Ansible" required: true ssh-auth-socket: description: "The SSH_AUTH_SOCK environment variable" default: "/tmp/ssh_agent.sock" host-group: description: "The host group to setup the Ansible environment" default: "all" outputs: ssh-auth-sock: description: "The SSH_AUTH_SOCK environment variable" value: ${{ steps.start-ssh-agent.outputs.ssh-auth-sock }} hosts: description: "The list of hosts in the host group" value: ${{ steps.read-hosts.outputs.hosts }} runs: using: "composite" steps: - name: Install Python and required packages shell: bash run: | sudo apt-get update sudo apt-get install -y python3-pip python3 -m pip install -r requirements.txt - name: Prepare SSH key shell: bash env: VAULT_MASTER_PASSWORD: ${{ inputs.vault-master-password }} run: | sectool ssh unlock ansible mkdir -p ~/.ssh chmod 700 ~/.ssh cp ssh-keys/ansible/id_ecdsa ~/.ssh/id_ecdsa chmod 600 ~/.ssh/id_ecdsa - name: Read hosts from inventory id: read-hosts shell: bash env: HOST_GROUP: ${{ inputs.host-group }} run: | ansible-inventory -i inventory/hosts --list --yaml $HOST_GROUP | grep -oP '(\w+\.){2}\w+' | sort -u > /tmp/ansible_hosts echo "hosts=$(cat /tmp/ansible_hosts)" >> $GITHUB_OUTPUT - name: Read SSH host keys from remote hosts shell: bash run: | mkdir -p ~/.ssh for HOST in $(cat /tmp/ansible_hosts); do ssh-keyscan $HOST >> ~/.ssh/known_hosts done chmod 600 ~/.ssh/known_hosts - name: Enable SSH host key algorithms shell: bash run: | echo "Host *" >> ~/.ssh/config echo " HostKeyAlgorithms +sk-ecdsa-sha2-nistp256@openssh.com, sk-ssh-ed25519@openssh.com" chmod 600 ~/.ssh/config - name: Start SSH agent and add SSH key id: start-ssh-agent shell: bash env: VAULT_MASTER_PASSWORD: ${{ inputs.vault-master-password }} SSH_AUTH_SOCK: ${{ inputs.ssh-auth-socket }} run: | ssh-agent -a $SSH_AUTH_SOCK > /dev/null echo 'sectool vault get SSH_PASSWORD' > ~/.ssh_askpass && chmod +x ~/.ssh_askpass DISPLAY=None SSH_ASKPASS=~/.ssh_askpass ssh-add ~/.ssh/id_ecdsa < /dev/null rm ~/.ssh_askpass echo "ssh-auth-sock=$SSH_AUTH_SOCK" >> $GITHUB_OUTPUT - name: Check SSH Connection to remote hosts shell: bash env: ANSIBLE_USER: ${{ inputs.ansible-user }} SSH_AUTH_SOCK: ${{ inputs.ssh-auth-socket }} run: | for HOST in $(cat /tmp/ansible_hosts); do ssh $ANSIBLE_USER@$HOST echo "SSH connection to $HOST successful" done