使用ansible-galaxy部署角色

梁福
2023-12-01

1. ansible galaxy的介绍

Ansible Galaxy是一个Ansible内容公共资源库,这些内容由许许多多Ansible管理员和用户编写, Ansible Galaxy的官方网站是:ansible的官网

2. Ansible Galaxy命令的使用

2.1 使用Ansible Galaxy命令创建角色目录

// 下面两条命令都可以创建角色.
[root@node1 roles]# ansible-galaxy role init httpd  这个种创建角色的方式是面向对象的
- Role httpd was created successfully

[root@node1 roles]# ansible-galaxy init httpd

2.2 使用Ansible Galaxy命令查看角色

默认查看/ansible/roles目录下的角色
[root@node1 roles]# ansible-galaxy list
# /ansible/roles
- now_role, (unknown version)
- selinux, (unknown version)
还可以使用下面的命令查看指定目录下的角色
[root@node1 roles]# ansible-galaxy list -p /opt/playbook/roles
# /opt/playbook/roles
- timesync, (unknown version)
- selinux, (unknown version)
- httpd, (unknown version)
# /ansible/roles
- now_role, (unknown version)
- selinux, (unknown version)
这里为什么是未知的版本,这是因为是我们用ansible-galaxy命令创建的角色所以没有版本信息.

2.3 以命令的方式从galaxy的库中搜索角色.

// 使用search命令可以根据版本,平台,作者以及关键字.

这里我们根据httpd这个关键字来进行查找.
[root@node1 roles]# ansible-galaxy role search httpd


Found 329 roles matching your search:

 Name                                                          Description
 ----                                                          -----------
 2kloc.trellis-monit                                           Install and c>
 aarunmichael.base_httpd                                       your role des>
 a_arvind_k.base_httpd                                         your role des>
 abdelmouhssine.hardening_cis_apache_http_server_2_4_benchmark CLOUD APPLICA>
 abdusamy.web_server_role                                      Deploy http v>
 acandid.httpd                                                 Install httpd>
 acropia.httpd                                      

2. 4 使用Ansible Galaxy命令导入一个角色

--role-name后面跟上用户名 导入到仓库的角色名字随意
[root@node1 roles]# ansible-galaxy role import --role-name

2. 5 使用Ansible Galaxy命令查看角色的详细信息

[root@node1 roles]# ansible-galaxy info robertdebock.httpd

Role: robertdebock.httpd
        description: Install and configure httpd on your system.
        active: True
        commit: 9fc0e5c1f38873f26c1d896d7db1424b2e07181b
        commit_message: 404 is also good.
        commit_url: https://api.github.com/repos/robertdebock/ansible-role-h>
        company: none
        created: 2017-11-10T16:04:25.981866Z
        download_count: 141048
        forks_count: 11
        github_branch: master

2.6 使用Ansible Galaxy命令安装角色

安装角色从文件,URL或ansiblegalaxy进行安装
角色默认存放在/ansible/roles目录下,若想安装在指定的目录下可以使用-p选项,并且可以重新命名,名字自己定义.

[root@node1 roles]# ansible-galaxy role install robertdebock.httpd
- downloading role 'httpd', owned by robertdebock
- downloading role from https://github.com/robertdebock/ansible-role-httpd/archive/7.0.0.tar.gz
- extracting robertdebock.httpd to /ansible/roles/robertdebock.httpd
- robertdebock.httpd (7.0.0) was installed successfully

2.7 使用Ansible Galaxy命令移除安装的角色

[root@node1 roles]# ls
all.yml  now_role  robertdebock.httpd  selinux  selinux.yml
[root@node1 roles]# ansible-galaxy role remove robertdebock.httpd
- successfully removed robertdebock.httpd
[root@node1 roles]# ls
all.yml  now_role  selinux  selinux.yml

2.8 使用Ansible Galaxy命令移除自己创建的角色

需要指定移除角色的路径
[root@node1 roles]# pwd
/opt/playbook/roles
[root@node1 roles]# ansible-galaxy role remove httpd -p ./
- successfully removed httpd

2.9 使用Ansible Galaxy命令查看指定角色

[root@node1 roles]# ansible-galaxy role list robertdebock.httpd
# /ansible/roles
- robertdebock.httpd, 7.0.0

2.9.1 使用yml文件安装指定的角色

重要提醒最好在安装角色的文件中指定角色的版本,因为在实际的生产环境中,若不指定版本就默认是最新版本,如果角色的作者对角色的环境做了更改,就会产生与用户的playbook冲突的不兼容问题.这就可能导致失败报错.

使用 -r 参数指定文件,安装角色.
[root@node1 ansible]# vim merole.yml
[root@node1 ansible]# ansible-galaxy role install -r merole.yml 
- downloading role 'httpd', owned by robertdebock
- downloading role from https://github.com/robertdebock/ansible-role-httpd/archive/7.0.0.tar.gz
- extracting robertdebock.httpd to /ansible/roles/robertdebock.httpd
- robertdebock.httpd (7.0.0) was installed successfully
[root@node1 ansible]# cat merole.yml 
- src: robertdebock.httpd

可以安装多个角色

[root@node1 ansible]# ansible-galaxy role install -r merole.yml --force
- changing role robertdebock.httpd from 7.0.0 to unspecified
- downloading role 'httpd', owned by robertdebock
- downloading role from https://github.com/robertdebock/ansible-role-httpd/archive/7.0.0.tar.gz
- extracting robertdebock.httpd to /ansible/roles/robertdebock.httpd
- robertdebock.httpd (7.0.0) was installed successfully
- downloading role 'epel', owned by robertdebock
- downloading role from https://github.com/robertdebock/ansible-role-epel/archive/3.1.0.tar.gz
- extracting robertdebock.epel to /ansible/roles/robertdebock.epel
- robertdebock.epel (3.1.0) was installed successfully

[root@node1 ansible]# cat merole.yml 
- src: robertdebock.httpd
- src: robertdebock.epel

2.10 使用Ansible Galaxy命令查看角色的平台

[root@node1 roles]# ansible-galaxy role search robertdebock.httpd --platform Fedora 

Found 10 roles matching your search:

 Name                       Description
 ----                       -----------
 robertdebock.ca            Install and configure a certificate authority on>
 robertdebock.certbot       Install and configure certbot on your system.
 robertdebock.haproxy       Install and configure haproxy on your system.
 robertdebock.httpd         Install and configure httpd on your system.
 robertdebock.mediawiki     Install and configure mediawiki on your system.
 robertdebock.moodle        Install and configure moodle on your system.
 robertdebock.php           Install and configure php on your system.
 类似资料: