# How to work with gpg key generation in Ansible?

**URL:** <https://forum.ansible.com/t/how-to-work-with-gpg-key-generation-in-ansible/7220>\
**Category:** Get Help\
**Created:** [July 11, 2024, 1:51pm UTC](https://forum.ansible.com/t/how-to-work-with-gpg-key-generation-in-ansible/7220 "2024-07-11T13:51:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PinguPongu](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@PinguPongu](https://forum.ansible.com/u/PinguPongu)\
**Post date:** [July 11, 2024, 1:51pm UTC](https://forum.ansible.com/t/how-to-work-with-gpg-key-generation-in-ansible/7220/1 "2024-07-11T13:51:12Z")

</div>

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:

```auto
---
- name: Key Generations
  hosts: test
  become: yes
  tasks:

    - name: Install gpg
      ansible.builtin.package:
        name: gnupg
        state: present

    - name: Create GPG key configuration file
      copy:
        dest: /tmp/gpg_key_config
        content: |
          Key-Type: default
          Key-Length: 2048
          Subkey-Type: default
          Subkey-Length: 2048
          Name-Real: John Doe
          Name-Email: your_email@example.com
          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:

```auto
{
    "<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

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [July 12, 2024, 1:27pm UTC](https://forum.ansible.com/t/how-to-work-with-gpg-key-generation-in-ansible/7220/2 "2024-07-12T13:27:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [July 12, 2024, 2:10pm UTC](https://forum.ansible.com/t/how-to-work-with-gpg-key-generation-in-ansible/7220/3 "2024-07-12T14:10:14Z")

</div>

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