Delegate Ansible Tasks to a pod in OpenShift
I needed to delegate ansible tasks to a container which ran in a pod in OpenShift. This is what needs to be done.
- 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
- Add this pod to your inventory
- name: add the tower pod to the inventory.
add_host:
name: '{{ tower_pod.stdout }}'
oc_pod: '{{ tower_pod.stdout }}'
oc_namespace: "{{ tower_openshift_namespace }}"
ansible_connection: oc
- use it :)
- name: create a directory
file:
path: /var/foo/bar
state: directory
delegate_to: '{{ tower_pod.stdout }}'
Read other posts