Ansible Galaxy 实践

Ansible Galaxy 初识

Ansible Galaxy 是 Ansible 官方 Roles 资源库(galaxy.ansible.com),在 Galaxy 平台上所有人可以分享 ansible 功能模块

为什么要用 ansible-galaxy

作为一个入门的运维人员,你可以从一些 palybook 开始编写你的自动化项目。但是随着你对 Ansilble playbook 的增加,以及你对 Ansible Role 的需求,你会进一步意识到,使用 Ansible Galaxy 变得非常有价值。Ansible Galaxy 作为 Ansible Roles 的资源库,可以直接放到你的 playbook 中来简化你的自动化项目。

ansible-galaxy 命令的使用

[root@localhost root]# ansible-galaxy --help
Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...

Options:
  -h, --help            show this help message and exit
  -c, --ignore-certs    Ignore SSL certificate validation errors.
  -s API_SERVER, --server=API_SERVER
                        The API server destination
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)
  --version             show program's version number and exit

 See 'ansible-galaxy <command> --help' for more information on a specific command.

这里有一些有用的 ansiber -galaxy 命令,你可能会经常使用:

ansible-galaxy list 显示已安装 Roles 的列表和版本号

ansible-galaxy remove <role> 删除已安装的角色

ansible-galaxy install 安装 Ansible Galaxy 资源库中的角色.

ansible-galaxy init 初始化 Role 角色模板

搜索 role

[root@localhost root]# ansible-galaxy search helm
Found 54 roles matching your search:

 Name                                                   Description
 ----                                                   -----------
 aalaesar.helm_release                                  Create, update, upgrade an delete Helm releases using Ansible and the Helm CLI
 abdennour.kube_local_environment                       Installs kubernetes utilities for DevOps environment
 acikogun.winit                                         An extensible installer for tools listed below. ansible awscli azurecli cloudsdk docker docker-compose eksctl go helm java8 java11 node packer terraform vagrant
 afonsog.k8s_helm                                       Install helm on master nodes & tiller on k8s-cluster
 afpacket.devops_tools                                  Ansible role for installing DevOps tools
 alvarobacelar.ansible_role_helm_istio                  Role de instalação do helm e istio
 andrewrothstein.kubernetes-helm                        installs kubernetes helm
 ansible-ThoTeam.nexus3-oss                             Nexus Repository Manager 3.x (Sonatype)
 Anthony25.kubernetes-keel                              Install Keel on Kubernetes
 antongorkovenko.k8s                                    Kubernetes: setup master and worker nodes
...

安装 role

ansible-galaxy install dodas.helm
[root@localhost root]# ansible-galaxy install dodas.helm
- downloading role 'helm', owned by dodas
- downloading role from https://github.com/DODAS-TS/ansible-role-helm/archive/v2.0.0-rc5.tar.gz
- extracting dodas.helm to <path>/roles/dodas.helm
- dodas.helm (v2.0.0-rc5) was installed successfully

根据输出提示,安装的 role 放在相应 Ansible 目录中

查看这个 role 目录结构:

[root@localhost root]# tree
.
├── defaults
│   └── main.yml
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── helm.yml
│   └── main.yml
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

6 directories, 9 files

之后,你可以在你的 playbook 中使用该 role

新建 role

你也可以使用 Ansible -galaxy init 初始化一个新的 galaxy 角色

ansible-galaxy init vivaserver.lamp

初始化后的文件目录和上文的 role 目录结构类似, 你可以在task/main.yml 这个 role 的入口文件中,编写各样的 tasks,并结合 templates 中 jinjia2 模块文件等来丰富你要创建的 role 等

实践详谈

Galaxy 可以使用 git 添加角色源,比如 GitHub。 因此在公司的实际使用中,通常会实现一些 Ansbile common Roles 以满足特定的需求。将定义 Ansible Roles 代码放在 Git 仓库中作为公共模块使用,可以更加方便地加载到其他 repo 中, 更好地实践 gitops 和 IaC(infrastructure as code) 理念

例如,在我创建 Kubernetes,Prometheus 等(IaC)项目库里,会常使用 Helm 或者 terraform 这些 role 来部署 AWS 或者 K8S 等相关资源

Ansible 目录结构如下

├── playbooks
│   ├── configuration.yml
│   ├── kubernetes.yml
│   ├── network.yml
├── requirements.yml
├── roles
└── tasks
    ├── aws_get_resources.yml

首先,在 requirements.yml 里定义:

- name: terraform
  src: https://github.com/solutionDrive/ansible-role-terraform/archive/v1.2.0.tar.gz
- name: helm 
  src: [email protected]:<github>/ansible-role-helm.git
  scm: git 
  version: 1.0.5

并在 ansible.cfg 中指定 roles 安装路径

roles_path = ansible/roles

使用如下命令安装 roles 模块到指定的路径

$ ansible-galaxy install -r requirements.yml

安装后,Ansible 目录结构大概如下

├── playbooks
│   ├── configuration.yml
│   ├── kubernetes.yml
│   ├── network.yml
├── requirements.yml
├── roles
│   ├── helm
│   └── terraform
└── tasks
    ├── aws_get_resources.yml

接着,在某个 playbook.yml 可以这么引用 Role,来使用 helm deploy K8S 资源

  tasks:
    - name: init Helm
      import_role:
        name: helm

最后 使用ansible-playbook 来部署

ansible-playbook ansible/playbooks/kubernetes.yml 

总结

本文介绍了 Ansible Galaxy 命令的使用,并详谈了在实际工作中你可以如何应用 Galaxy 创建的 role 到你的项目中。使用 Ansible Galaxy 来创造 Role 是一种极好的想法,也是一种理想的方式来组织和管理你不断增长的 playbook。通过简单、强大、无代理、快速有效地扩展基础设施的自动化解决方案,让自己成为更高阶的运维人员。

猜你喜欢

转载自blog.csdn.net/dongshi_89757/article/details/111998038