<p>Is it possible to create and k8s secret from a file in ansible?</p>
<p>Currently, I am doing it like this but it only works on the first run because if I run the playbook again it says the secret already exists</p>
<pre><code>- name: generate keypair 
  openssh_keypair:
    path: /srv/{{item.namespace}}/id_{{item.name}}_rsa  
  when: item.additional_keys == true
  loop: &quot;{{ containers_release }}&quot;

- name: create private key secret for auth api
  shell: kubectl -n {{ item.namespace }} create secret generic id-{{ item.name }}-rsa-priv --from-file=/srv/{{ item.namespace }}/id_authapi_rsa
  when: item.additional_keys == true
  loop: &quot;{{ containers_release }}&quot;

- name: create public key secret for {{ item.name }}
  shell: kubectl -n {{ item.namespace }} create secret generic id-{{ item.name }}-rsa-pub --from-file=/srv/{{ item.namespace }}/id_{{ item.name }}_rsa.pub
  when: item.additional_keys == true
  loop: &quot;{{ containers_release }}&quot;

</code></pre>

Ydrab
<p>As I have mentioned in comment section <a href="https://www.ansible.com/" rel="nofollow noreferrer">ansible</a> is idempotent. If the configuration is already in place, ansible makes no change after redeploying. That is why after running playbook again your are getting playbook again it say info that the secret already exists.</p>
<p>Take a look:  <a href="https://stackoverflow.com/questions/59203583/how-can-i-create-a-kubernetes-secret-with-ansible">create-secret-with-ansible</a>.</p>
<p>You can try to use <a href="https://github.com/secrethub/ansible-secrethub" rel="nofollow noreferrer">SecretHub</a>.</p>
<p>See: <a href="https://secrethub.io/blog/how-to-inject-secrets-into-ansible-playbooks/" rel="nofollow noreferrer">ansible-playbook-secret</a>.</p>
