I needed to delegate ansible tasks to a container which ran in a pod in OpenShift. This is what needs to be done.

  1. Get the name of the pod

In my case it’s just one pod. If you have more than one replicas, you slightly need to adjust the command.

- name: get tower pod
  shell: "oc --no-headers=true get pod -l app=ansible-tower -o custom-columns=:metadata.name -n {{ tower_openshift_namespace }}"
  register: tower_pod

Another option would be to use the k8s_info module: https://docs.ansible.com/ansible/latest/modules/k8s_info_module.html#k8s-info-module

  1. Add this pod to your inventory
- name: add the tower pod to the inventory.
    name: '{{ tower_pod.stdout }}'
    oc_pod: '{{ tower_pod.stdout }}'
    oc_namespace: "{{ tower_openshift_namespace }}"
    ansible_connection: oc
  1. use it :)
- name: create a directory
    path: /var/foo/bar
    state: directory
    delegate_to: '{{ tower_pod.stdout }}'