Skip to content

16. ansible-galaxy

介绍

什么是ansible-galaxy

Ansible 的 Galaxy 工具,类似程序员使用的 github,docker 镜像仓库,yum仓库和deb仓库等。
可以将自己编写的 Role 通过 Galaxy 这个平台进行分享。
同样,我们也可以通过 Galaxy 这个平台去获取一些我们想要的 Role

官方网站

ansible-galaxy

帮助信息

# ansible-galaxy -h
usage: ansible-galaxy [-h] [--version] [-v] TYPE ...

Perform various Role and Collection related operations.

positional arguments:
  TYPE
    collection   Manage an Ansible Galaxy collection.
    role         Manage an Ansible Galaxy role.

optional arguments:
  --version      show program's version number, config file location, configured module search path, module location, executable location and exit
  -h, --help     show this help message and exit
  -v, --verbose  Causes Ansible to print more debug messages. Adding multiple -v will increase the verbosity, the builtin plugins currently evaluate up to -vvvvvv. A reasonable level
                 to start is -vvv, connection debugging might require -vvvv.
# ansible-galaxy role -h
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 into a galaxy server
    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
# ansible-galaxy collection -h
usage: ansible-galaxy collection [-h] COLLECTION_ACTION ...

positional arguments:
  COLLECTION_ACTION
    download         Download collections and their dependencies as a tarball for an offline install.
    init             Initialize new collection with the base structure of a collection.
    build            Build an Ansible collection artifact that can be published to Ansible Galaxy.
    publish          Publish a collection artifact to Ansible Galaxy.
    install          Install collection(s) from file(s), URL(s) or Ansible Galaxy
    list             Show the name and version of each collection installed in the collections_path.
    verify           Compare checksums with the collection(s) found on the server and the installed copy. This does not verify dependencies.

optional arguments:
  -h, --help         show this help message and exit

collection和role的关系

collection是roles的集合,比如我们可以把很多相关的role定义成collection。

常用指令

# ansible-galaxy search nginx

Found 1811 roles matching your search. Showing first 1000.

 Name                                                       Description
 ----                                                       -----------
 0x0i.prometheus                                            Prometheus - a multi-dimensional time-series data monitoring and alerting toolkit
 0x5a17ed.ansible_role_netbox                               Installs and configures NetBox, a DCIM suite, in a production setting.
 1davidmichael.ansible-role-nginx                           Nginx installation for Linux, FreeBSD and OpenBSD.
# ansible-galaxy role init roles/nfs
# ansible-galaxy role init roles/nfs-client
# ansible-galaxy role init roles/vsftpd

案例

## 通过如下的 方式可以快速创建roles的目录结构
## 安装nfs的role创建

# ansible-galaxy role init roles/nfs
- Role roles/nfs was created successfully
# ansible-galaxy role init roles/nfs-client
- Role roles/nfs-client was created successfully
# ansible-galaxy role init roles/vsftpd
- Role roles/vsftpd was created successfully
# tree -LN 3 roles/
roles/
├── nfs
│   ├── defaults
│   │   └── main.yml
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── README.md
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   ├── tests
│   │   ├── inventory
│   │   └── test.yml
│   └── vars
│       └── main.yml
├── nfs-client
│   ├── defaults
│   │   └── main.yml
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── README.md
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   ├── tests
│   │   ├── inventory
│   │   └── test.yml
│   └── vars
│       └── main.yml
└── vsftpd
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── README.md
    ├── tasks
    │   └── main.yml
    ├── templates
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml
# tree
.
├── all.yml                         
├── group_vars                      #全局变量
│   └── all.yml
└── roles
## 参考命令
# ansible-playbook -b -i ../hosts.ini --syntax-check all.yml   # 语法检测
# ansible-playbook -b -i ../hosts.ini  all.yml                                  # 执行


-b, --become          run operations with become (does not imply password prompting)
-i INVENTORY, --inventory INVENTORY, --inventory-file INVENTORY specify inventory host path or comma separated host list. --inventory-file is deprecated