복붙노트

[PYTHON] Anbook 2.0 Python API를 사용하여 플레이 북을 실행하는 방법

PYTHON

Anbook 2.0 Python API를 사용하여 플레이 북을 실행하는 방법

변수의 목록을 반복하면서 재생 목록을 반복하고 싶기 때문에 기존 Anabilities 플레이 북을 호출하는 Python 스크립트를 작성하려고합니다.

이 포스트는 그것을 잘 설명합니다. 2.0 이전 버전 : Python API를 사용하여 장난 플레이 북 실행하기

이 문서는 스크립트에 새로운 게임 플레이 북을 작성하는 경우 매우 잘 설명합니다. http://docs.ansible.com/ansible/developing_api.html

그러나 파이썬 API 2.0을 사용하여 기존 게임 플레이 북을 호출하는 방법을 볼 수 없으며, anant.runner는 더 이상 작동하지 않습니다.

도와주세요, Stackoverflow-Wan Kenobi. 너는 내 유일한 희망이야.

해결법

  1. ==============================

    1.설명서가 놀라 울 정도로 부족하고 여기에서 시작해야합니다.

    설명서가 놀라 울 정도로 부족하고 여기에서 시작해야합니다.

    즉, 여기에 제가 해설 한 간단한 스크립트가 있습니다.

    #!/usr/bin/env python
    
    import os
    import sys
    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='/home/slotlocker/hosts2')
    playbook_path = '/home/slotlocker/ls.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 = {'hosts': 'mywebserver'} # This can accomodate various other command line arguments.`
    
    passwords = {}
    
    pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
    
    results = pbex.run()
    
  2. from https://stackoverflow.com/questions/35368044/how-to-use-ansible-2-0-python-api-to-run-a-playbook by cc-by-sa and MIT license