Skip to content

Update test_ansible.yml #35

Update test_ansible.yml

Update test_ansible.yml #35

Workflow file for this run

name: Test Ansible Playbook Setup
on:
push:
branches:
- fb_ansible_test
jobs:
ansible:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up SSH agent and add both keys
uses: webfactory/[email protected]
with:
ssh-private-key: |
${{ secrets.SSH_PRIVATE_KEY_1 }}
- name: Add keys to SSH config
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY_1 }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
echo "
Host gateway_host
HostName ${{ secrets.GATEWAY_HOST }}
User ${{ secrets.GATEWAY_USER }}
IdentityFile ~/.ssh/id_rsa
Host target_host
HostName ${{ secrets.TARGET_HOST }}
User ${{ secrets.TARGET_USER }}
IdentityFile ~/.ssh/id_rsa
ProxyJump ${{ secrets.GATEWAY_USER }}@${{ secrets.GATEWAY_HOST }}
" >> ~/.ssh/config
- name: Check SSH agent keys
run: ssh-add -l
- name: Add gateway host to known hosts
run: ssh-keyscan -v -H ${{ secrets.GATEWAY_HOST }} >> ~/.ssh/known_hosts
- name: Add target host to known hosts
run: ssh-keyscan -v -H ${{ secrets.TARGET_HOST }} >> ~/.ssh/known_hosts
- name: Establish SSH tunnel
run: |
ssh -v -o StrictHostKeyChecking=no -f -N -L 8153:localhost:8153 -o ServerAliveInterval=15 -J ${{ secrets.GATEWAY_USER }}@${{ secrets.GATEWAY_HOST }} ${{ secrets.TARGET_USER }}@${{ secrets.TARGET_HOST }}
- name: Test SSH tunnel
run: ssh -v -o StrictHostKeyChecking=no -p 8153 ${{ secrets.TARGET_USER }}@localhost "echo Connected to target host through tunnel"
- name: Create .password file
run: |
echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > ~/.ssh/.password
chmod 600 ~/.ssh/.password
- name: Configure AWS credentials
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
mkdir -p ~/.aws
echo "[default]" > ~/.aws/credentials
echo "aws_access_key_id=${{ secrets.AWS_ACCESS_KEY_ID }}" >> ~/.aws/credentials
echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.aws/credentials
- name: Log in to ECR
run: |
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 100225593120.dkr.ecr.us-east-1.amazonaws.com
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.docker-cache
key: ${{ runner.os }}-docker-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-
- name: Load Docker cache
run: |
if [ -d "/tmp/.docker-cache" ]; then
mkdir -p ~/.docker
cp /tmp/.docker-cache/* ~/.docker/ -r
fi
- name: Pull Ansible Docker Image
run: docker pull 100225593120.dkr.ecr.us-east-1.amazonaws.com/agr_ansible_run:stage
- name: Save Docker cache
run: |
mkdir -p /tmp/.docker-cache
cp ~/.docker/* /tmp/.docker-cache/ -r
- name: Test Docker Container
run: |
docker run --rm \
--mount type=bind,source=${{ github.workspace }},target=/usr/src/ansible \
--mount type=bind,source=$HOME/.ssh/.password,target=/usr/src/ansible/.password \
100225593120.dkr.ecr.us-east-1.amazonaws.com/agr_ansible_run:stage \
/bin/bash -c "echo 'Hello, World!'"
- name: Confirm Connection to Final Server
run: ssh -v target_host "touch ~/github_action_test_file"