【问题标题】:Ansible python API 2.0: run a playbook in a python script for windows clientsAnsible python API 2.0:在 Windows 客户端的 python 脚本中运行剧本
【发布时间】:2016-05-31 10:10:42
【问题描述】:

我正在尝试重新编写此脚本 (How to use Ansible 2.0 Python API to run a Playbook?) 以在针对 Windows 客户端运行的 python 脚本中调用 ansible playbook。我有一个测试文件夹,其中包含子文件夹 group_vars/win_clones.yml 中的 playbook (deploy.yml) 主机文件 (hosts) 和其他变量 (ansible_user,ansible_port..)。

我想指向这些文件以在 python 脚本中运行我的剧本。我为deploy.ymlhosts 文件这样做了,但我不知道在哪里指向group_vars/win_clones.yml。我怎样才能做到这一点?

from collections import namedtuple
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='fullpath/to/hosts')
playbook_path = 'fullpath/to/deploy.yml'

if not os.path.exists(playbook_path):
    print '[INFO] The playbook does not exist'
    sys.exit()

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='slotlocker', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='root', verbosity=None, check=False)

variable_manager.extra_vars = {'ansible_user': 'ansible', 'ansible_port': '5986', 'ansible_connection': 'winrm', 'ansible_password': 'pass', 'ansible_winrm_server_cert_validation': 'ignore'} # Here are the variables used in the winclones.yml

passwords = {}

pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
results = pbex.run()

编辑 1

这是通过python脚本运行的输出:

TASK [setup]     *******************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'upper'
fatal: [cl3]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'upper'
fatal: [cl1]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}

【问题讨论】:

    标签: python ansible


    【解决方案1】:

    我在使用 Ansible python API 时遇到了同样的问题。经过长时间的研究和一些对 ansible 代码的研究,我找到了可能的原因。 根据Ansible Documentation,您应该在提供 become 和 become_user 时设置 become_method,您还应该设置 become_method 值。通常 CLI 命令会从 ansible.cfg 文件中读取。 但是在 API 情况下,您将 become_method 显式设置为 None,这在成为特定用户(在您的情况下为 root)时在内部调用。

    将 become_method 设置为 (sudo, , su, pbrun, pfexec, doas,dzdo) 中的值之一,它应该可以正常工作。

    from collections import namedtuple
    from ansible.parsing.dataloader import DataLoader
    from ansible.vars import VariableManager
    from ansible.inventory import Inventory
    from ansible.playbook.play import Play
    from ansible.executor.playbook_executor import PlaybookExecutor 
    
    variable_manager = VariableManager()
    loader = DataLoader()
    
    inventory = Inventory(loader=loader, variable_manager=variable_manager,      host_list='fullpath/to/hosts')
    playbook_path = 'fullpath/to/deploy.yml'
    
    if not os.path.exists(playbook_path):
        print '[INFO] The playbook does not exist'
        sys.exit()
    
    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='slotlocker', 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=None, check=False)
    
    variable_manager.extra_vars = {'ansible_user': 'ansible', 'ansible_port': '5986', 'ansible_connection': 'winrm', 'ansible_password': 'pass', 'ansible_winrm_server_cert_validation': 'ignore'} # Here are the variables used in the winclones.yml
    
    passwords = {}
    
    pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
    results = pbex.run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 2020-03-31
      • 1970-01-01
      • 2016-05-10
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多