<p>Add the key per host or per group in your inventory. Below example is for an all-in-one yaml inventory. See <a href="https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html" rel="nofollow noreferrer">How to build you inventory</a> for all options to organize your vars (I suggest you read this documentation in depth).</p>
<p><strong>inventory/example.yml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>---
all:
  hosts:
    master1.tld.local:
    master2.tld.local:
    worker1.tld.local:
    worker2.tld.local:
      # This will be used only for this node and will
      # override group var as it is more specific
      # see: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#understanding-variable-precedence
      my_apt_key: https://some.server.com/custom_node_key.gpg
    worker3.tld.local:

  children:
    master:
      vars:
        # This will be applied to all machines in group
        my_apt_key: https://some.server.com/master_key.gpg
      hosts:
        master1.tld.local:
        master2.tld.local:

    worker:
      vars:
        my_apt_key: https://some.server.com/worker_key.gpg
      hosts:
        worker1.tld.local:
        worker2.tld.local:
        worker3.tld.local:
</code></pre>
<p>Modify your task in your playbook. As a good practice, I'll add a default value so it does not break if the var is not defined. You can drop this if this is not the behavior you want:</p>
<p><strong>playbook.yml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>  - name: add Kubernetes apt-key
    apt_key:
      url: &quot;{{ my_apt_key | default('https://some.server.com/default.gpg) }}&quot;
      state: present
</code></pre>
<p>Then enjoy:</p>
<pre class="lang-bash prettyprint-override"><code>ansible-playbook -i inventory/example.yml playbook.yml
</code></pre>

CC BY-SA 4.0
2021-06-06T08:55:09.137Z
2021-06-06T16:10:26.480Z
2021-06-06T16:10:26.480Z
Zeitounator
<p>I am completely new to ansible this is my second day working with ansible, What I'm ultimately trying to do is configure a kubernetes cluster which has a master node and one worker node. But my problem is I've been asked to add an apt-key without using a hardcoded url.
So I have an ansible playbook called kube-repos.yml which is as follows:</p>
<pre><code>- hosts: all
  become: yes
  tasks:

  - name: add Kubernetes apt-key
    apt_key:
      url: &quot;https://packages.cloud.google.com/apt/doc/apt-key.gpg&quot;
      state: present

  - name: add Kubernetes' APT repository
    apt_repository:
      repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
      state: present
</code></pre>
<p>where I add the apt-key I need to use another method to add the apt-key where it needs to be defined per-host. If anyone knows of other ways of adding an apt-key I would really like to hear them so I can look further into alternate ways of adding an apt-key.
Thanks in advanced for any help or points in the right direction.</p>

<ansible>
How can I add an apt-key without using the hardcoded url in an ansible playbook?
user2152012
# How can I add an apt-key without using the hardcoded url in an ansible playbook?

(Asked by: username_0 on 2021-05-30)

I am completely new to ansible this is my second day working with ansible, What I'm ultimately trying to do is configure a kubernetes cluster which has a master node and one worker node. But my problem is I've been asked to add an apt-key without using a hardcoded url.
So I have an ansible playbook called kube-repos.yml which is as follows:
- hosts: all
  become: yes
  tasks:

  - name: add Kubernetes apt-key
    apt_key:
      url: "https://packages.cloud.google.com/apt/doc/apt-key.gpg"
      state: present

  - name: add Kubernetes' APT repository
    apt_repository:
      repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
      state: present

where I add the apt-key I need to use another method to add the apt-key where it needs to be defined per-host. If anyone knows of other ways of adding an apt-key I would really like to hear them so I can look further into alternate ways of adding an apt-key.
Thanks in advanced for any help or points in the right direction.

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2021-06-06. Score: 1

Add the key per host or per group in your inventory. Below example is for an all-in-one yaml inventory. See How to build you inventory for all options to organize your vars (I suggest you read this documentation in depth).
inventory/example.yml
---
all:
  hosts:
    master1.tld.local:
    master2.tld.local:
    worker1.tld.local:
    worker2.tld.local:
      # This will be used only for this node and will
      # override group var as it is more specific
      # see: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#understanding-variable-precedence
      my_apt_key: https://some.server.com/custom_node_key.gpg
    worker3.tld.local:

  children:
    master:
      vars:
        # This will be applied to all machines in group
        my_apt_key: https://some.server.com/master_key.gpg
      hosts:
        master1.tld.local:
        master2.tld.local:

    worker:
      vars:
        my_apt_key: https://some.server.com/worker_key.gpg
      hosts:
        worker1.tld.local:
        worker2.tld.local:
        worker3.tld.local:

Modify your task in your playbook. As a good practice, I'll add a default value so it does not break if the var is not defined. You can drop this if this is not the behavior you want:
playbook.yml
  - name: add Kubernetes apt-key
    apt_key:
      url: "{{ my_apt_key | default('https://some.server.com/default.gpg) }}"
      state: present

Then enjoy:
ansible-playbook -i inventory/example.yml playbook.yml


---

