How to create a deploy token for Ansible deployment¶
For several applications, Ansible is used to deploy them to the on-premise infrastructure. For example, Ansible is used most heavily in Grails application ansible deployment project.
This document describes the steps needed to create new or update expired deploy tokens.
Deploy tokens are used to enable authentication of deployment tasks, independent of a user account. With a deploy token, Ansible deployment can:
- Clone Git repositories.
- Pull from a GitLab container registry.
- Pull from a GitLab package registry.
Create a token in the the GitLab project¶
The Ansible deployments use GitLab deploy tokens, encrypted and stored in each project's Ansible Vault. To create a project's deploy token:
- Open the project in GitLab UI.
- Go to Settings > Repository.
- Expand the "Deploy Tokens" section.
- Click the "Add token" button.
-
Fill the following fields as shown below:
- Name: Token should be named "Ansible Deployment".
- Expiration Date (optional): Could be omitted. By default, a deploy token does not expire.
- Username: Set username to
ansible-<PROJECT-NAME>
, wherePROJECT-NAME
is a project's slug (gaobase, rgea, etc).
-
Give
read_repository
,read_registry
andread_package_registry
permissions. - Copy username and token, they will be used in the next step.
Encrypt the token and udpate the Ansible project¶
- Clone the git project locally.
-
Create a new branch:
git checkout -b upgrade-deploy-token
-
Prepare environment. Filenames vary across project, but in common scenario the preparation process is as shown:
eval $(op signin) source <APP>-<ENV>-setup
where
APP
is an application's slug,ENV
is the environment name (live, test, etc.). -
Ecrypt the username and token.
DEPLOY_USERNAME='PUT USERNAME HERE' DEPLOY_TOKEN='PUT TOKEN HERE' ansible-vault encrypt_string $DEPLOY_USERNAME --vault-password-file secrets/open-vault --name=gitlab_access_user ansible-vault encrypt_string $DEPLOY_TOKEN --vault-password-file secrets/open-vault --name=gitlab_access_token
-
Copy the output from
ansible-vault
to thegroup_vars/<APP>-<ENV>-secrets.yml
, whereAPP
is an application's slug,ENV
is the environment name. -
Add the file to the git and push:
git add group_vars/<APP>-<ENV>-secrets.yml git commit -m "chore: Update deploy token for <APP>" git push --set-upstream origin upgrade-deploy-token
-
Submit a new merge request for review.