Register Variable from docker-compose container

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.

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:

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:

- 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