Cannot run k8s module of ansible multiple times (not idempotent)

9/23/2019

I want to create some resources in an OpenSHift cluster using Ansible. Creation of resources works well, but when I run the same script for the second time, it claims that the resources already exist. In my opinion, this should not happen as ansible should be idempotent. I searched and found that this problem should be fixed (https://github.com/CiscoCloud/kubernetes-ansible/issues/69). What am I doing wrong?

Error message:

TASK [apply_templates : Set state of project-request in project developtest to present] *************************************************************************************************************************** fatal: [localhost]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "error": 409, "msg": "Failed to create object: {\"kind\":\"Status\",\"apiVersion\":\"v1\", \"metadata\":{},\"status\":\"Failure\",\"message\":\"project.project.openshift.io \\"developtest\\" already exists\",\"reason\":\"AlreadyExists\",\"details\":{\"name\":\"developtest\",\"group\":\"project.opens hift.io\",\"kind\":\"project\"},\"code\":409}\n", "reason": "Conflict", "status": 409}

Part of my playbook:

---

# Create projectrequest
- name: Set state of project-request in project {{current_project.name}} to {{state}}
  k8s:
    api_version: "{{apiversion}}"
    state: "{{ state }}"
    resource_definition: "{{ lookup('file', '{{ templating.destination_dir }}/{{current_project.name}}/projectrequest.yaml')}}"

# Create namespace and namespace-quota
- name: Set state of object in project {{current_project.name}} to {{state}}
  k8s:
    api_version: "{{apiversion}}"
    state: "{{ state }}"
    resource_definition: "{{ lookup('file', '{{ templating.destination_dir }}/{{current_project.name}}/namespace.yaml')}}"     

Maybe you can give me a hint where to look at. Thanks in advance.

-- sengbatz
ansible
kubernetes
openshift
python

0 Answers