2
我想通過Ansible V2 python api運行正確的playbook。這裏是我的方法:如何在Ansible v2中運行playbook api
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.executor.playbook_executor import PlaybookExecutor
variable_manager = VariableManager()
loader = DataLoader()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/path/to/inventory')
playbook_path = '/path/to/playbook'
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='ubuntu', private_key_file=ssh_key_file, 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=None, check=False)
variable_manager.extra_vars = {'hosts': 'mywebserver'}
passwords = {}
pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
results = pbex.run()
但它不能正常運行,並給我下面的錯誤消息:
TASK [jetty : Create default variables for jetty] ******************************
fatal: [172.17.21.52]: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'groups' is undefined"}
在這種TASK
我有一個template
Jinja2的文件與它的變量groups['xxxx']
。我認爲這是Ansible內置的組變量。任何人都可以告訴它爲什麼會發生,以及如何解決它。提前致謝。
我註釋掉'variable_manager.extra_vars = { '主機': 'MyWebServer的'}'因爲它是沒有必要的。 – Binary