Reputation: 41
I am trying run playbook from withing python code however I do not know how to pass --limit
option.
It's similar to following How to set "--limit" option in Ansible playbook api ,however I think API has changed a bit. I have following inventory:
[rhosts]
host_a
host_b
host_c
Inside ping.yaml hosts is set rhosts
but i need to limit it to only run on host_a,host_c
This is my code to run the playbook
from ansible import context
from ansible.cli import CLI
from ansible.module_utils.common.collections import ImmutableDict
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager
from ansible.vars.manager import VariableManager
from simple_term_menu import TerminalMenu
INVENTORY_FILE = 'hosts'
loader = DataLoader()
context.CLIARGS = ImmutableDict(tags={}, listtags=False, listtasks=False,
listhosts=False, syntax=False, connection='ssh',
module_path=None, forks=100, private_key_file=None,
ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None,
scp_extra_args=None, become=True, become_method='sudo', become_user='root',
verbosity=True, check=False, start_at_task=None,)
inventory = InventoryManager(loader=loader, sources=[INVENTORY_FILE])
variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False))
pbex = PlaybookExecutor(playbooks=['ping.yml'],
inventory=inventory, variable_manager=variable_manager,
loader=loader, passwords={})
results = pbex.run()
I tried to search in Ansible code and only found
def get_host_list(inventory, subset, pattern='all'):
no_hosts = False
if len(inventory.list_hosts()) == 0:
# Empty inventory
if C.LOCALHOST_WARNING and pattern not in C.LOCALHOST:
display.warning("provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'")
no_hosts = True
inventory.subset(subset)
hosts = inventory.list_hosts(pattern)
if not hosts and no_hosts is False:
raise AnsibleError("Specified hosts and/or --limit does not match any hosts")
return hosts
I am can't work out what and where to pass it, if that's the right bit of code.
Upvotes: 0
Views: 394
Reputation: 71
I just gave a try with ansible 2.16.2, and It works on my side with a few tweaks.
This is my code :
from ansible import context
from ansible.cli import CLI
from ansible.module_utils.common.collections import ImmutableDict
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager
from ansible.vars.manager import VariableManager
from ansible.plugins.loader import init_plugin_loader
INVENTORY_FILE = 'hosts'
loader = DataLoader()
context.CLIARGS = ImmutableDict(tags={}, listtags=False, listtasks=False,
listhosts=False, syntax=False, connection='ssh',
module_path=None, forks=100, private_key_file=None,
ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None,
scp_extra_args=None, become=True, become_method='sudo', become_user='root',
verbosity=True, check=False, start_at_task=None, subset='emptygroup',)
init_plugin_loader([])
inventory = InventoryManager(loader=loader, sources=[INVENTORY_FILE])
variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False))
CLI.get_host_list(inventory, context.CLIARGS['subset'])
pbex = PlaybookExecutor(playbooks=['ping.yml'],
inventory=inventory, variable_manager=variable_manager,
loader=loader, passwords={})
results = pbex.run()
You will notice that :
subset
keywork is used in context.CLIARGS
definition. Its value matches the --limit
parameter when directly given to ansible-playbook CLI.get_host_list
static method of CLI
class is invoked between inventory definition and playbook executor instanciation. You can find it in ansible source code here : https://github.com/ansible/ansible/blob/v2.16.2/lib/ansible/cli/playbook.py#L145. It is a wrapper for calling the subset
method of InventoryManager
class. Source code here : https://github.com/ansible/ansible/blob/v2.16.2/lib/ansible/cli/__init__.py#L582context.CLIARGS
.When I run this python code, the subset='emptygroup'
does not match any host in my inventory, and ansible only output this :
PLAY [A play] *********************************************************************************************************************************************
skipping: no hosts matched
PLAY RECAP ************************************************************************************************************************************************
Upvotes: 0