<p>I am trying to run a series of commands to create and add the same type of user to multiple mongodbs.
Currently I make a connection to the pod I want: <code>connect = os.system(&quot;kubectl exec -n epic-dev zerodtmongo-0  -it -- /bin/sh&quot;</code></p>
<p>but my python script ends there and the new shell for that pod opens within the python terminal.</p>
<p>I would like to continue the python script to execute this block of code within multiple pods I have stored in a list.</p>
<pre><code>    # Create roles to create roles 'listDatabase' &amp; 'readChangeStream'
mongo -u admin -p admin localhost:27017/admin &lt;&lt;-EOF
  db.runCommand(
  {
    createRole: &quot;listDatabases&quot;, 
    privileges: [
      { 
        resource: {cluster : true}, 
        actions: [&quot;listDatabases&quot;]
      }
    ],
    roles: []
  }
);

  db.runCommand(
  {
    createRole: &quot;readChangeStream&quot;,
    privileges: [
      { 
        resource: { db: &quot;&quot;, collection: &quot;&quot;}, 
        actions: [ &quot;find&quot;, &quot;changeStream&quot; ] 
      }
    ],
    roles: []
   });
EOF


# Create a debezium user
mongo -u admin -p admin localhost:27017/admin &lt;&lt;-EOF
  db.createUser(
  {
    user: 'foo',
    pwd: 'bar',
    roles: [
      { role: &quot;read&quot;, db: &quot;admin&quot; }
    ]
  });
EOF
</code></pre>
<p>Something like:</p>
<pre><code>for pods in list:
    connect(pod)
    add user
    close connection
</code></pre>

ctheising98
<p>you should use the python Kubernetes client to leverage the maximum benefit.</p>
<p><a href="https://github.com/kubernetes-client/python/" rel="nofollow noreferrer">kubernetes-client-python</a></p>
<p>Here is the working example that expects the <a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/" rel="nofollow noreferrer">Kube config</a> already set up.</p>
<pre><code>helm install my-release bitnami/mongodb
</code></pre>
<pre class="lang-py prettyprint-override"><code>
from click import command
from kubernetes import client, config
from kubernetes.stream import stream

def pod_exec(name, namespace, command, api_instance):
    exec_command = [&quot;/bin/sh&quot;, &quot;-c&quot;, command]

    resp = stream(api_instance.connect_get_namespaced_pod_exec,
                  name,
                  namespace,
                  command=exec_command,
                  stderr=True, stdin=False,
                  stdout=True, tty=False,
                  _preload_content=False)

    while resp.is_open():
        resp.update(timeout=1)
        if resp.peek_stdout():
            print(f&quot;STDOUT: \n{resp.read_stdout()}&quot;)
        if resp.peek_stderr():
            print(f&quot;STDERR: \n{resp.read_stderr()}&quot;)

    resp.close()

    if resp.returncode != 0:
        raise Exception(&quot;Script failed&quot;)

command = &quot;&quot;&quot;mongo -u root -p $MONGODB_ROOT_PASSWORD localhost:27017/admin &lt;&lt;-EOF
  db.runCommand(
  {
    createRole: &quot;listDatabases&quot;, 
    privileges: [
      { 
        resource: {cluster : true}, 
        actions: [&quot;listDatabases&quot;]
      }
    ],
    roles: []
  }
);

  db.runCommand(
  {
    createRole: &quot;readChangeStream&quot;,
    privileges: [
      { 
        resource: { db: &quot;&quot;, collection: &quot;&quot;}, 
        actions: [ &quot;find&quot;, &quot;changeStream&quot; ] 
      }
    ],
    roles: []
   });
EOF&quot;&quot;&quot;
print (command)
pod = &quot;my-release-mongodb-7bcd6b847-qzjxz&quot;
config.load_kube_config()

v1 = client.CoreV1Api()

pod_exec(pod, &quot;mongodb&quot;, command, v1)
</code></pre>
<p><code>python3 k8_create_db.py</code></p>
<p><a href="https://i.stack.imgur.com/eTHnQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eTHnQ.jpg" alt="enter image description here" /></a></p>
