Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ansibleop to 4-7 #1149

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ansibleop/ansible-k8s-modules/finish.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Information, check out the links below:
## Chat
* Kubernetes Slack Chat (upstream): #kubernetes-operators at https://kubernetes.slack.com/
* Operator-Framework on Google Groups: https://groups.google.com/forum/#!forum/operator-framework
* OpenShift Operators Special Interest Group (SIG): https://commons.openshift.org/sig/OpenshiftOperators.html
* OpenShift Operators Special Interest Group (SIG): https://commons.openshift.org/sig/OpenshiftOperators.html
8 changes: 4 additions & 4 deletions ansibleop/ansible-k8s-modules/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"details": {
"steps": [
{
"title": "Running the k8s Ansible modules locally",
"title": "Step 1 - Running the k8s Ansible modules locally",
"text": "step1.md"
}, {
"title": "Leveraging existing k8s Resource Files inside of Ansible",
"title": "Step 2 - Leveraging existing k8s Resource Files inside of Ansible",
"text": "step2.md"
}, {
"title": "Customizing k8s Resource files with Ansible",
"title": "Step 3 - Customizing k8s Resource files with Ansible",
"text": "step3.md"
}],
"intro": {
Expand All @@ -40,7 +40,7 @@
},
"backend": {
"autoUpgrade": true,
"imageid": "openshift-4-5",
"imageid": "openshift-4-7",
"port": 8443
}
}
15 changes: 9 additions & 6 deletions ansibleop/ansible-k8s-modules/intro.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
By the end of this scenario, you'll be able to use the Ansible k8s module for a variety of Kubernetes operations.

In this tutorial, you will:
* Create and remove Kubernetes resources
* Reuse existing Kubernetes manifest files with Ansible

## Getting started

Since you are interested in *using Ansible for lifecycle management of applications on Kubernetes*, it will be beneficial to learn how to use the [Ansible k8s (Kubernetes) module](https://docs.ansible.com/ansible/latest/modules/k8s_module.html#k8s-info-module).

The k8s module allows you to:
Expand All @@ -6,9 +14,4 @@ The k8s module allows you to:

One of the biggest benefits of using Ansible in conjunction with existing Kubernetes resource files is the ability to use Ansible's built-in [Jinja templating](https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html) engine to customize deployments by simply setting Ansible variables.

---

By the end of this scenario, you'll be able to use the Ansible k8s module to:

- _Create_ and _remove_ Kubernetes resources
- _Reuse_ existing Kubernetes manifest files with Ansible
In the following sections, you will go through the above steps to interact with the Ansible Kubernetes module. Let's get started!
35 changes: 14 additions & 21 deletions ansibleop/ansible-k8s-modules/step1.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
For this example we will *create and delete a namespace* with the switch of an Ansible variable.
For this example we will create and delete a namespace with the switch of an Ansible variable.

---
## Creating a new namespace through Ansible

###### **a. Modify tasks file `example-role/tasks/main.yml` to contain the Ansible shown below.**
<pre class="file">
---
First, modify tasks file `example-role/tasks/main.yml` to contain the Ansible shown below:

```
- name: set test namespace to {{ state }}
k8s:
api_version: v1
kind: Namespace
name: test
state: "{{ state }}"
ignore_errors: true

</pre>
```

You can easily update this file by running the following command:

`wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/tasksmain1.yml -O /root/tutorial/example-role/tasks/main.yml`{{execute}}
<br>
---

###### **b. Modify vars file `example-role/defaults/main.yml`, setting `state: present` by default.**
Secondly, modify vars file `example-role/defaults/main.yml`, setting `state: present` by default.

<pre class="file">
---
```
state: present

</pre>
```

You can easily update this file by running the following command:

`wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/defaultsmain1.yml -O /root/tutorial/example-role/defaults/main.yml`{{execute}}
<br>
---

###### **c. Run playbook.yml, which will execute 'example-role'.**
Then, run playbook.yml, which will execute 'example-role':

`ansible-playbook -i myhosts playbook.yml`{{execute}}

---
Finally, check that the namespace `test` was created.

###### **d. Check that the namespace `test` was created.**
`oc get projects | grep test`{{execute}}.

$ `oc get projects | grep test`{{execute}}
You should see the following output:

```
NAME DISPLAY NAME STATUS
test Active
```
```
38 changes: 12 additions & 26 deletions ansibleop/ansible-k8s-modules/step2.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
Next, we'll use the Ansible k8s module to leverage existing Kubernetes and OpenShift Resource files. Let's take the **[nginx deployment example](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment)**
from the Kubernetes docs.
Next, we'll use the Ansible k8s module to leverage existing Kubernetes and OpenShift Resource files. Let's take the [nginx deployment example](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment) from the Kubernetes docs.

**Note**: *We've modified the resource file slightly as we will be deploying
on OpenShift.*
**Note**: We've modified the resource file slightly as we will be deploying on OpenShift.

---
## Leverage existing K8s and OpenShift resource files

###### **a. Copy the nginx deployment definition `nginx-deployment.yml` into `example-role/templates`, adding a .j2 extension**

`cp nginx-deployment.yml ./example-role/templates/nginx-deployment.yml.j2`{{execute}}

```
$ cat ./example-role/templates/nginx-deployment.yml.j2
Firstly, copy the nginx deployment definition `nginx-deployment.yml` into `example-role/templates`, adding a .j2 extension.

```
kind: Deployment
apiVersion: apps/v1
metadata:
Expand All @@ -32,13 +26,12 @@ spec:
selector:
name: nginx
```
---

`cp nginx-deployment.yml ./example-role/templates/nginx-deployment.yml.j2`{{execute}}

###### **b. Update tasks file `example-role/tasks/main.yml` to create the nginx deployment using the k8s module**
Next, update tasks file `example-role/tasks/main.yml` to create the nginx deployment using the k8s module

<pre class="file">
---
```
- name: set test namespace to {{ state }}
k8s:
api_version: v1
Expand All @@ -51,23 +44,16 @@ spec:
state: "{{ state }}"
definition: "{{ lookup('template', 'nginx-deployment.yml.j2') }}"
namespace: test
</pre>
```

You can easily update this file by running the following command:

`wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/tasksmain2.yml -O /root/tutorial/example-role/tasks/main.yml`{{execute}}

---

###### **c. Run the Playbook to deploy nginx onto OpenShift**

Running the Playbook with the command below will read the `state` variable defined in `example-role/defaults/main.yml`

`ansible-playbook -i myhosts playbook.yml`{{execute}}
After, run the Playbook to deploy nginx onto OpenShift. Running the Playbook with the command below will read the `state` variable defined in `example-role/defaults/main.yml`

---
`ansible-playbook -i myhosts playbook.yml`{{execute}}

###### **d. Examine Playbook results**
You can see the `test` namespace created and the `nginx` deployment created in the new namespace.
Finally, examine the Playbook results. You can see the `test` namespace created and the `nginx` deployment created in the new namespace.

`oc get all -n test`{{execute}}
45 changes: 14 additions & 31 deletions ansibleop/ansible-k8s-modules/step3.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
Next, let's make it possible to customize the replica count for our [nginx deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment) by adding an `nginx_replicas` variable to the Deployment template and filling the variable value dynamically with Ansible.

---
## Working with replicas

###### **a. Modify vars file `example-role/defaults/main.yml`, setting `nginx_replicas: 2`**
First, modify vars file `example-role/defaults/main.yml`, setting `nginx_replicas: 2`

<pre class="file">
---
```
state: present
nginx_replicas: 2

</pre>
```

You can easily update this file by running the following command:

```
wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/defaultsmain2.yml -O /root/tutorial/example-role/defaults/main.yml
```{{execute}}
<br>

---
`wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/defaultsmain2.yml -O /root/tutorial/example-role/defaults/main.yml`{{execute}}

###### **b. Modify nginx deployment definition `nginx-deployment.yml.j2` to read `replicas` from the `nginx_replicas` variable**
Next, modify nginx deployment definition `nginx-deployment.yml.j2` to read `replicas` from the `nginx_replicas` variable.

<pre class="file">
```
kind: Deployment
apiVersion: apps/v1
metadata:
Expand All @@ -41,32 +34,22 @@ spec:
replicas: {{ nginx_replicas }}
selector:
name: nginx
</pre>
```

You can easily update this file by running the following command:

```
wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/nginx-deployment-updated.yml.j2 -O /root/tutorial/example-role/templates/nginx-deployment.yml.j2
```{{execute}}
<br>
---
`wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/nginx-deployment-updated.yml.j2 -O /root/tutorial/example-role/templates/nginx-deployment.yml.j2`{{execute}}

###### **c. Run the Playbook to change the nginx replica count**
It's time to run the Playbook to change the nginx replica count. Running the Playbook again will read the variable `nginx_replicas` and use the provided value to customize the nginx Deployment.

Running the Playbook again will read the variable `nginx_replicas` and use the provided value to customize the nginx Deployment.

`ansible-playbook -i myhosts playbook.yml`{{execute}}
`ansible-playbook -i myhosts playbook.yml`{{execute}}

---

###### **d. Examine Playbook results**
After running the Playbook, the cluster will scale down one of the nginx pods to meet the new requested replica count of 2.
Let's examine the Playbook results! After running the Playbook, the cluster will scale down one of the nginx pods to meet the new requested replica count of 2.

`oc get pods -n test`{{execute}}

---
###### **e. Tear down the nginx deployment**

To remove the nginx deployment, we'll override the `state` variable to contain `state=absent` using the [`-e / --extra-vars`](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#passing-variables-on-the-command-line) flag.
Finally, tear down the nginx deployment. To remove the nginx deployment, we'll override the `state` variable to contain `state=absent` using the [`-e / --extra-vars`](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#passing-variables-on-the-command-line) flag.

`ansible-playbook -i myhosts playbook.yml --extra-vars state=absent`{{execute}}
`ansible-playbook -i myhosts playbook.yml --extra-vars state=absent`{{execute}}
2 changes: 1 addition & 1 deletion ansibleop/ansible-mcrouter-operator/finish.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Thank you for taking a closer look at the *Ansible Operator*. For more
Thank you for taking a closer look at the *Mcrouter Operator*. For more
Information, check out the links below:

## Git Hub
Expand Down
2 changes: 1 addition & 1 deletion ansibleop/ansible-mcrouter-operator/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"backend": {
"autoUpgrade": true,
"imageid": "openshift-4-5",
"imageid": "openshift-4-7",
"port": 8443
}
}
12 changes: 7 additions & 5 deletions ansibleop/ansible-mcrouter-operator/intro.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
The Mcrouter Operator was built with the [Ansible Operator SDK](https://github.com/operator-framework/operator-sdk/blob/master/doc/ansible/user-guide.md). It is not yet intended for production use.
In this self-paced tutorial you will learn an overview of the Mcrouter Operator, built with the [Ansible Operator SDK](https://github.com/operator-framework/operator-sdk/blob/master/doc/ansible/user-guide.md). It is not yet intended for production use.

## Getting started

[Mcrouter](https://github.com/facebook/mcrouter) is a Memcached protocol router for scaling [Memcached](http://memcached.org/) deployments. It's a core component of cache infrastructure at Facebook and Instagram where mcrouter handles almost 5 billion requests per second at peak.

Expand All @@ -15,8 +17,8 @@ Mcrouter features:

Mcrouter is developed and maintained by Facebook.



At this point in our training, we should have a basic understanding of the *Operator pattern*.
- Ansible Operator is an Operator which is _powered by Ansible_.
- Custom Resource events trigger Ansible tasks as opposed to the traditional approach of handling these events with Go code.
- Ansible Operator is an Operator which is _powered by Ansible_.
- Custom Resource events trigger Ansible tasks as opposed to the traditional approach of handling these events with Go code.

In the following sections, you will go through various stages of deployment, usage, and testing of Mcrouter. Let's get started!
2 changes: 1 addition & 1 deletion ansibleop/ansible-operator-overview/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"backend": {
"autoUpgrade": true,
"imageid": "openshift-4-5",
"imageid": "openshift-4-7",
"port": 8443
}
}
23 changes: 12 additions & 11 deletions ansibleop/ansible-operator-overview/intro.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
This section will give a brief overview of the *Ansible Operator* with a step-by-step example of developing an Ansible Operator using [Operator SDK](https://sdk.operatorframework.io).

The reader is expected to have a basic understanding of the *Operator pattern*.
- Ansible Operator is an Operator which is _powered by Ansible_.
- Custom Resource events trigger Ansible tasks as opposed to the traditional approach of handling these events with Go code.

Ansible Operator development and testing is fully supported as a first-class citizen within the Operator SDK. Operator SDK can be used to create new Operator projects, test existing Operator projects, build Operator images, and generate new Custom Resource Definitions (CRDs) for an Operator.

---

By the end of this section the reader should have a basic understanding of:
In this self-paced tutorial, you will learn a brief overview of the *Ansible Operator* with a step-by-step example of developing an Ansible Operator using [Operator SDK](https://sdk.operatorframework.io).

In this tutorial, you will gain a basic understanding of:
* What the Ansible Operator is
* How the Ansible Operator maps Custom Resource events to Ansible code
* How to pass extra variables to Ansible code via the operator
* How to leverage existing roles from Ansible Galaxy
* How to deploy and run the Ansible Operator in a OpenShift cluster
* How to run the Ansible Operator Locally for development

## Getting started

The reader is expected to have a basic understanding of the *Operator pattern*.
- Ansible Operator is an Operator which is _powered by Ansible_.
- Custom Resource events trigger Ansible tasks as opposed to the traditional approach of handling these events with Go code.

Ansible Operator development and testing is fully supported as a first-class citizen within the Operator SDK. Operator SDK can be used to create new Operator projects, test existing Operator projects, build Operator images, and generate new Custom Resource Definitions (CRDs) for an Operator.

In the following sections, you will go through various steps to work with and leverage the Ansible Operator. Let's get started!