How to work with gpg key generation in Ansible?

I am fairly new to Ansible.

What I want to achieve here is to simply generate a gpg key which I would normally achieve by runnin the command:
gpg --full-generate-key

After that I would be given input prompts on what kind of type, length, my name, email and passphrase.

To bypass having to input these, I am creating a gpg_key_config file that has all the configurations and then run the command:
gpg --batch --gen-key /tmp/gpg_key_config

But the problem is that after setting that up in Ansible, this exact command won’t ever run successfully, even though I’ve tried in the host machine directly.

My playbook so far:

- name: Key Generations
  hosts: test
  become: yes

    - name: Install gpg
        name: gnupg
        state: present

    - name: Create GPG key configuration file
        dest: /tmp/gpg_key_config
        content: |
          Key-Type: default
          Key-Length: 2048
          Subkey-Type: default
          Subkey-Length: 2048
          Name-Real: John Doe
          Expire-Date: 1y
          Passphrase: SuperSecretPassphrase

    - name: Generate GPG key
      command: gpg --batch --gen-key /tmp/gpg_key_config

I tried debuggin and it gave me this error message:

    "<class 'dict'>": "VARIABLE IS NOT DEFINED!"

What am I missing here? Why isn’t it as straight forward as it would seem?

Both Machines are running Debian Bookworm and I have Ansible [core 2.14.3]

Thank you

Can you run your playbook with -vvv option and paste the output?

It would seem you’ve omitted pasting something as your playbook doesn’t use variables.