0

我的剧本需要能够支持在执行生产部署时向 github 存储库添加标签。目的是通过 Ansible 的内置 github_release 模块自动生成发布标签,作为 Ansible 服务器上的本地操作。该模块的文档很清楚,并且直截了当,该模块需要 github3.py Python 模块。

我现在遇到的问题是我们的“CICD 操作员”不相信 github3.py 模块需要安装在 Ansible 服务器上。信念是“github3.py”可以放入 /library 目录,并且无论在哪个服务器上运行该模块都可用。基于我对 Ansible playbook 库的有限理解,这似乎是合乎逻辑的;但是,github3 不仅仅是一个可以复制到目录中的简单 Python 脚本。

我发现的一切都表明这需要通过 PIP 安装在服务器上。这是做到这一点的唯一方法,还是我错过了一些基本过程来获取源 tar 球并将其添加为本地剧本库?我尝试将源代码提取到 ./library,甚至创建了指向结构中不同点的符号链接。

https://docs.ansible.com/ansible/latest/modules/github_release_module.html

https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#bundling-ansible-modules-with-playbooks

https://pypi.org/project/github3.py/1.3.0/

/playbook $ ls
ansible.cfg  checkConnectivity.yml  gitTagging.yml  library  roles  site.yml
/playbook $ ls -la library/
... dedup_list.py
... github3 -> /home/#####/AppEng_Ansible_Playbook/playbook/library/github3.py-1.3.0/src/github3
... github3.py -> /home/#####/AppEng_Ansible_Playbook/playbook/library/github3.py-1.3.0/src/github3
... github3.py-1.3.0
... github3.py-1.3.0.tar.gz
/playbook $ cat ansible.cfg
[default]
library = ./library
#github3.py = ./library/github3.py
#github3.py = ./library/github3
#github3 = ./library/github3
/playbook $ cat gitTagging.yml
---
- hosts: all
  tasks:
  - name: Local Action - Create Tag
    local_action:
      module: github_release
      user: XYZ
      password: #####
      action: create_release
      repo: git@repo.XXXXX.com:Enterprise-Communications/web_php_cicd_testing.git
      tag: MyTag
      target: v0.0.9rc
      name: Ansible Test
      body: Test 1
    run_once: true
ansible-playbook 2.8.2
...
...
TASK [Local Action - Create Tag] ************************************************************************************************************************************************************************************************************
task path: /home/#####/AppEng_Ansible_Playbook/playbook/gitTagging.yml:14
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: #####
<localhost> EXEC /bin/sh -c 'echo ~##### && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/#####/.ansible/tmp/ansible-tmp-1568051389.77-61244317218380 `" && echo ansible-tmp-1568051389.77-61244317218380="` echo /home/#####/.ansible/tmp/ansible-tmp-1568051389.77-61244317218380 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/source_control/github_release.py
<localhost> PUT /home/#####/.ansible/tmp/ansible-local-228190qaU7p/tmptpN1cj TO /home/#####/.ansible/tmp/ansible-tmp-1568051389.77-61244317218380/AnsiballZ_github_release.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/#####/.ansible/tmp/ansible-tmp-1568051389.77-61244317218380/ /home/#####/.ansible/tmp/ansible-tmp-1568051389.77-61244317218380/AnsiballZ_github_release.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/#####/.ansible/tmp/ansible-tmp-1568051389.77-61244317218380/AnsiballZ_github_release.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/#####/.ansible/tmp/ansible-tmp-1568051389.77-61244317218380/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_github_release_payload_fZHzWp/__main__.py", line 134, in <module>
    import github3
ImportError: No module named github3

fatal: [localhost -> localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "action": "create_release",
            "body": "Test 1",
            "draft": false,
            "name": "Ansible Test",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "prerelease": false,
            "repo": "git@repo.XXXXX.com:Enterprise-Communications/web_php_cicd_testing.git",
            "tag": "MyTag",
            "target": "v0.0.9rc",
            "token": null,
            "user": "XYZ"
        }
    },
    "msg": "Failed to import the required Python library (github3.py >= 1.0.0a3) on led####'s Python /usr/bin/python. Please read module documentation and install in the appropriate location"
}
4

1 回答 1

0

您可以让 playbook 将必要的依赖项安装到临时 virtualenv 中,然后使用 playbook 的其余部分针对该 venv 执行:

- hosts: localhost
  connection: local
  tasks:
  - raw: |
     if [ -x /tmp/bob/bin/python ]; then exit 0; fi
     /usr/local/bin/python3 -m venv --prompt tmp /tmp/bob
     .  /tmp/bob/bin/activate
     /tmp/bob/bin/pip install --upgrade 'github3.py >= 1.0.0a3'

- hosts: localhost
  connection: local
  vars:
    ansible_python_interpreter: /tmp/bob/bin/python
  tasks:
  - github_release:
      user: ansible
      repo: ansible
      action: latest_release

(替换你自己的hosts:and connection:;我只是使用 localhost 和 local 来测试这样的事情是否可行,以及它的答案)

甚至可以在一个角色中做这样的事情,进行一些清理行动或其他什么,但我没有走那么远

于 2019-09-10T04:33:29.573 回答