Ansible deployment role for managing large projects using Ansible galaxy

brief introduction

  1. Ansible Galaxy (official website: https://galaxy.ansible.com )Is a public repository of ansible content written by many ansible administrators and users.

  2. It contains thousands of Ansible roles and has a searchable database to help Ansible users identify roles that may help them complete management tasks

  3. Ansible Galaxy contains documentation and video links for new ansible users and role developers

Get Ansible galaxy help

  1. You can access the page describing how to use Ansible Galaxy through the documentaion tab on the home page of Ansible Galaxy website

  2. It contains information on how to download and use roles from the Ansible Galaxy. This page also provides instructions on how to develop roles and upload them to Ansible Galaxy.

Browse for roles in the Ansible galaxy

  1. Through the Search tab on the left side of the Ansible Galaxy website home page, users can access information about roles published on Ansible Galaxy

  2. Users can use tags to search for Ansible roles by role name or by other role attributes.

  3. The results are arranged in descending order according to the Best Match score, which is calculated according to the character quality, character popularity and search criteria.

Ansible Galaxy command line tool

1. Search for roles from the command line

  1. The Ansible Galaxy search subcommand searches for roles in Ansible Galaxy

  2. If a string is specified as a parameter, it can be used to search for roles in Ansible Galaxy by keyword

  3. Users can narrow the search results using the – author, – platforms, and – Galaxy tags options

  4. For example, the command ansible Galaxy search -- author gearlingguy will display all roles submitted by user gearlingguy

[root@localhost ~]# ansible-galaxy search 'redis' --author geerlingguy
 
Found 3 roles matching your search:
 
 Name                  Description
 ----                  -----------
 geerlingguy.php-pecl  PHP PECL extension installation.
 geerlingguy.php-redis PhpRedis support for Linux
 geerlingguy.redis     Redis for Linux
  1. The ansible Galaxy info subcommand displays more detailed information about the role

  2. Ansible Galaxy obtains this information from multiple locations, including meta / main YML file and its GigHub repository

[root@localhost ~]# ansible-galaxy info geerlingguy.redis

The above command is used to display the geerlingguy provided by the Ansible Galaxy Redis role information.

Installing roles from Ansibel galaxy

  1. The Ansible Galaxy install subcommand downloads the role from the Ansible Galaxy and installs it locally on the control node

  2. By default, roles are installed to the user's roles_ In the first writable directory under path. According to the default roles set for ansible_ Path, the role will usually be installed to the user's ~ / ansible/roles directory

  3. Default roles_path may be by the user's current Ansible configuration file or environment variable ANSIBLE_ROLES_PATH override, which will affect the behavior of Ansible galaxy

  4. Users can install roles by specifying a specific directory by using the - p DIRECTORY option

[root@localhost ~]# ls project/
playbook.yaml
[root@localhost ~]# ansible-galaxy install robertdebock. Httpd - P project / / / - P specify the installation directory
- downloading role 'httpd', owned by robertdebock
- downloading role from https://github.com/robertdebock/ansible-role-httpd/archive/5.3.0.tar.gz
- extracting robertdebock.httpd to /root/project/robertdebock.httpd
- robertdebock.httpd (5.3.0) was installed successfully
[root@localhost ~]# ls project/
playbook.yaml  

Install roles using required installation files

You can use ansible galaxy to install a role list according to the definition in a text file

For example, if a user's playbook needs to install a specific role, you need to create a roles / requirements. Net in the project directory YML file to specify the required roles

The src attribute specifies the source of the role. In this case, it is Robert dobock from the Ansible Galaxy Httpd role

  • Important:

    It should be in requirements Specify the role version in the YML file, especially the playbook in the production environment
    If no version is specified, the latest version of the role will be obtained. If the author makes changes to the role and is incompatible with the user's playbook, this may cause automation failures or other problems.

To install roles using a role file, use the - R requirements file option

[root@localhost project]# cat roles/requirements.yml
---
- src: robertdebock.httpd
[root@localhost project]# ansible-galaxy install -r roles/requirements.yml -p ./
- downloading role 'httpd', owned by robertdebock
- downloading role from https://github.com/robertdebock/ansible-role-httpd/archive/5.3.0.tar.gz
- extracting robertdebock.httpd to /root/project/robertdebock.httpd
- robertdebock.httpd (5.3.0) was installed successfully
[root@localhost project]# ls
playbook.yaml  robertdebock.httpd  roles

Users can use Ansible Galaxy to install roles that are not in Ansible Galaxy. You can host your own private or internal roles on a private Git repository or Web server.

[root@localhost project]# cat roles/requirements.yml
---
- src: geerlingguy.redis
 
- src: geerlingguy.redis
  version: "1.5.0"
  name: redis_prod
   
- src: https://gitlab.com/guardianproject-ops/ansible-nginx-acme.git
  scm: git
  version: 56e00a54
  name: nginx-acme
   
- src: git@gitlab.com:guardianproject-ops/ansible-nginx-acme.git
  scm: git
  version: master
  name: nginx-acme-ssh
   
- src: file:///opt/local/roles/myrole.tar
  name: myrole

src keyword specifies the Ansible Galaxy role name. If the role is not hosted in the Ansible Galaxy, the src keyword indicates the URL of the role.

Manage download roles

The ansible Galaxy command can also manage local roles, such as those located in the roles directory of the playbook project. The ansible Galaxy list subcommand lists locally found roles

[root@ansible ~]# ansible-galaxy list
# /usr/share/ansible/roles
- linux-system-roles.kdump, (unknown version)
- linux-system-roles.network, (unknown version)
- linux-system-roles.postfix, (unknown version)
- linux-system-roles.selinux, (unknown version)
- linux-system-roles.storage, (unknown version)
- linux-system-roles.timesync, (unknown version)
- rhel-system-roles.kdump, (unknown version)
- rhel-system-roles.network, (unknown version)
- rhel-system-roles.postfix, (unknown version)
- rhel-system-roles.selinux, (unknown version)
- rhel-system-roles.storage, (unknown version)
- rhel-system-roles.timesync, (unknown version)
# /etc/ansible/roles
[WARNING]: - the configured path /root/.ansible/roles does not exist.

You can delete roles locally using the ansible Galaxy remove subcommand.

ansible-galaxy remove + Role name

The downloaded and installed roles are used in the same way as any other role in the playbook

stay roles Section with the name of the role it downloaded. If the role is not in the project roles Directory, the roles_path To see if the role is installed in one of the directories, the first match will be used

Keywords: Linux ansible

Added by tomasd on Sun, 26 Dec 2021 12:26:49 +0200