An Ansible inventory defines and groups remote hosts by IP address or domain name. A workflow can create inventory.ini for the machines that Ansible will manage. section 4.2.1
An inventory supports a simple deployment setup:
- Groups hosts under a name such as [my_host_group], allowing a playbook to target that group or all hosts. section 4.2.1 section 4.2.2
- Works with a default Ansible configuration file that points commands toward the custom inventory. section 4.2.1
- Lets Ansible apply declared tasks consistently to one host or many hosts, provided the required SSH keys are available. section 4.2.2
A generated inventory
A GitHub Actions step creates devops/ansible/inventory.ini by writing [my_host_group] followed by the remote host IP address. The command ansible all -m ping then checks that the inventory and related configuration are in the expected location. section 4.2.1
Listing 4.5 Running Ansible with GitHub Actions
name: Run Ansible
on:
workflow_dispatch:
jobs:
run-playbooks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Python 3
uses: actions/setup-python@v4
with:
python-version: "3.8"
- name: Upgrade Pip & Install Ansible
run: |
python -m pip install --upgrade pip
python -m pip install ansible
- name: Implement the Private SSH Key
run: |
mkdir -p ~/.ssh/
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
- name: Ansible Inventory File for Remote host
run: |
mkdir -p ./devops/ansible/
export INVENTORY_FILE=./devops/ansible/inventory.ini
echo "[my_host_group]" > $INVENTORY_FILE
echo "${{ secrets.AKAMAI_INSTANCE_IP_ADDRESS }}" >> $INVENTORY_FILE
- name: Ansible Default Configuration File
run: |
mkdir -p ./devops/ansible/
cat <<EOF > ./devops/ansible/ansible.cfg
[defaults]
ansible_python_interpreter = '/usr/bin/python3'
ansible_ssh_private_key_file = ~/.ssh/id_rsa
remote_user = root
inventory = ./inventory.ini
host_key_checking = False
EOF
- name: Ping Ansible Hosts
working-directory: ./devops/ansible/
run: |
ansible all -m ping
- name: Run Ansible Playbooks
working-directory: ./devops/ansible/
run: |
ansible-playbook install-nginx.yaml
- name: Deploy Python via Ansible
working-directory: ./devops/ansible/
run: |
ansible-playbook deploy-python.yaml
Inspect the complete workflow that installs Ansible, creates the inventory, configures SSH access, and runs playbooks. section 4.2.1

