Ansible Collection Publish To Galaxy

摘要

创建Collection

1
2
3
4
# 在当前目录下创建collection
# 注意这里的collerction名称格式为 namespace.collection,名称只支持 [a-zA-Z0-9_]
$ ansible-galaxy collection init hanqunfeng.install_mongodb
- Collection hanqunfeng.install_mongodb was created successfully
  • 目录结构

1
2
3
4
5
6
7
8
9
10
hanqunfeng
└── install_mongodb
├── docs
├── galaxy.yml
├── meta
│ └── runtime.yml
├── plugins
│ └── README.md
├── README.md
└── roles

galaxy.yml中配置collection的元数据
runtime.yml中配置collection的运行时依赖
README.md中配置collection的描述信息
roles中创建role,这里可以创建多个role,我这里将上面创建的role复制到roles目录下,不过名称需要修改一下,不支持下划线,所以这里将role的名称改为centos_mongodb_install

相关代码参考:https://github.com/hanqunfeng/ansible_collections

  • 构建collection的tar包

1
2
3
4
5
# 构建collection的tar包时先要进入namespace目录下
$ cd hanqunfeng
# 指定collection的目录名称,会基于我们在 galaxy.yml中的配置信息生成tar包
$ ansible-galaxy collection build install_mongodb
Created collection for hanqunfeng.install_mongodb at /Users/hanqf/ansible_workspace/collections/hanqunfeng/hanqunfeng-install_mongodb-1.0.0.tar.gz
  • 上传collection到Galaxy




  • 搜索collection

  • 安装collection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 默认安装最新版本
$ ansible-galaxy collection install hanqunfeng.install_mongodb
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/hanqunfeng-install_mongodb-1.0.0.tar.gz to /Users/hanqf/.ansible/tmp/ansible-local-322947_s0gv88/tmp1fbnqss_/hanqunfeng-install_mongodb-1.0.0-s2wczaen
Installing 'hanqunfeng.install_mongodb:1.0.0' to '/Users/hanqf/.ansible/collections/ansible_collections/hanqunfeng/install_mongodb'
hanqunfeng.install_mongodb:1.0.0 was installed successfully

# 升级安装时需要加上 --force
ansible-galaxy collection install --force hanqunfeng.install_mongodb

# 查看已安装的role
ansible-galaxy collection list
  • 使用collection

1
2
3
4
5
---
- hosts: test-mongo
become: yes # 切换到root用户下执行
roles:
- hanqunfeng.install_mongodb.centos_mongodb_install # <namespace>.<collection_name>.<role_name>
  • 删除collection

1
2
# 没有现成的remove命令,只能手动删除
$ rm -rf ~/.ansible/collections/ansible_collections/hanqunfeng/install_mongodb
  • 更新collection

  • 重新安装collection

1
2
3
4
5
6
7
8
9
10
11
12
# 默认安装最新版本
$ ansible-galaxy collection install --force hanqunfeng.install_mongodb
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/hanqunfeng-install_mongodb-1.0.1.tar.gz to /Users/hanqf/.ansible/tmp/ansible-local-530779nbn853q/tmpiuxg6ads/hanqunfeng-install_mongodb-1.0.1-ep0cswny
Installing 'hanqunfeng.install_mongodb:1.0.1' to '/Users/hanqf/.ansible/collections/ansible_collections/hanqunfeng/install_mongodb'
hanqunfeng.install_mongodb:1.0.1 was installed successfully


# 安装指定版本
ansible-galaxy collection install hanqunfeng.install_mongodb:1.0.1

命令行发布collection

  • 先要在ansible-galaxy网站上获取api-key

  • 发布collection

1
2
3
4
5
6
# 注意替换自己的 api-key
$ ansible-galaxy collection publish --token xxxxxxxxxxxxxxx hanqunfeng-install_mongodb-1.0.2.tar.gz
Publishing collection artifact '/Users/hanqf/ansible_workspace/collections/hanqunfeng/hanqunfeng-install_mongodb-1.0.2.tar.gz' to default https://galaxy.ansible.com/api/
Collection has been published to the Galaxy server default https://galaxy.ansible.com/api/
Waiting until Galaxy import task https://galaxy.ansible.com/api/v3/imports/collections/018e7f26-b771-7c47-9c92-14e671b776dc/ has completed
Collection has been successfully published and imported to the Galaxy server default https://galaxy.ansible.com/api/

后记

目前2.16.4这个版本尚不支持在命令行搜索collection,只能通过web界面搜索。