<p>I am writing ansible scripts for deploying services using Kubernetes, I am stuck with a step that is for the post-deployment process:</p>
<p>I have deployed a service having &quot;<strong>replicas: 3</strong>&quot;, and all the replicas are up and running now my problem is to I have to do a migration for which I have to get into the container and run a script already present there.</p>
<p>I can do it manually by getting into the container individually and then run the script but this will again require manual intervention.</p>
<p>What I want to achieve is once the deployment is done and all the replicas are up and running I want to run the scripts by getting into the containers and all these steps should be performed by ansible script and no manual effort required.</p>
<p>Is there a way to do this?</p>

Anuj Kishor
<p>Take a look at <a href="https://docs.ansible.com/ansible/latest/collections/community/kubernetes/k8s_exec_module.html" rel="nofollow noreferrer">k8s_exec</a> module.</p>
<pre><code>- name: Check RC status of command executed
  community.kubernetes.k8s_exec:
    namespace: myproject
    pod: busybox-test
    command: cmd_with_non_zero_exit_code
  register: command_status
  ignore_errors: True

- name: Check last command status
  debug:
    msg: &quot;cmd failed&quot;
  when: command_status.return_code != 0
</code></pre>
