Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude an inventory group from 'all' by default in Ansible?

I'm setting up a small project in ansible, with a shared node with other projects. This node is the CI runner, and should rarely be the target of a playbook.

I want to exclude a group from all by default

The current solution that I have is just to have a group called bystanders, and exclude it from all playbooks that run all

hosts:

[groupA]
node1
[bystanders]
ci-node

playbook_example:

hosts: all:!bystanders
...

But this is prone to error, or forgetting to exclude that in some playbook, inadvertently running a playbook on that node.

like image 701
Isidoro Avatar asked Sep 02 '25 09:09

Isidoro


1 Answers

I asked this question somewhere else, and Dynamic inventory scripts were mentioned.

The dynamic inventory returns 'all' and 'ungrouped', so we can manipulate the results for these variables with dynamic inventory scripts.

{
    "_meta": {
            "hostvars": {}
    },
    "all": {
            "children": [
                    "ungrouped"
            ]
    },
    "ungrouped": {}
 }

However, in that conversation it was mentioned that 'all' is a bit of an anti-pattern, and avoiding it might be a good idea in the first place. 'all' means all, and in this case nothing that is project specific should use 'all'.

So I think this answers the question for me. I will avoid the use of all and in case I really need to do this, I will go with the dynamic inventory scripts

like image 161
Isidoro Avatar answered Sep 04 '25 23:09

Isidoro