Skip to content

Update run-ansible-configs.yml #13

Update run-ansible-configs.yml

Update run-ansible-configs.yml #13

name: Run Ansible Configurations
on:
push:
branches:
- master
paths:
- '.github/workflows/run-ansible-configs.yml'
- 'server/**'
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install Ansible
run: |
python -m pip install --upgrade pip
pip install ansible
- name: Create Inventory File
run: |
echo "[servers]" > inventory
echo "${{ secrets.EC2_PRIVATE_IP_1 }} ansible_ssh_user=ubuntu" >> inventory
- name: Decrypt secrets file
run: |
echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > .vault_pass
ansible-vault decrypt ansible/secrets.yml --vault-password-file .vault_pass
- name: Add SSH key and debug
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keygen -l -f ~/.ssh/id_ed25519 || echo "Invalid key format"
ls -l ~/.ssh/id_ed25519
- name: Add target host to known_hosts
run: |
ssh-keyscan -H "${{ secrets.EC2_PRIVATE_IP_1 }}" >> ~/.ssh/known_hosts 2>/dev/null || echo "ssh-keyscan failed"
- name: Check EC2 instance accessibility
run: |
echo "Checking EC2 instance accessibility"
ping -c 4 ${{ secrets.EC2_PRIVATE_IP_1 }} || echo "Ping failed"
nc -zv ${{ secrets.EC2_PRIVATE_IP_1 }} 22 || echo "Netcat failed"
- name: Test SSH connection
run: |
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no ubuntu@${{ secrets.EC2_PRIVATE_IP_1 }} echo "SSH connection successful" || echo "SSH connection failed"
- name: Run Ansible Playbook
run: |
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory ansible/playbook.yml --vault-password-file .vault_pass -vvv
- name: Cleanup
if: always()
run: |
rm -f ~/.ssh/id_ed25519 .vault_pass