-
Notifications
You must be signed in to change notification settings - Fork 0
41 lines (39 loc) · 1.35 KB
/
ansible-count-hosts.yaml
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
---
name: Count Ansible hosts
on:
workflow_call:
inputs:
inventory_file:
type: string
description: "Relative path to the inventory file in the repository"
required: true
limit:
type: string
description: "Ansible limit that will have its hosts counted"
required: true
requirements_file:
type: string
description: "Relative path to the requirements file containing the Ansible version"
default: requirements.txt
outputs:
count:
value: ${{ jobs.job.outputs.count }}
jobs:
job:
runs-on: ubuntu-latest
outputs:
count: ${{ steps.count_hosts.outputs.count }}
steps:
- uses: actions/checkout@v4
- name: Create virtual environment and install ansible
run: |
python -m venv .venv
source .venv/bin/activate
# Only keep Ansible in the pip requirements
pip3 install $(grep ansible $GITHUB_WORKSPACE/${{ inputs.requirements_file }})
echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH
echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV
- name: Count hosts in limit
id: count_hosts
run: |
echo "count=$(ansible-inventory -i $GITHUB_WORKSPACE/${{ inputs.inventory_file }} --list -l ${{ inputs.limit }} | jq -e '._meta.hostvars | keys | length')" >> $GITHUB_OUTPUT