1

如果我使用ansible-playbook命令运行,我有一本可以正常工作的剧本。但我想从 python 脚本执行这个剧本所以我在 python 中创建了一个函数,它接受库存文件的路径和剧本文件的路径。

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

def runAnsible(inventory, playbook, extra_vars = None):
    print('Running Playbook: ' + playbook)
    loader = DataLoader()
    context.CLIARGS = ImmutableDict(tags={}, listtags=False, listtasks=False, listhosts=False, syntax=False,
                                    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=False,
                                    become_method='sudo', become_user='root', verbosity=True, check=False, start_at_task=None)
    inventory = InventoryManager(loader=loader, sources=(inventory,))
    variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False))
    if extra_vars is not None:
        variable_manager._extra_vars = extra_vars
    pbex = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, passwords={})
    return pbex.run()

如果我使用此函数执行剧本,我会收到以下错误:

Running Playbook: /home/backend/backend-2.0/ansible-playbooks/configure_windows.yml

PLAY [Win Playbook] *************************************************************

TASK [Gathering Facts] *********************************************************
ok: [155.138.136.47]

TASK [Create Installer directory] **********************************************
ok: [155.138.136.47]

TASK [Download the latest installer] *******************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: at <ScriptBlock>, <No file>: line 29
fatal: [155.138.136.47]: FAILED! => {"changed": false, "msg": "Unhandled exception while executing module: Exception calling \"Create\" with \"2\" argument(s): \"Object reference not set to an instance of an object.\""}

PLAY RECAP *********************************************************************
155.138.136.47             : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

这是剧本代码:

---
- name: Configure Windows
  hosts: all
  become: false
  gather_facts: true
  vars:
    userdir : ansible_user
    ansible_connection: winrm
    ansible_winrm_server_cert_validation: ignore
    ansible_port: 5986
    ansible_transport: Basic
    ansible_winrm_scheme: https
    ansible_winrm_transport: ntlm

  tasks:
   - name: Create Installer directory
     win_file:
      path: C:\abc\xyz
      state: directory
   - name: Download the latest installer
     win_get_url:
      url: https://example.com/msi-installer.msi
      dest: C:\abc\xyz\msi-installer.msi
   - name: Install the latest package
     win_package:
      path: C:\abc\xyz\msi-installer.msi
      state: present
4

0 回答 0