How to Create a Custom Access Token in TeamCity

TeamCity provides a UI for creating access tokens with project-specific permissions. However, the UI does not expose every permission available in the TeamCity permission model (based on my experience and setup at least)

I faced this issue when trying to create an access token that could customize build revisions (VCS revisions) in a specific project.

The standard token creation form allows selecting permissions from the Build category, but the Customize build revisions permission is missing from the list, although the user has roles with required rights.

The standard Build permissions include options such as:

  • Change build status
  • Clean build configuration sources
  • Comment build
  • Manually label / merge build sources
  • Mute / unmute problems and tests in project
  • Pause / activate build configuration
  • Pin / unpin build
  • Remove finished build
  • Reorder builds in queue
  • Run build
  • Stop / remove from queue

    As a result, using a token without this permission leads to the following error when attempting to customize a build revision:
HTTP 403 Forbidden
You do not have "Customize build revisions" permission
in project with internal id: project1234

The permission actually exists in TeamCity. The solution is to use the TeamCity REST API to retrieve its ID and explicitly include it when creating a custom access token.

This guide is based on TeamCity 2025.11.8.

Find the TeamCity REST API endpoint

To work around this limitation, we can use the TeamCity REST API.

The endpoint for creating user access tokens is:

POST /app/rest/users/{userLocator}/tokens

For example:

https://TEAMCITY_SERVER/app/rest/users/username:build_user/tokens

You can also inspect the REST API specification available on your TeamCity server:

https://TEAMCITY_SERVER/app/rest/swagger.json

Retrieve all available permissions

Before creating the token, we need to find the internal permission ID.

Use the following request to retrieve permissions assigned to the built-in SYSTEM_ADMIN role:

curl -s \
-H "Authorization: Bearer USER_TOKEN" \
-H "Accept: application/xml" \
"https://TEAMCITY_SERVER/app/rest/roles/id:SYSTEM_ADMIN?fields=permissions(permission(id,name,global))"

The response contains permission IDs, display names, and their scope.

Among the returned permissions, we can find:

<permission id="customize_build_parameters"
name="Customize build parameters"
global="false"/>
<permission id="customize_build_revisions"
name="Customize build revisions"
global="false"/>

This confirms that customize_build_revisions exists in the TeamCity permission model, even though it is missing from the standard Build permissions list in the token creation UI.

The global attribute indicates the permission scope:

  • global="false" — project-level permission.
  • global="true" — global permission.

For our use case, customize_build_revisions is a project-level permission, so we can explicitly associate it with the target project in the token definition.

Create a custom token

Now we can create a token with the required permissions.

Create a file named token.xml:

<token name="myproject-custom-token"
expirationTime="2027-11-27T10:38:59.902+03:00">
<permissionRestrictions>
<permissionRestriction isGlobalScope="false">
<project id="myproject"/>
<permission id="run_build"/>
</permissionRestriction>
<permissionRestriction isGlobalScope="false">
<project id="myproject"/>
<permission id="view_project"/>
</permissionRestriction>
<permissionRestriction isGlobalScope="false">
<project id="myproject"/>
<permission id="view_build_configuration_settings"/>
</permissionRestriction>
<permissionRestriction isGlobalScope="false">
<project id="myproject"/>
<permission id="customize_build_revisions"/>
</permissionRestriction>
</permissionRestrictions>
</token>

The important part is the explicit permission restriction:

<permissionRestriction isGlobalScope="false">
<project id="myproject"/>
<permission id="customize_build_revisions"/>
</permissionRestriction>

Unlike the UI, the XML request allows us to specify the permission ID directly.

Each restriction defines:

  • project — the target TeamCity project ID.
  • permission — the internal permission ID.
  • isGlobalScope="false" — limits the permission to the specified project.

The expiration date uses ISO 8601 format, including the timezone offset.

Send the XML file to TeamCity:

curl -i -X POST \
"https://TEAMCITY_SERVER/app/rest/users/username:build_user/tokens" \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/xml" \
-H "Accept: application/xml" \
--data-binary @token.xml

Replace:

  • TEAMCITY_SERVER with your TeamCity server address.
  • build_user with the target user’s login.
  • USER_TOKEN with an existing token authorized to create tokens for that user.

If the request succeeds, TeamCity creates a new access token with the specified restrictions.

Verify the token permissions

After creating the token, verify that TeamCity saved all restrictions:

curl -s \
-H "Authorization: Bearer USER_TOKEN" \
-H "Accept: application/xml" \
"https://TEAMCITY_SERVER/app/rest/users/username:build_user/tokens?fields=token(name,expirationTime,permissionRestrictions(permissionRestriction(project(id),permission(id),isGlobalScope)))"

Check that the response contains:

<permissionRestriction isGlobalScope="false">
<project id="myproject"/>
<permission id="customize_build_revisions"/>
</permissionRestriction>

Also verify that the token has the expected expiration date and that all permissions are associated with the correct project.

Finally, test the token by attempting to run a build with a custom VCS revision.

Sync Files Between Linux Servers with Ansible

Keeping files synchronized between Linux servers is a common task. You may want to copy application configuration, NGINX settings, SSL certificates, or other files from one server to another to make both look identical.

Ansible provides several ways to copy files, but for server-to-server synchronization, ansible.posix.synchronize is usually the best choice. It uses rsync, so only changed files are transferred, making it much faster than copying everything every time.

The playbook performs four steps:

  • Check whether the target server already has an SSH key.
  • Upload the SSH key if it is missing.
  • Allow the target server to connect to the source server.
  • Synchronize the required directories or files.

Prerequisites: network connection between servers, configured sudoers , generated a pair of ssh keys, defined variables used in the playbook

Step 1 – Check whether an SSH key already exists

Before uploading a key, the playbook checks whether one is already present.

- name: Check if id_rsa key already exists (target)
ansible.builtin.stat:
path: ~/.ssh/id_rsa
register: id_rsa
become: true
become_user: "{{ target_host_user }}"
when: ('target_host' in inventory_hostname)

The important parts are:

  • register: id_rsa stores the result of the stat module so later tasks can check whether the file exists.
  • become_user: "{{ target_host_user }}" runs the task as the application user instead of root. This checks the correct home directory.
  • when ensures this task runs only on the target server.

Step 2 – Upload the SSH key

If the key does not exist, create the .ssh directory and upload both the private and public keys.

- name: Set target ssh
when: ('target_host' in inventory_hostname) and (not id_rsa.stat.exists)
become: true
become_user: "{{ target_host_user }}"
block:
- name: Set target ssh | Create directories for ssh (target)
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: "{{ target_host_user }}"
group: "{{ target_host_user }}"
mode: "0700"
loop:
- ~/.ssh
- name: Set target ssh | Upload keys to ssh dir
no_log: true
ansible.builtin.copy:
dest: "~/.ssh/{{ item.file }}"
content: "{{ item.content }}"
mode: "{{ item.mode }}"
loop:
- { file: "id_rsa", content: "{{ target_host_private_key }}", mode: "0600" }
- { file: "id_rsa.pub", content: "{{ target_host_public_key }}", mode: "0644" }

The interesting options are:

  • block groups related tasks under a single when condition.
  • no_log: true prevents the private SSH key from appearing in the Ansible output or logs.
  • loop allows us to interact with multiple files in just a one task
  • The task only runs when id_rsa.stat.exists is false, making the playbook idempotent.

Step 3 – Allow the target server to connect

The source server must trust the target server’s SSH key.

- name: Add target public key to source host
ansible.posix.authorized_key:
user: "{{ source_host_user }}"
key: "{{ target_host_ssh_public_key }}"
state: present
register: auth_key
when: ('source_host' in inventory_hostname)

The authorized_key module adds the target server’s public key to the source server’s authorized_keys file. After this, the target server can connect over SSH without a password.

Step 4 – Synchronize the files

Finally, synchronize the required directories.

- name: Synchronize source and target files (target)
ansible.posix.synchronize:
src: "{{ source_host_user }}@{{ source_host }}:{{ item.src }}"
dest: "{{ item.dest }}"
mode: pull
rsync_path: sudo rsync
rsync_opts:
- "-e 'ssh -i /home/{{ target_host_user }}/.ssh/id_rsa'"
- "--mkpath"
- "--delete"
loop:
- { src: "/etc/nginx/", dest: "/etc/nginx/" }
- { src: "{{ app_dir }}/config/", dest: "{{ app_dir }}/config/" }
- { src: "/home/{{ app_user }}/.somefile", dest: "/home/{{ app_user }}/" }
delegate_to: "{{ inventory_hostname }}"
register: rsync_result
failed_when: rsync_result.rc not in [0, 23]
become: true
when: ('target_host' in inventory_hostname) and (auth_key is defined)

A few options are worth explaining:

  • mode: pull means the target server connects to the source server and downloads the files.
  • src: "{{ source_host_user }}@{{ source_host }}:{{ item.src }}" tells rsync to read the files directly from the remote server over SSH.
  • rsync_path: sudo rsync runs rsync with sudo on the source server, allowing access to protected directories such as /etc/nginx.
  • -e 'ssh -i ...' tells rsync which SSH private key to use.
  • --mkpath creates the destination directory if it does not already exist.
  • --delete removes files that no longer exist on the source server, keeping both servers synchronized.
  • delegate_to: "{{ inventory_hostname }}" makes sure the synchronization runs on the target server. Since we use mode: pull, the target server is responsible for opening the SSH connection.
  • failed_when: rsync_result.rc not in [0, 23] ignores rsync exit code 23, which commonly indicates that a source file or directory does not exist. Depending on your environment, this may be expected and should not fail the playbook.

Push vs Pull

The synchronize module supports two synchronization modes.

  • Push (mode: push) – the source server connects to the destination server and sends the files
  • Pull (mode: pull) – the destination server connects to the source server and downloads the files.

This example uses pull mode because the target server initiates the SSH connection. This is often preferred when the source server should not connect to other machines.

Other ways to copy files

The synchronize module is not the only option.

  • copy – Copies files from the Ansible control node to managed hosts. Best for small files or configuration files.
  • template – Similar to copy, but processes Jinja2 templates before copying.
  • fetch – Copies files from managed hosts back to the Ansible control node.
  • command or shell – Runs rsync directly if you need options that are not supported by the synchronize module.

I hope the post was useful.

Cheers.