使用Python API运行Ansible-Playbook [英] Running ansible-playbook using Python API

查看:337
本文介绍了使用Python API运行Ansible-Playbook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python脚本运行剧本?在python中使用ansible模块等效于以下内容:

How can I run a playbook in python script? What is the equivalent of the following using ansible module in python:

ansible -i hosts dbservers -m setup
ansible-playbook -i hosts -vvvv -k site.yml

我正在 http://docs.ansible.com/developing_api.html但它们的示例非常有限.

I was looking at their documenation in http://docs.ansible.com/developing_api.html but they have very limited examples.

推荐答案

弃用声明:此帖子自ansible 2起不起作用.API已更改.

"Python API"下的 Ansible文档中对此进行了介绍.

This covered in the Ansible documentation under "Python API."

例如,ansible -i hosts dbservers -m setup是通过以下方式实现的:

For example, ansible -i hosts dbservers -m setup is implemented via:

import ansible.runner

runner = ansible.runner.Runner(
   module_name='setup',
   module_args='',
   pattern='dbservers',
)
dbservers_get_facts = runner.run()

Runner的__init__方法中有很多未记录的参数(来自ansible.runner). 太多内容无法在线列出,但是我在这篇文章中包含了一些参数,以猜测您要查找的内容.

There are a bunch of non-documented parameters in the __init__ method of Runner (from ansible.runner). There's too many to list inline, but I've included some of the parameters in this post as a guess to what you're specifically looking for.

class Runner(object):
    ''' core API interface to ansible '''

    # see bin/ansible for how this is used...

    def __init__(self,
        host_list=C.DEFAULT_HOST_LIST,      # ex: /etc/ansible/hosts, legacy usage
        module_path=None,                   # ex: /usr/share/ansible
        module_name=C.DEFAULT_MODULE_NAME,  # ex: copy
        module_args=C.DEFAULT_MODULE_ARGS,  # ex: "src=/tmp/a dest=/tmp/b"
        ...
        pattern=C.DEFAULT_PATTERN,          # which hosts?  ex: 'all', 'acme.example.org'
        remote_user=C.DEFAULT_REMOTE_USER,  # ex: 'username'
        remote_pass=C.DEFAULT_REMOTE_PASS,  # ex: 'password123' or None if using key
        remote_port=None,                   # if SSH on different ports
        private_key_file=C.DEFAULT_PRIVATE_KEY_FILE, # if not using keys/passwords
        sudo_pass=C.DEFAULT_SUDO_PASS,      # ex: 'password123' or None
        ...
        sudo=False,                         # whether to run sudo or not
        sudo_user=C.DEFAULT_SUDO_USER,      # ex: 'root'
        module_vars=None,                   # a playbooks internals thing
        play_vars=None,                     #
        play_file_vars=None,                #
        role_vars=None,                     #
        role_params=None,                   #
        default_vars=None,                  #
        extra_vars=None,                    # extra vars specified with he playbook(s)
        is_playbook=False,                  # running from playbook or not?
        inventory=None,                     # reference to Inventory object
        ...
        su=False,                           # Are we running our command via su?
        su_user=None,                       # User to su to when running command, ex: 'root'
        su_pass=C.DEFAULT_SU_PASS,
        vault_pass=None,
        ...
        ):

例如,上面指定sudo用户和密码的命令为:

For instance, the above command that specifies a sudo user and pass would be:

runner = ansible.runner.Runner(
   module_name='setup',
   module_args='',
   pattern='dbservers',
   remote_user='some_user'
   remote_pass='some_pass_or_python_expression_that_returns_a_string'
)

对于剧本,请查看 playbook.PlayBook ,它需要一组类似的初始化程序:

For playbooks, look into playbook.PlayBook, which takes a similar set of initializers:

class PlayBook(object):
    '''
    runs an ansible playbook, given as a datastructure or YAML filename.
    ...
    '''

    # *****************************************************

    def __init__(self,
        playbook         = None,
        host_list        = C.DEFAULT_HOST_LIST,
        module_path      = None,
        .... 

,并且可以使用.run()方法执行.例如:

and can be executed with the .run() method. e.g.:

from ansible.playbook import PlayBook
pb = PlayBook(playbook='/path/to/book.yml, --other initializers--)
pb.run()

更强大的用法可以在 ansible-playbook文件中找到.

more robust usage can be found in the ansible-playbook file.

据我所知,将剧本转换为Python模块会涉及更多的工作,但是上面列出的文档应该能让您了解,您可以重用Ansible内置的YAML解析器将剧本转换为变量.

As far as I know, translating playbooks to Python modules is a bit more involved, but the documentation listed above should get you covered and you can reuse the YAML parser built into Ansible to convert playbooks to variables.

这篇关于使用Python API运行Ansible-Playbook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆