Before apt-key was deprecated, I was using Ansible playbooks to add and update keys in my servers. At the moment, apt-key no longer updates the keys. In few searches, I found that I need to use gpg now. However, I have many servers and I don't want to do this manually for each one of them. Is there a way to manage my keyrings with gpg with Ansible?
Here are my Ansible tasks, with deprecated apt-key:
- apt_key:
url: "https://packages.treasuredata.com/GPG-KEY-td-agent"
state: present
- apt_repository:
repo: "deb http://packages.treasuredata.com/3/ubuntu/{{ ansible_distribution_release }}/ {{ ansible_distribution_release }} contrib"
state: present
filename: "treasure-data" # Name of the pre-compiled fluentd-agent
I tried apt-key update but it is not working for me. If a key already exists but it is expired, it doesn't update it anymore.
In short, you need to put the GPG key with the correct extension into a separate directory that is not searched by default, and point your repository configuration at that specific file.
For more info on why you need a separate directory, see this answer to "Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead".
Warning:
aptwill not accept ASCII GPG keys saved with .gpg extension.You can verify whether you have the old ASCII GPG format(.asc) or the newer binary GPG format(.gpg) via
file:If your key is the old format, you can either use the .asc extension, or you can optionally de-armor it via
gpg --dearmor elastic.gpginto the new binary format and use the .gpg extension.The de-armor step is annoying for ansible automation, so I suggest you just use whatever format upstream provides as is.
On Ubuntu 22.04, there's a folder you're expected to use that is not preloaded -
/etc/apt/keyrings- or you can create your own directory and use that.As for the Ansible part, you can use
get_urlorfileto push the GPG key onto the system, and then useapt_repositorylike before to add the repo, with the addition of specifying the keyring.Using the binary GPG format with .gpg
Or using the .asc extension if upstream still hasn't switched over yet
Then you can define your repository via apt_repository module like before.
Keep in mind that apt_repository uses the old
.listformat instead of the new DEB822 compliant.sourcesformat.If you want/need to use the newer DEB822 format and you happen to be running Ansible 2.15 or newer, you should use the deb822_repository module.
If you are stuck using older Ansible core, you can template it yourself similarly to this:
tasks/main.yamltemplates/repo.j2