Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to limit ansible hosts with regex #785

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

marwin1991
Copy link

Hello, I have added simple change urllib.parse.unquote(host) for host variable to allow using regex.

I think is kinda not the best way, I was thinking about option --ansible-limit and in ansble.py we could change method get_hosts as follows:

    @classmethod
    def get_hosts(cls, host: str, **kwargs: Any) -> list[str]:
        inventory = kwargs.get("ansible_inventory")
        hosts = AnsibleRunner.get_runner(inventory).get_hosts(host or "all")
        if kwargs.get("ansible_limit"):
            # Filter hosts based on the limit expression
            from ansible.parsing.dataloader import DataLoader
            from ansible.inventory.manager import InventoryManager

            loader = DataLoader()
            inventory_manager = InventoryManager(loader=loader, sources=inventory)
            limited_hosts = inventory_manager.get_hosts( pattern=kwargs.get("ansible_limit"))
            return [host for host in inventory if
                    host.name in [h.name for h in limited_hosts]]
        else:
            return hosts

what do you think?

@marwin1991
Copy link
Author

@philpep ping :)

@marwin1991
Copy link
Author

If this change is okay I will create docs

Comment on lines 94 to 95
from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be clearer to bring the imports to the top of the file

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osk-8 done :)

@osk-8
Copy link

osk-8 commented Nov 21, 2024

++1 to that change - seems useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants