将存储在企业内部仓库(如GitHub Enterprise)中的ansible角色存储库安装并使用
如果想要在企业中托管的GitHub Enterprise中使用来自多个Playbook存储库的参考并利用其中的自定义角色,请参考以下提示。
不仅仅是GitHub企业版,我认为在其他基于git的软件配置管理(SCM)中,比如GitLab,也可以使用同样的方法(未经核实)。
太長沒時間看
创建一个需要安装的角色列表 requirements.yml。
- src: git@github.example.com:yassan/ansible-role-ping.git
  scm: git
  version: v1.0  # master でもOK
  name: ping
使用以下内容进行安装。
ansible-galaxy install -v -r requirements.yml
roles_path=.ansible/roles请确保先创建 .ansible/roles 文件夹。
环境
Ansible:2.9.27
Python:3.8
操作系统:Ubuntu 20.04 LTS
前提条件
事先创建仅包含角色的存储库。
大致就是这样。
请参考:如何创建角色
使用ansible-galaxy命令创建Role会更加方便,具体步骤如下。
如何使用Role
机制 (jī zhì)
使用ansible-galaxy命令,并指定角色存储库来进行安装。
❯ ansible-galaxy role install --help
usage: ansible-galaxy role install [-h] [-s API_SERVER] [--api-key API_KEY] [-c] [-v] [-f] [-p ROLES_PATH] [-i] [-n | --force-with-deps]
                                   [-r ROLE_FILE] [-g]
                                   [role_name [role_name ...]]
positional arguments:
  role_name             Role name, URL or tar file
optional arguments:
  -h, --help            show this help message and exit
  -s API_SERVER, --server API_SERVER
                        The Galaxy API server URL
  --api-key API_KEY     The Ansible Galaxy API key which can be found at https://galaxy.ansible.com/me/preferences. You can also set the token
                        for the GALAXY_SERVER_LIST entry.
  -c, --ignore-certs    Ignore SSL certificate validation errors.
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable connection debugging)
  -f, --force           Force overwriting an existing role or collection
  -p ROLES_PATH, --roles-path ROLES_PATH
                        The path to the directory containing your roles. The default is the first writable one configured via
                        DEFAULT_ROLES_PATH: ~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
  -i, --ignore-errors   Ignore errors and continue with the next specified role.
  -n, --no-deps         Don't download roles listed as dependencies.
  --force-with-deps     Force overwriting an existing role and its dependencies.
  -r ROLE_FILE, --role-file ROLE_FILE
                        A file containing a list of roles to be imported.
  -g, --keep-scm-meta   Use tar instead of the scm archive option when packaging the role.
细节详述
指定安装先
如果在ansible-galaxy命令中未指定安装路径,则安装将在~/.ansible/roles下进行。
如果使用Jenkins等CI工具,由于~/.ansible/roles目录不唯一,会导致CI作业环境不稳定,因此建议添加配置到ansible.cfg,将角色安装在使用Playbook的代码库的.ansible/role文件夹中(如果不需要也可不指定)。
[defaults]
roles_path=.ansible/roles
请先创建一个名为 `.ansible/roles` 的文件夹。
参考 Ansible 配置设置 —— Ansible 文档
创建一个希望安装的角色列表。
用以下的格式创建一个名为requirements.yml的文件,其中包含您想要安装的角色列表。
- src: git@github.example.com:yassan/ansible-role-ping.git
  scm: git
  version: v1.0  # master でもOK
  name: ping
关于列表的格式,还有其他模式可供选择。
从文件中安装多个角色 | 安装内容 — Ansible文档
角色的安装
按照以下方式进行安装。
ansible-galaxy install -v -r requirements.yml
然而,如果安装相同的版本,将会被跳过如下所示。
❯ ansible-galaxy install -v -r requirements.yml 
Using /home/yassan/repo/example/test-ext-role-play/ansible.cfg as config file
- ping (v1.0) is already installed, skipping.
如果你确实想要重新安装,请加上 -f 选项进行执行,就可以了。
❯ ansible-galaxy install -v -f -r requirements.yml 
Using /home/yassan/repo/example/test-ext-role-play/ansible.cfg as config file
- changing role ping from v1.0 to v1.0
- extracting ping to /home/yassan/repo/example/test-ext-role-play/.ansible/roles/ping
- ping (v1.0) was installed successfully
此外,在进行安装操作后,您会发现Role已保存在roles_path参数指定路径的子目录中。
❯ tree .ansible -d | head
.ansible
└── roles
    └── ping
        ├── defaults
        ├── handlers
        ├── meta
        ├── molecule
        │   └── default
        ├── tasks
        ├── tests
最终
结束
最后
到最后
如果在plyabook的存储库中放入太多的角色,会使测试管理变得复杂,因此建议在角色增加到一定程度时将存储库分离并进行使用。