ansible galaxy的应用

欧阳俊逸
2023-12-01

使用ansible galaxy

Ansible Galaxy命令行工具

  • 使用ansible-galaxy role init 创建角色结构:
[root@192 roles]# ansible-galaxy role init nginx

[root@192 nginx]# tree
.
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

  • 使用ansible-galaxy list 列出角色:
[root@192 roles]# ansible-galaxy list
# /usr/share/ansible/roles
- linux-system-roles.certificate, (unknown version)
- linux-system-roles.crypto_policies, (unknown version)
- linux-system-roles.ha_cluster, (unknown version)
- linux-system-roles.kdump, (unknown version)
- linux-system-roles.kernel_settings, (unknown version)
- linux-system-roles.logging, (unknown version)
- linux-system-roles.metrics, (unknown version)
- linux-system-roles.nbde_client, (unknown version)
- linux-system-roles.nbde_server, (unknown version)
- linux-system-roles.network, (unknown version)
- linux-system-roles.postfix, (unknown version)
- linux-system-roles.selinux, (unknown version)
- linux-system-roles.ssh, (unknown version)
- linux-system-roles.sshd, (unknown version)
- linux-system-roles.storage, (unknown version)
- linux-system-roles.timesync, (unknown version)
- linux-system-roles.tlog, (unknown version)
- rhel-system-roles.certificate, (unknown version)
- rhel-system-roles.crypto_policies, (unknown version)
- rhel-system-roles.ha_cluster, (unknown version)
- rhel-system-roles.kdump, (unknown version)
- rhel-system-roles.kernel_settings, (unknown version)
- rhel-system-roles.logging, (unknown version)
- rhel-system-roles.metrics, (unknown version)
- rhel-system-roles.nbde_client, (unknown version)
- rhel-system-roles.nbde_server, (unknown version)
- rhel-system-roles.network, (unknown version)
- rhel-system-roles.postfix, (unknown version)
- rhel-system-roles.selinux, (unknown version)
- rhel-system-roles.ssh, (unknown version)
- rhel-system-roles.sshd, (unknown version)
- rhel-system-roles.storage, (unknown version)
- rhel-system-roles.timesync, (unknown version)
- rhel-system-roles.tlog, (unknown version)
# /etc/ansible/roles

  • 使用ansible-galaxy role search 搜索角色:
[root@192 roles]# ansible-galaxy role search httpd
//列出所有的httpd角色后,可以搜索自己想要搜索的作者
/robertdebock
 robertdebock.ca                                               Install and configure a cer>
 robertdebock.certbot                                          Install and configure certb>
 robertdebock.haproxy                                          Install and configure hapro>
 robertdebock.httpd                                            Install and configure httpd>
 robertdebock.mediawiki                                        Install and configure media>
 robertdebock.moodle                                           Install and configure moodl>
 robertdebock.nextcloud                                        Install and configure Nextc>
 robertdebock.owncloud                                         Install and configure owncl>
 robertdebock.php                                              Install and configure php o>
 robertdebock.php_fpm                                          The purpose of this role is>
 robertdebock.phpmyadmin                                       Install and configure phpmy>
 robertdebock.roundcubemail                                    Install and configure round>
 robertdebock.users                                            The purpose of this role is>
 robertdebock.zabbix_web                                       Install and configure zabbi>

  • 使用ansible-galaxy role info 查看角色信息:
[root@192 roles]# ansible-galaxy role info robertdebock.httpd

  • 使用ansible-galaxy role install 安装角色:
[root@192 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 /root/.ansible/roles/robertdebock.httpd
- robertdebock.httpd (7.0.0) was installed successfully
//不指定路径会安装在/root/.ansible/roles/
  • 使用ansible-galaxy role remove 删除已安装的角色(使用remove删除的时候只能删除已安装的):
[root@192 roles]# ansible-galaxy role remove  robertdebock.httpd
- successfully removed robertdebock.httpd

  • 使用ansible-galaxy role install (角色名字) -p +指定的路径, 安装角色并指定路径:
//指定路径,安装在当前目录
[root@192 roles]# ansible-galaxy role install  robertdebock.httpd -p .

  • 使用ansible-galaxy role search +角色名 – platform + 平台名:
[root@192 ~]# ansible-galaxy role search robertdebock.httpd --platform Fedora
- 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 /root/.ansible/roles/robertdebock.httpd
- robertdebock.httpd (7.0.0) was installed successfully
[root@192 ~]# 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 your system.
 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.
 robertdebock.php_fpm       The purpose of this role is to install and configure php_fpm o>
 robertdebock.phpmyadmin    Install and configure phpmyadmin on your system.
 robertdebock.roundcubemail Install and configure roundcubemail on your system.
(END)

使用文件安装角色

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

//创建角色文件
[root@192 ~]# vim requirements.yml 
- src: robertdebock.httpd

//安装角色
[root@192 ~]# ansible-galaxy role install -r requirements.yml 

使用要求文件安装角色

可以使用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不兼容,这可能会造成自动化失败或其他问题。

 类似资料: