天天看点

12Ansible ansible galaxy的应用1.角色的基本操作2.使用要求文件安装角色3.管理下载的角色

角色的应用

  • 1.角色的基本操作
  • 2.使用要求文件安装角色
  • 3.管理下载的角色

1.角色的基本操作

  • 使用角色的帮助文档
[[email protected] ~]# ansible-galaxy role --help
usage: ansible-galaxy role [-h] ROLE_ACTION ...

positional arguments:
  ROLE_ACTION
    init       Initialize new role with the base structure of a role.
    remove     Delete roles from roles_path.
    delete     Removes the role from Galaxy. It does not remove or alter the
               actual GitHub repository.
    list       Show the name and version of each role installed in the
               roles_path.
    search     Search the Galaxy database by tags, platforms, author and
               multiple keywords.
    import     Import a role
    setup      Manage the integration between Galaxy and the given source.
    info       View more details about a specific role.
    install    Install role(s) from file(s), URL(s) or Ansible Galaxy

optional arguments:
  -h, --help   show this help message and exit
[[email protected] ~]# 

           
  • 创建角色
  • 删除已安装的角色
  • 列出所有角色的详细信息
  • 列出当前目录下的角色信息
  • 搜索角色
  • 搜索角色的平台
  • 查看角色信息
  • 安装角色
  • 安装在当前位置

2.使用要求文件安装角色

可以使用ansible-galaxy,根据某一文本文件中的定义来安装一个角色列表。例如,如果用户的一个playbook需要安装特定的角色,可以在项目目录中创建一个roles/requirements.yml文件来指定所需的角色。此文件充当playbook项目的依赖项清单,使得playbook的开发和调试能与任何支持角色分开进行。

例如,一个用于安装geerlingguy.redis的简单requirements.yml可能类似于如下:

- src: geerlingguy.redis
  version: "1.5.0"
           

src属性指定角色的来源,本例中为来自Ansible Galaxy的geerlingguy.redis角色。version属性是可选的,指定要安装的角色版本,本例中为1.5.0。

重要:
应当在requirements.yml文件中指定角色版本,特别是生产环境中的playbook。
如果不指定版本,将会获取角色的最新版本。如果作者对角色做出了更改,并与用户的playbook不兼容,这可能会造成自动化失败或其他问题。
           

若要使用角色文件来安装角色,可使用-r REQUIREMENTS-FILE选项:

ansible-galaxy install -r roles/requirements.yml -p roles
           

用户可以使用ansible-galaxy来安装不在Ansible Galaxy中的角色。可以在私有的Git存储库或Web服务器上托管自有的专用或内部角色。下例演示了如何利用各种远程来源配置要求文件。

[[email protected] project]# cat roles/requirements.yml
# from Ansible Galaxy, using the latest version
- src: geerlingguy.redis

# from Ansible Galaxy, overriding the name and using a specific version
- src: geerlingguy.redis
  version: "1.5.0"
  name: redis_prod
  
# from any Git-based repository, using HTTPS
- src: https://gitlab.com/guardianproject-ops/ansible-nginx-acme.git
  scm: git
  version: 56e00a54
  name: nginx-acme
  
# from any Git-based repository, using SSH
- src: [email protected]:guardianproject-ops/ansible-nginx-acme.git
  scm: git
  version: master
  name: nginx-acme-ssh
  
# from a role tar ball, given a URL
# supports 'http', 'https', or 'file' protocols
- src: file:///opt/local/roles/myrole.tar
  name: myrole
           

src关键字指定Ansible Galaxy角色名称。如果角色没有托管在Ansible Galaxy中,则src关键字将指明角色的URL。

如果角色托管在来源控制存储库中,则需要使用scm属性。ansible-galaxy命令能够从基于git或mercurial的软件存储库下载和安装角色。基于Git的存储库要求scm值为git,而托管在Mercurial存储库中的角色则要求值为hg。如果角色托管在Ansible Galaxy中,或者以tar存档形式托管在Web服务器上,则省略scm关键字。

name关键字用于覆盖角色的本地名称。version关键字用于指定角色的版本。version关键字可以是与严自角色的软件存储库的分支、标记或提交哈希对应的任何值。

若要安装与playbook项目关联的角色,可执行ansible-galaxy install命令:

  • 示例:
[[email protected] project]# vim requirements.yml 
[[email protected] project]# cat requirements.yml 
- src: https://github.com/geerlingguy/ansible-role-nginx.git
  scm: git
  version: master
  name: geerlingguy.nginx
[[email protected] project]# ansible-galaxy role install -r requirements.yml -p /opt/project/playbook/roles/
- extracting geerlingguy.nginx to /opt/project/playbook/roles/geerlingguy.nginx
- geerlingguy.nginx (master) was installed successfully
[[email protected] project]# ls /opt/project/playbook/roles/
geerlingguy.nginx  httpd  nginx
[[email protected] project]# 

           

3.管理下载的角色

ansible-galaxy命令也可管理本地的角色,如位于playbook项目的roles目录中的角色。ansible-galaxy list子命令列出本地找到的角色。

ansible-galaxy list
           

可以使用ansible-galaxy remove子命令本地删除角色。

ansible-galaxy remove nginx-acme-ssh
ansible-galaxy list
           

在playbook中使用下载并安装的角色的方式与任何其他角色都一样。在roles部分中利用其下载的角色名称来加以引用。如果角色不在项目的roles目录中,则将检查roles_path来查看角色是否安装在了其中一个目录中,将使用第一个匹配项。以下use-role.ymlplaybook引用了redis_prod和geerlingguy.redis角色:

[[email protected] project]# cat use-role.yml
---
- name: use redis_prod for prod machines
  hosts: redis_prod_servers
  remote_user: devops
  become: True
  roles:
    - redis_prod

- name: use geerlingguy.redis for Dev machines
  hosts: redis_dev_servers
  remote_user: devops
  become: True
  roles:
    - geerlingguy.redis
           

此playbook使不同版本的geerlingguy.redis角色应用到生产和开发服务器。借助这种方式可以对角色更改进行系统化测试和集成,然后再部署到生产服务器上。如果角色的近期更改造成了问题,则借助版本控制来开发角色,就能回滚到过去某一个稳定的角色版本。

继续阅读