# How to run oc commands with Ansible

**URL:** <https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456>\
**Category:** Get Help\
**Tags:** openshift, awx, ee\
**Created:** [November 19, 2023, 7:09pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456 "2023-11-19T19:09:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkoci](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dkoci/32/815_2.png) [@dkoci](https://forum.ansible.com/u/dkoci)\
**Post date:** [November 19, 2023, 7:09pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/1 "2023-11-19T19:09:52Z")

</div>

Hi.  
Is there any module that enables the execution of Openshift oc commands? I run playbooks within the awx-ee execution environment, which doesn’t have the oc CLI installed, so I cant use ansible builtin.shell: oc …

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [November 19, 2023, 8:47pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/2 "2023-11-19T20:47:42Z")

</div>

Hi,

Haven’t tried it, but have a look on this collection: [GitHub - openshift/community.okd: OKD/Openshift collection for Ansible](https://github.com/openshift/community.okd).

Another way would be to install `oc` in your image, then run tasks using `command` or `shell` modules, as you initially planned to.

IIRC, you specifically need `oc` command to login, everything else could be done using `kubectl`, through the [kubernetes.core collection](https://github.com/ansible-collections/kubernetes.core) or else.

---

<div class="post-metadata">

**Author:** ![Tas-sos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tas-sos/32/672_2.png) [@Tas-sos](https://forum.ansible.com/u/Tas-sos)\
**Post date:** [November 19, 2023, 10:28pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/3 "2023-11-19T22:28:37Z")

</div>

Interesting question.

I think the most important and difficult part is the login. For example you can use the [cki-project / ocp-sso-token · GitLab](https://gitlab.com/cki-project/ocp-sso-token) to login.

However reading the documentation bellow:  
[OKD Collection for Ansible | Using modules from the OKD Collection in your playbooks](https://github.com/openshift/community.okd#using-modules-from-the-okd-collection-in-your-playbooks)

```yaml
---
plugin: community.okd.openshift
connections:
  - namespaces:
    - testing

```

Is it possible to login using **only** Ansible ? We have to define somewhere a `plugins` or `connection_plugins` and define there the OpenShift credentials ?

Another idea: If we define the corresponding variables `ansible_kubectl_*` somewhere ( _group\_vars/host\_vars or inventory file_ ) is it supposed that Ansible should be able to login to OpenShift ? Should this work ?

---

<div class="post-metadata">

**Author:** ![dkoci](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dkoci/32/815_2.png) [@dkoci](https://forum.ansible.com/u/dkoci)\
**Post date:** [November 20, 2023, 11:30am UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/4 "2023-11-20T11:30:38Z")

</div>

Login is not the problem. I use community.okd module which allows login. But I need to execute oc command. Kubectl is not an option.

---

<div class="post-metadata">

**Author:** ![dkoci](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dkoci/32/815_2.png) [@dkoci](https://forum.ansible.com/u/dkoci)\
**Post date:** [November 20, 2023, 1:37pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/5 "2023-11-20T13:37:46Z")

</div>

I installed oc cli in my custom EE image.

---

<div class="post-metadata">

**Author:** ![sirjaren](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@sirjaren](https://forum.ansible.com/u/sirjaren)\
**Post date:** [November 20, 2023, 1:59pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/6 "2023-11-20T13:59:08Z")

</div>

I think installing `oc` in your EE’s is probably your best bet for now.

Since `kubernetes.core` collection requires `openshift-clients`, you’ll get the `kubectl` plugin as well as the `oc` command.

For example, if building with UBI8-minimal or UBI9-minimal `openshift-clients` is not available in the default repos, which means an RPM install of `openshift-clients` is needed to support the `kubernetes.core` collection.

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [November 20, 2023, 5:11pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/7 "2023-11-20T17:11:17Z")

</div>

You could do it from this previously linked collection, with [this plugin](https://github.com/openshift/community.okd/blob/main/docs/community.okd.oc_connection.rst) specifically, though `oc` binary needs to be installed prior on control node. I see on your next comment you did precisely that, now you can keep using this collection’s plugins and modules, I guess.

---

<div class="post-metadata">

**Author:** ![dkoci](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dkoci/32/815_2.png) [@dkoci](https://forum.ansible.com/u/dkoci)\
**Post date:** [November 20, 2023, 5:52pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/8 "2023-11-20T17:52:27Z")

</div>

Plugin community.okd.oc runs oc command in running pods (oc exec). I dont need that. The only thing I need is to run oc command on managed host. Managed host is localhost (EE) without oc binary installed.

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [November 20, 2023, 6:35pm UTC](https://forum.ansible.com/t/how-to-run-oc-commands-with-ansible/2456/9 "2023-11-20T18:35:39Z")

</div>

Right ! I misread that. Other modules seems to directly talk to the API, so no strict `oc` command usage. So yeah, as others suggested, having `oc` installed in your EE container might be the way to go, whether by rebuilding your image to comport this binary, or installing it afterwards from a task in `pre_tasks:` or else.
