创建一个 ansible 集合并在 gitlab 的 playbook 上使用它

问题描述 投票:0回答:1

我创建了一个包含 2 个角色的 ansible 集合,用于部署 node_explorer 和配置防火墙。我使用的是ansible 2.12

这是 gitlab 存储库中的根目录(使用 ansible-creator 生成)

.
└── mynamespace
    └── mycollection
        ├── CHANGELOG.rst
        ├── CODE_OF_CONDUCT.md
        ├── CONTRIBUTING
        ├── LICENSE
        ├── MAINTAINERS
        ├── README.md
        ├── changelogs
        │   └── config.yaml
        ├── devfile.yaml
        ├── docs
        │   └── docsite
        │       └── links.yml
        ├── extensions
        │   ├── eda
        │   │   └── rulebooks
        │   │       └── rulebook.yml
        │   └── molecule
        │       ├── integration_hello_world
        │       │   └── molecule.yml
        │       └── utils
        │           ├── playbooks
        │           │   ├── converge.yml
        │           │   └── noop.yml
        │           └── vars
        │               └── vars.yml
        ├── galaxy.yml
        ├── meta
        │   └── runtime.yml
        ├── plugins
        │   ├── README.md
        │   ├── action
        │   │   └── __init__.py
        │   ├── cache
        │   │   └── __init__.py
        │   ├── filter
        │   │   ├── __init__.py
        │   │   └── hello_world.py
        │   ├── inventory
        │   │   └── __init__.py
        │   ├── module_utils
        │   │   └── __init__.py
        │   ├── modules
        │   │   └── __init__.py
        │   ├── plugin_utils
        │   │   └── __init__.py
        │   ├── sub_plugins
        │   │   └── __init__.py
        │   └── test
        │       └── __init__.py
        ├── pyproject.toml
        ├── requirements.txt
        ├── roles
        │   ├── firewall
        │   │   ├── LICENSE
        │   │   ├── README.md
        │   │   ├── defaults
        │   │   │   └── main.yml
        │   │   ├── handlers
        │   │   ├── meta
        │   │   │   └── main.yml
        │   │   ├── molecule
        │   │   │   └── default
        │   │   │       ├── converge.yml
        │   │   │       └── molecule.yml
        │   │   ├── tasks
        │   │   │   └── main.yml
        │   │   └── templates
        │   ├── geerlingguy.node_exporter
        │   │   ├── LICENSE
        │   │   ├── README.md
        │   │   ├── defaults
        │   │   │   └── main.yml
        │   │   ├── handlers
        │   │   │   └── main.yml
        │   │   ├── meta
        │   │   │   └── main.yml
        │   │   ├── molecule
        │   │   │   └── default
        │   │   │       ├── converge.yml
        │   │   │       └── molecule.yml
        │   │   ├── tasks
        │   │   │   ├── config-version.yaml
        │   │   │   └── main.yml
        │   │   └── templates
        │   │       └── node_exporter.service.j2
        │   ├── run
        │   │   ├── README.md
        │   │   ├── defaults
        │   │   │   └── main.yml
        │   │   ├── files
        │   │   ├── handlers
        │   │   │   └── main.yml
        │   │   ├── meta
        │   │   │   └── main.yml
        │   │   ├── tasks
        │   │   │   └── main.yml
        │   │   ├── templates
        │   │   ├── tests
        │   │   │   └── inventory
        │   │   └── vars
        │   │       └── main.yml
        │   └── settings.json
        ├── test-requirements.txt
        ├── tests
        │   ├── integration
        │   │   ├── __init__.py
        │   │   ├── targets
        │   │   │   └── hello_world
        │   │   │       └── tasks
        │   │   │           └── main.yml
        │   │   └── test_integration.py
        │   └── unit
        │       ├── __init__.py
        │       └── test_basic.py
        └── tox-ansible.ini

然后我构建它以生成 tar.gz 包(来自 mynamespace/mycollection 文件夹,因为没有 Galaxy.yml 文件导致顶层出现错误)

在另一个存储库 git 中,我有一个 ansible 脚本,我想导入上面的集合以使用我的两个角色

从我的 ansible 脚本的顶部文件夹中,我运行了 Ansible 安装集合命令

ansible-galaxy collection install <path to root folder of my collection>/mynamespace/mycollection/mynamespace-mycollection-1.0.0.tar.gz -p ./collections/

然后我配置我的剧本以使用它

    ---

- name: Installation de node exporter
  hosts: node_exporter
  tasks:
    - import_role:
        name: namespace.mycollection.geerlingguy.node_exporter
  tags:
    - node_exporter
  become: true

然后我运行剧本

ansible-playbook node_exporter.yml -i inventories/transverse --vault-password-file ~/.vault_pass.txt

但是我的角色没有找到

ERROR! the role 'mynamespace.mycollection.geerlingguy.node_exporter' was not found in <ansible script root path>/roles:/home/myuser/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:<ansible script root path>

The error appears to be in '<ansible script root path>/node_exporter.yml': line 7, column 15, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    - import_role:
        name: mynamespace.mycollection.geerlingguy.node_exporter
              ^ here

我遵循 https://blog.stephane-robert.info/docs/infra-as-code/gestion-de-configuration/ansible/collections/#build-de-notre-collection-ansible 的教程,但我有失败的。我的行为有什么问题吗?

另一个问题,我需要在 gitlab 管道中编写脚本,因此我必须在每个管道上构建并安装来自 gitlab 的集合。如何从 gitlab 将集合构建/安装到 ansible 脚本中?

ansible gitlab ansible-2.x ansible-collections
1个回答
0
投票

这是黑暗中的镜头,我现在无法重新创建您的环境,但我认为您需要从导入角色中删除前导

namespace.mycollection.

从此:

tasks:
- import_role:
    name: namespace.mycollection.geerlingguy.node_exporter

对此:

tasks:
- import_role:
    name: geerlingguy.node_exporter

我认为这仍然会优先选择当前命名空间中的

geerlingguy
命名空间:Ansible 应该在使用其他源之前使用更具体(更本地)的角色/集合。

我相信这在文档的“安装与剧本相邻的集合”部分中进行了讨论:

https://docs.ansible.com/ansible/latest/collections_guide/collections_installing.html

作为最后一个选择,您可以使用

ANSIBLE_COLLECTIONS_PATHS
环境变量来告诉可用的可以使用哪些集合。 这对我来说感觉“糟糕”,并且随着时间的推移,维护可能会变得更加复杂。

© www.soinside.com 2019 - 2024. All rights reserved.