如何将收藏与剧本一起保存并推送到自己的 github 存储库?

Kim*_*cks 4 ansible

我目前的剧本是这样构建的

projectroot
 |
 |--ubuntu2004
       |
       |--00_setup
             |
             |--vars
             |--playbook.yml
             |--readme.md
Run Code Online (Sandbox Code Playgroud)

因为我的剧本使用 ansible.posix 并且我还将我的剧本提交到 github 存储库中。我希望是否有办法在这种情况下包含所需的集合ansible.posix作为要求以及如何安装它?

我看到有多种方式https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#installing-collections

我想知道在使用 github 存储库作为剧本的版本控制时有意义的最佳实践方式是什么?

rpm*_*192 5

有几种方法可以做到这一点。我建议使用需求文件,因为它最容易设置和管理。

  1. 创建用于安装所需模块的需求文件。

    最好的方法是创建一个需求文件,该文件引用您的剧本所需的集合。然后您可以使用它来安装所需的集合和/或角色。

    ---
    collections:
      - name: ansible.posix
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将模块存储在存储库中并通过ansible-galaxy.

    您可以将模块上传到 git 存储库,然后安装它。我不建议这样做,因为当有管理依赖项的工具可用时,将依赖项存储在源代码中并不是一个好习惯。

    ansible-galaxy collection install 
    git+https://github.com/organization/repo_name.git,devel
    
    Run Code Online (Sandbox Code Playgroud)
  3. 通过剧本安装模块

    我已经设置了一个主节点来运行 Ansible 之前的剧本,并通过command剧本中的任务安装模块。只要您不引用/包含任何使用该模块的任务或游戏,这将正常工作。

    - name: Install ansible posix module
      command: ansible-galaxy collection install ansible.posix
    
    Run Code Online (Sandbox Code Playgroud)

理想的方法是在执行任务之前必须按照剧本安装所需的模块。但是,无法忽略模块丢失时抛出的错误,因此您必须创建一个不包含/引用使用该模块的任何任务的游戏。