# Register Variable from docker-compose container

**URL:** <https://forum.ansible.com/t/register-variable-from-docker-compose-container/5170>\
**Category:** Get Help\
**Created:** [April 26, 2024, 9:47am UTC](https://forum.ansible.com/t/register-variable-from-docker-compose-container/5170 "2024-04-26T09:47:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrickJoris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/brickjoris/32/3010_2.png) [@BrickJoris](https://forum.ansible.com/u/BrickJoris)\
**Post date:** [April 26, 2024, 9:47am UTC](https://forum.ansible.com/t/register-variable-from-docker-compose-container/5170/1 "2024-04-26T09:47:04Z")

</div>

Hey i just have a quick question. I need to export a tsig-keygen output from a docker-compose container. This output needs to be registered in a var. This var needs to be used later in my playbook to be put into a writen file.

---

<div class="post-metadata">

**Author:** ![atul\_mohan](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@atul\_mohan](https://forum.ansible.com/u/atul_mohan)\
**Post date:** [January 13, 2025, 6:11am UTC](https://forum.ansible.com/t/register-variable-from-docker-compose-container/5170/2 "2025-01-13T06:11:08Z")

</div>

Step 1: Use Docker Compose to Run Your Command  
First, ensure your docker-compose.yml file includes the service where you want to run tsig-keygen. Here’s an example:

```auto
version: '3'
services:
  dns-key-generator:
    image: your-dns-utils-image # Replace with an appropriate image that has tsig-keygen
    command: tsig-keygen -a HMAC-MD5 example.tsig # Example command
    volumes:
      - .:/app # Mount a volume if you need to save the key to a file inside the container

```

Step 2: Use Ansible to Run Docker Compose and Capture Output  
In your Ansible playbook, you can use the docker\_compose module to start your Docker services and then use the shell or command module to run a command inside the container to capture the output of tsig-keygen. Here’s how you might structure this:

```auto
- name: Generate TSIG key and register variable
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Start docker-compose services
      community.docker.docker_compose:
        project_src: /path/to/your/docker-compose-directory
        state: present

    - name: Run tsig-keygen inside container and capture output
      shell:
        cmd: docker-compose exec dns-key-generator tsig-keygen -a HMAC-MD5 example.tsig
        executable: /bin/bash
      register: tsig_output

    - name: Extract the secret from the output
      set_fact:
        tsig_key: "{{ tsig_output.stdout | regex_search('secret \"(.*)\"', '\\1') }}"

    - name: Write the TSIG key to a file
      copy:
        content: "key example.tsig. {\n algorithm hmac-md5;\n secret \"{{ tsig_key }}\";\n};\n"
        dest: /path/to/your/config_file.conf

```
