可以从GitHub以外的存储库中安装角色,使用 Ansible Galaxy

背景:

为了重复使用Ansible Role,可以通过Ansible Galaxy来安装托管在其中的Role,但也可以直接引用自己创建的存储库来进行安装。
然而,在使用方法及Role更新方面,使用Github和其他平台存在差异,并且在开发过程中可能会遇到麻烦。为此,我进行了总结。

安装Role

如果使用Galaxy服务器托管的角色

ansible-galaxy install Role名

如果使用Github存储库

ansible-galaxy install Repository名

默认情况下,这些文件将安装在 ~/.ansible/roles/ 下,但可以通过使用 -p 选项来指定其他的安装目录。同时也可以指定提交哈希和分支名称。

ansible-galaxy install -p path Repository名

如果使用除了Github之外的代码仓库

ansible-galaxy install Git+Repository名

类似于Github的情况,可以使用-p参数。

角色的批量管理

通过编写requirements.yml文件可以批量管理要安装的角色。

# from galaxy
- src: yatesr.timezone

# from GitHub
- src: https://github.com/bennojoy/nginx

# from GitHub, overriding the name and specifying a specific tag
- src: https://github.com/bennojoy/nginx
  version: master
  name: nginx_role

# from a webserver, where the role is packaged in a tar.gz
- src: https://some.webserver.example.com/files/master.tar.gz
  name: http-role

# from Bitbucket
- src: git+http://bitbucket.org/willthames/git-ansible-galaxy
  version: v1.4

# from Bitbucket, alternative syntax and caveats
- src: http://bitbucket.org/willthames/hg-ansible-galaxy
  scm: hg

# from GitLab or other git-based scm
- src: git@gitlab.company.com:mygroup/ansible-base.git
  scm: git
  version: "0.1"  # quoted, so YAML doesn't parse this as a floating-point value

我会解释每一个属性.

    • src: roleのソースであり、必至の項目です。書き方はCLIからインストールするときと同様です。

 

    • scm: Gitlabの場合はgit、Bitbucketの場合はhgとしてください、GithubとGlaxyから利用する場合は書かないでください。

 

    • version: ダウンロードするroleのバージョンを指定します。コミットハッシュ、ブランチ名、roleのtagを指定できます。

 

    name: roleを保存したい名前を記述できます。インストール後は指定した名前で使用することができます。

如果编写了requirements.yml文件,可以使用以下命令进行安装。

ansible-galaxy install -r requirements.yml

尾声

以下是一种选项的中文释义:“按照将role安装的方法,提到了使用Ansible Galaxy和使用Git仓库的方法。常用的role和较大的role应该存放在不同的仓库中!”

广告
将在 10 秒后关闭
bannerAds