How to include a dictionary into a k8s_raw data field

5/22/2019

I'm writing an Ansible-playbook to insert a list of secret object into Kubernetes. I'm using k8s_raw syntax and I want to import this list from a group_vars file. I can't find the right syntax to import the list of secret into my data field.

playbook.yml

- hosts: localhost
  tasks:
  - name: Create a Secret object
    k8s_raw:
      state: present
      definition:
        apiVersion: v1
        kind: Secret
        data:
          "{{ secrets }}"
          SKRT: "c2trcnIK"
        metadata:
          name: "test"
          namespace: "namespace-test"
        type: Opaqueroot
  vars_files:
    - "varfile.yml"

varfile.yml

secrets:
  TAMAGOTCHI_CODE: "MTIzNAo="
  FRIDGE_PIN: "MTIzNAo="
-- Sleepless
ansible
ansible-template
kubernetes

3 Answers

5/22/2019

The problem was the SKRT: "c2trcnIK" field just under the "{{ secrets }}" line. I deleted it and now it works ! Thank you all.

-- Sleepless
Source: StackOverflow

5/22/2019

To import this list from a group_vars file

Put the localhost into a group. For example a group test

> cat hosts
test:
  hosts:
    localhost:

Put the varfile.yml into the group_vars/test directory

$ tree group_vars
group_vars/
├── test
    └── varfile.yml

Then running the playbook below

$ cat test.yml
- hosts: test
  tasks:
    - debug:
        var: secrets.TAMAGOTCHI_COD

$ ansible-playbook -i hosts test.yml

gives:

PLAY [test] ***********************************
TASK [debug] **********************************
ok: [localhost] => {
    "secrets.TAMAGOTCHI_CODE": "MTIzNAo="
}
PLAY RECAP *************************************
localhost: ok=1    changed=0    unreachable=0    failed=0
-- Vladimir Botka
Source: StackOverflow

5/22/2019

First, what does it actually say when you attempt the above? It would help to have the result of your attempts.

Just guessing but try moving the var_files to before the place where you try to use the variables. Also, be sure that your indentation is exactly right when you do.

- hosts: localhost
  vars_files:
    - /varfile.yml

  tasks:
  - name: Create a Secret object
    k8s_raw:
      state: present
      definition:
        apiVersion: v1
        kind: Secret
        data:
          "{{ secrets }}"
        metadata:
          name: "test"
          namespace: "namespace-test"
        type: Opaqueroot

Reference

side note: I would debug this immediately without attempting the task. Remove your main task and after trying to use vars_files, attempt to directly print the secrets using the debug play. This will allow you to fine tune the syntax and keep fiddling with it until you get it right without having to run and wait for the more complex play that follows. Reference.

-- Old Schooled
Source: StackOverflow