-
Notifications
You must be signed in to change notification settings - Fork 0
60 lines (52 loc) · 1.77 KB
/
run-ansible-configs.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
name: Run Ansible Configurations
on:
push:
branches:
- master
paths:
- 'server/**'
- '.github/workflows/run-ansible-configs.yml'
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
run: |
set -x
mkdir -p ~/.ssh
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/id_ed25519_temp
ssh-keygen -p -f ~/.ssh/id_ed25519_temp -m pem -N ""
mv ~/.ssh/id_ed25519_temp ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ls -l ~/.ssh/id_ed25519
ssh-keyscan -H ${{ secrets.EC2_PRIVATE_IP_1 }} >> ~/.ssh/known_hosts
cat ~/.ssh/known_hosts
shell: bash
- name: Test SSH connection
run: |
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no ubuntu@${{ secrets.EC2_PRIVATE_IP_1 }} echo "SSH connection successful"
- name: Run Ansible Playbook
run: |
ansible-playbook -i inventory ansible/playbook.yml --vault-password-file .vault_pass
- name: Cleanup
if: always()
run: |
rm -f ~/.ssh/id_ed25519 .vault_pass