Python 如何将外部库导入Ansible模块?

Python 如何将外部库导入Ansible模块?,python,ansible,Python,Ansible,我想在本地添加Ansible模块。模块应该使用外部Python库。我只添加了以下行: from ansible.module_utils.foo import Bar 添加到Ansible新模块模板,使其如下所示: my_test.py #!/usr/bin/python # Copyright: (c) 2018, Terry Jones <terry.jones@example.org> # GNU General Public License v3.0+ (see COPY

我想在本地添加Ansible模块。模块应该使用外部Python库。我只添加了以下行:

from ansible.module_utils.foo import Bar
添加到Ansible新模块模板,使其如下所示:

my_test.py

#!/usr/bin/python

# Copyright: (c) 2018, Terry Jones <terry.jones@example.org>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    'status': ['preview'],
    'supported_by': 'community'
}

DOCUMENTATION = '''
---
module: my_test

short_description: This is my test module

version_added: "2.4"

description:
    - "This is my longer description explaining my test module"

options:
    name:
        description:
            - This is the message to send to the test module
        required: true
    new:
        description:
            - Control to demo if the result of this module is changed or not
        required: false

extends_documentation_fragment:
    - azure

author:
    - Your Name (@yourhandle)
'''

EXAMPLES = '''
# Pass in a message
- name: Test with a message
  my_test:
    name: hello world

# pass in a message and have changed true
- name: Test with a message and changed output
  my_test:
    name: hello world
    new: true

# fail the module
- name: Test failure of the module
  my_test:
    name: fail me
'''

RETURN = '''
original_message:
    description: The original name param that was passed in
    type: str
    returned: always
message:
    description: The output message that the test module generates
    type: str
    returned: always
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.foo import Bar


def run_module():
    # define available arguments/parameters a user can pass to the module
    module_args = dict(
        name=dict(type='str', required=True),
        new=dict(type='bool', required=False, default=False)
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(
        changed=False,
        original_message='',
        message=''
    )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
    )

    # if the user is working with this module in only check mode we do not
    # want to make any changes to the environment, just return the current
    # state with no modifications
    if module.check_mode:
        module.exit_json(**result)

    # manipulate or modify the state as needed (this is going to be the
    # part where your module will do what it needs to do)
    result['original_message'] = module.params['name']
    result['message'] = 'goodbye'

    # use whatever logic you need to determine whether or not this module
    # made any modifications to your target
    if module.params['new']:
        result['changed'] = True

    # during the execution of the module, if there is an exception or a
    # conditional state that effectively causes a failure, run
    # AnsibleModule.fail_json() to pass in the message and the result
    if module.params['name'] == 'fail me':
        module.fail_json(msg='You requested this to fail', **result)

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    module.exit_json(**result)

def main():
    run_module()

if __name__ == '__main__':
    main()
我已将模块my_test.py放在以下本地化中:

~/.ansible/plugins/modules
我扩展了ANSIBLE\u模块\u UTILS环境变量,使foo库可见。通常,撇开Ansible不谈,foo库的一部分可以通过以下方式导入Python脚本:

from foo import Bar
我已经测试过,当例如foo是一个Python脚本并且Bar是该脚本中的一个类时,testmod.yml剧本可能会正确运行。问题是foo是一个目录,没有foo.py文件,也没有Bar.py。在我的例子中,当我运行testmod.yml时,我收到回溯:

ImportError: No module named foo.config

您能告诉我如何才能在本地Ansible模块中使用foo外部库吗?

我没有深入研究您的代码。但是如果我可以问一下,您是否检查了您的模块是否存在于PATH或sys.PATH中?您可以迭代sys.PATH并检查列表中是否存在python words中的目录或包paht。如果没有,您可以通过sys.path.insert()@Adam我没有插入它的路径。扩展sys.path后,它开始正常工作。我被一个事实误导了,Ansible能够找到foo,而它是一个插入到该路径中的Python脚本,而没有扩展sys.path。我没有深入研究您的代码。但是如果我可以问一下,您是否检查了您的模块是否存在于PATH或sys.PATH中?您可以迭代sys.PATH并检查列表中是否存在python words中的目录或包paht。如果没有,您可以通过sys.path.insert()@Adam我没有插入它的路径。扩展sys.path后,它开始正常工作。我被一个事实误导了,Ansible能够找到foo,而它是插入到该路径中的Python脚本,而没有扩展sys.path
ImportError: No module named foo.config