<p>I would like to automate my Kubernetes configuration, like Ansible for VMs. </p>

<p>A sample scenario: </p>

<ol>
<li>Create namespace harbor </li>
<li>Deploy Harbor via Helm <a href="https://github.com/goharbor/harbor-helm" rel="nofollow noreferrer">https://github.com/goharbor/harbor-helm</a></li>
</ol>

<p>What kind tool is suitable for such as scenario? Can I also use terraform?</p>

softshipper
<p>I totally agree with <em>@P Ekambaram</em> that <strong>kubernetes automation</strong> can be successfully done with <strong>ansible</strong> but at the same time I totally disagree with him when it comes to the way it should be done. Such <em>"declarative"</em> code isn't actually declarative at all. It turns into a mere wrapper for the set of imperative commands. Apart from that such playbook isn't <a href="https://docs.ansible.com/ansible/latest/reference_appendices/glossary.html" rel="nofollow noreferrer">idempotent</a> (let's remind that <em>an operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any intervening actions</em>.). This approach is contrary to one of the key concepts present in <strong>ansible</strong>.</p>

<p><code>shell</code> or <code>command</code> modules should be used in <strong>ansible</strong> as last resort modules, only if there is no other way of performing required task e.g. when dedicated module doesn't exist or lacks some important functionalities.</p>

<p>When it comes to <strong>automating kubernetes with ansible</strong>, such dedicated modules already exist.</p>

<p>Take a quick look at <a href="https://docs.ansible.com/ansible/latest/modules/k8s_module.html#k8s-manage-kubernetes-k8s-objects" rel="nofollow noreferrer">k8s - ansible module for managing kubernetes objects</a> and you'll see that the desired results can be achieved in much more elegant and clearer way:</p>

<p><strong>namespace creation:</strong></p>

<pre><code>- name: Create a k8s namespace
  k8s:
    name: testing
    api_version: v1
    kind: Namespace
    state: present
</code></pre>

<p><strong>creating service based on yaml definition file</strong></p>

<pre><code>- name: Create a Service object by reading the definition from a file
  k8s:
    state: present
    src: /testing/service.yml
</code></pre>

<p>Have you just mentioned you're using <strong>helm</strong> for managing your <strong>kubernetes</strong> applications ? Take a look at <a href="https://docs.ansible.com/ansible/latest/modules/helm_module.html" rel="nofollow noreferrer">helm module documentation</a>. Any examples ? Here you are:</p>

<pre><code>- name: Install helm chart from a git repo
  helm:
    host: localhost
    chart:
      source:
        type: git
        location: https://github.com/user/helm-chart.git
    state: present
    name: my-example
    namespace: default
</code></pre>

<h3>update:</h3>

<p>To be able to run most <strong>Ansible</strong> modules on a certain host, you need to have installed on it two things:</p>

<ul>
<li>python 2.7</li>
<li>ssh server</li>
</ul>

<p>While it is true for most modules, some of them have additional requirements that must be met before the module can be run on such host. </p>

<p>When it comes to <code>k8s</code> module, as we can read in the its <a href="https://docs.ansible.com/ansible/latest/modules/k8s_module.html#requirements" rel="nofollow noreferrer">docs</a>:</p>

<blockquote>
  <p>The below requirements are needed on the host that executes this
  module.</p>

<pre><code>python &gt;= 2.7
openshift &gt;= 0.6
PyYAML &gt;= 3.11
</code></pre>
</blockquote>

<p>It isn't true that it requires <strong>openshift</strong> (different kubernetes implementation). It wouldn't make much sense to install <strong>openshift</strong> to manage workload of our <strong>kubernetes cluster</strong>. So let's be precise to avoid spreading any potentially misleading information: It requires <a href="https://github.com/openshift/openshift-restclient-python" rel="nofollow noreferrer"><strong>OpenShift python client library</strong></a> and it requires it to be installed on the host, on which the module is run (i.e. on the host on which we normally run our <code>kubectl</code> commands).</p>

<blockquote>
  <p>The OpenShift Python client wraps the K8s Python client, providing
  full access to all of the APIS and models available on both platforms.
  For API version details and additional information visit
  <a href="https://github.com/openshift/openshift-restclient-python" rel="nofollow noreferrer">https://github.com/openshift/openshift-restclient-python</a></p>
</blockquote>

<p><strong>Important!:</strong> you don't have to install any additional modules on your kubernetes nodes. You need them only on the machine from which you manage your <strong>kubernetes workload</strong>.</p>

<p>If we have our <strong>Ansible</strong> installed on the same host on which we have configured our <code>kubectl</code> tool and we want to run our playbooks on it directly without a need of using <strong>ssh</strong> or configuring <strong>ansible inventory</strong>, we simply need to refer to it in our <strong>ansible playbook</strong> as:</p>

<pre><code>hosts: localhost
connection: local
</code></pre>

<p>The whole playbook for creating new k8s namespace may look like this:</p>

<pre><code>---
- hosts: localhost
  connection: local
  tasks:
  - name: Create a k8s namespace
    k8s:
      name: testing
      api_version: v1
      kind: Namespace
      state: present
</code></pre>

<p>And we can run it by simply executing:</p>

<pre><code>ansible-playbook playbook.yaml
</code></pre>

<p>Before we can do that, however, we need to make sure we have all required dependencies installed which in fact arn't so many and they limit to <strong>python 2.7</strong> and <strong>two above mentioned libraries</strong> (in required versions).</p>

<p>Once you have <strong>Python</strong> installed on your host, the easiest way to install the rest of the required dependencies is by runnig:</p>

<pre><code>pip install openshift
</code></pre>

<p>and</p>

<pre><code>pip install PyYAML
</code></pre>

<p>You may encounter the following error:</p>

<pre><code>"msg": "Failed to import the required Python library (openshift) on ***** Python /usr/bin/python. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"
</code></pre>

<p>If so, running:</p>

<pre><code>pip install --upgrade requests
</code></pre>

<p>should fix it (at least it fixed it in my case) and after that our simple <strong>ansible playbook</strong> which creates new <code>namespace</code> in our <strong>kubernetes cluster</strong> should run smoothly. :)</p>
