Skip to content

Commit 9e4abf6

Browse files
committed
Feature add ansible-runner v0.3
Feature add ansible-runner v0.3
1 parent 4ada678 commit 9e4abf6

File tree

7 files changed

+346
-0
lines changed

7 files changed

+346
-0
lines changed

task/ansible-runner/0.3/README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Ansible Runner
2+
3+
Ansible Runner Task allows running the Ansible Playbooks using the [ansible-runner](https://ansible-runner.readthedocs.io/) tool.
4+
5+
The latest versions of ansible-runner requires [`community.general`](https://github.com/ansible-collections/community.general) to be installed from ansible-collections.
6+
7+
## Creating the Task
8+
9+
Create the Task and other resources:
10+
11+
```shell
12+
kubectl apply --filename https://api.hub.tekton.dev/v1/resource/tekton/task/ansible-runner/0.2/raw
13+
```
14+
15+
Verify the created tasks:
16+
17+
```shell
18+
tkn task ls
19+
```
20+
21+
## Parameters
22+
23+
* **project-dir**: The ansible-runner private data dir
24+
* **args:**: The array of arguments to pass to the runner command (_default:_ --help)
25+
* **user-home**: Absolute path to the user's home directory. (_default:_ /tekton/home)
26+
* **workspaces_ssl_ca_file**: file name of ca bundle. (_default:_ ca-bundle.crt)
27+
28+
## Workspaces
29+
30+
* **runner-dir**: A [workspace](https://github.com/tektoncd/pipeline/blob/main/docs/workspaces.md) to hold the `private_data_dir` as described in https://ansible-runner.readthedocs.io/en/latest/intro.html#runner-input-directory-hierarchy[Runner Directory]
31+
* **ssl-ca-directory**: A optional [workspace](https://github.com/tektoncd/pipeline/blob/main/docs/workspaces.md) to hold the ca_certs for custom certificate authorities.
32+
33+
## Platforms
34+
35+
The Task can be run on `linux/amd64` platform.
36+
37+
## Usage
38+
39+
The TaskRun uses the repository https://github.com/vinamra28/tektoncd-ansible-runner-example, that houses some example playbooks.
40+
41+
All the examples will be run in namespace called `funstuff`. Create the namespace and shift the context to it:
42+
43+
```shell
44+
kubectl create ns funstuff && \
45+
kubectl config set-context --current --namespace=funstuff
46+
```
47+
48+
### Create the PVC and clone example sources
49+
50+
```shell
51+
kubectl apply -f https://api.hub.tekton.dev/v1/resource/tekton/task/git-clone/0.5/raw \
52+
-f https://raw.githubusercontent.com/tektoncd/catalog/main/task/ansible-runner/0.2/support/playbooks-pvc.yaml
53+
```
54+
55+
Do the git clone of the examples repository:
56+
57+
```shell
58+
tkn task start git-clone \
59+
--workspace=name=output,claimName=ansible-playbooks \
60+
--param=url=https://github.com/vinamra28/tektoncd-ansible-runner-example \
61+
--param=revision=master \
62+
--param=deleteExisting=true \
63+
--showlog
64+
```
65+
66+
### Create the Service Account
67+
68+
As we will do get, list and create on the namespace, lets use a service account that has right RBAC:
69+
70+
```shell
71+
kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/ansible-runner/0.2/support/ansible-deployer.yaml
72+
```
73+
74+
### Run Tasks
75+
76+
List the pods of `kube-system` namespace:
77+
78+
```shell
79+
tkn task start ansible-runner \
80+
--serviceaccount ansible-deployer-account \
81+
--param=project-dir=kubernetes \
82+
--param=args=-p,list-pods.yml \
83+
--workspace=name=runner-dir,claimName=ansible-playbooks \
84+
--showlog
85+
```
86+
87+
### Create Deployment
88+
89+
Create a deployment in `funstuff` namespace:
90+
91+
```shell
92+
tkn task start ansible-runner \
93+
--serviceaccount ansible-deployer-account \
94+
--param=project-dir=kubernetes \
95+
--param=args=-p,create-deployment.yml \
96+
--workspace=name=runner-dir,claimName=ansible-playbooks \
97+
--showlog
98+
```
99+
100+
### Create Service
101+
102+
Create a service in `funstuff` namespace:
103+
104+
```shell
105+
tkn task start ansible-runner \
106+
--serviceaccount ansible-deployer-account \
107+
--param=project-dir=kubernetes \
108+
--param=args=-p,create-service.yml \
109+
--workspace=name=runner-dir,claimName=ansible-playbooks \
110+
--showlog
111+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
apiVersion: tekton.dev/v1beta1
3+
kind: Task
4+
metadata:
5+
name: ansible-runner
6+
labels:
7+
app.kubernetes.io/version: '0.3'
8+
app.kubernetes.io/ansible-version: '2.18.2'
9+
annotations:
10+
tekton.dev/deprecated: "false"
11+
tekton.dev/pipelines.minVersion: '0.12.1'
12+
tekton.dev/categories: CLI
13+
tekton.dev/tags: cli
14+
tekton.dev/displayName: 'Ansible Runner'
15+
tekton.dev/platforms: "linux/amd64"
16+
spec:
17+
description: >-
18+
Task to run Ansible playbooks using Ansible Runner
19+
20+
workspaces:
21+
- name: runner-dir
22+
description: The Ansibler runner directory
23+
- name: ssl-ca-directory
24+
optional: true
25+
description: |
26+
A Workspace containing CA certificates, this will be used by Ansible and pip to
27+
verify the peer with when interacting with remote repositories using
28+
HTTPS.
29+
params:
30+
- name: project-dir
31+
description: The project directory under the workspace runner-dir
32+
default: 'project'
33+
- name: args
34+
description: The arguments to pass ansible-runner
35+
type: array
36+
default:
37+
- --help
38+
- name: user-home
39+
description: Absolute path to the user's home directory.
40+
default: /tekton/home
41+
- name: image
42+
description: Ansible runner image.
43+
default: ghcr.io/ansible/community-ansible-dev-tools:v25.5.1 #tag: v25.5.1
44+
- name: workspaces_ssl_ca_file
45+
default: "ca-bundle.crt"
46+
47+
stepTemplate:
48+
env:
49+
- name: HOME
50+
value: $(params.user-home)
51+
- name: CURL_CA_BUNDLE
52+
value: "$(workspaces.ssl-ca-directory.path)/$(params.workspaces_ssl_ca_file)"
53+
steps:
54+
- name: requirements
55+
image: $(params.image)
56+
script: |
57+
#!/bin/bash
58+
set -e
59+
60+
if [ -f requirements.txt ];
61+
then
62+
pip3 install --user \
63+
-r requirements.txt
64+
fi
65+
66+
if [ -f requirements.yml ];
67+
then
68+
ansible-galaxy role install -vv \
69+
-r requirements.yml
70+
ansible-galaxy collection install -vv \
71+
-r requirements.yml
72+
fi
73+
workingDir: '$(workspaces.runner-dir.path)/$(params.project-dir)'
74+
75+
- name: run-playbook
76+
image: $(params.image)
77+
command: ['ansible-runner']
78+
args:
79+
- run
80+
- $(params.args)
81+
- $(params.project-dir)
82+
workingDir: '$(workspaces.runner-dir.path)'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: ansible-deployer-account
6+
namespace: funstuff
7+
---
8+
kind: ClusterRole
9+
apiVersion: rbac.authorization.k8s.io/v1
10+
metadata:
11+
name: ansible-deployer
12+
rules:
13+
# Core API
14+
- apiGroups: ['']
15+
resources: ['services', 'pods', 'deployments', 'configmaps', 'secrets']
16+
verbs: ['get', 'list', 'create', 'update', 'delete', 'patch', 'watch']
17+
# Apps API
18+
- apiGroups: ['apps']
19+
resources: ['deployments', 'daemonsets', 'jobs']
20+
verbs: ['get', 'list', 'create', 'update', 'delete', 'patch', 'watch']
21+
# Knative API
22+
- apiGroups: ['serving.knative.dev']
23+
resources: ['services', 'revisions', 'routes']
24+
verbs: ['get', 'list', 'create', 'update', 'delete', 'patch', 'watch']
25+
---
26+
apiVersion: rbac.authorization.k8s.io/v1beta1
27+
kind: ClusterRoleBinding
28+
metadata:
29+
name: ansible-deployer-binding
30+
subjects:
31+
- kind: ServiceAccount
32+
name: ansible-deployer-account
33+
namespace: funstuff
34+
roleRef:
35+
kind: ClusterRole
36+
name: ansible-deployer
37+
apiGroup: rbac.authorization.k8s.io
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
apiVersion: v1
3+
kind: PersistentVolumeClaim
4+
metadata:
5+
name: ansible-playbooks
6+
namespace: funstuff
7+
spec:
8+
accessModes:
9+
- ReadWriteOnce
10+
volumeMode: Filesystem
11+
resources:
12+
requests:
13+
storage: 1Gi
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Add git-clone
4+
add_task git-clone latest
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
apiVersion: v1
3+
kind: PersistentVolumeClaim
4+
metadata:
5+
name: ansible-playbooks-pvc
6+
spec:
7+
accessModes:
8+
- ReadWriteOnce
9+
resources:
10+
requests:
11+
storage: 100Mi
12+
---
13+
apiVersion: v1
14+
kind: ServiceAccount
15+
metadata:
16+
name: ansible-deployer-account
17+
namespace: ansible-runner-0-2
18+
---
19+
kind: ClusterRole
20+
apiVersion: rbac.authorization.k8s.io/v1
21+
metadata:
22+
name: ansible-deployer
23+
rules:
24+
# Core API
25+
- apiGroups: ['']
26+
resources: ['services', 'pods', 'deployments', 'configmaps', 'secrets']
27+
verbs: ['get', 'list', 'create', 'update', 'delete', 'patch', 'watch']
28+
# Apps API
29+
- apiGroups: ['apps']
30+
resources: ['deployments', 'daemonsets', 'jobs']
31+
verbs: ['get', 'list', 'create', 'update', 'delete', 'patch', 'watch']
32+
# Knative API
33+
- apiGroups: ['serving.knative.dev']
34+
resources: ['services', 'revisions', 'routes']
35+
verbs: ['get', 'list', 'create', 'update', 'delete', 'patch', 'watch']
36+
---
37+
apiVersion: rbac.authorization.k8s.io/v1
38+
kind: ClusterRoleBinding
39+
metadata:
40+
name: ansible-deployer-binding
41+
subjects:
42+
- kind: ServiceAccount
43+
name: ansible-deployer-account
44+
namespace: ansible-runner-0-2
45+
roleRef:
46+
kind: ClusterRole
47+
name: ansible-deployer
48+
apiGroup: rbac.authorization.k8s.io
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
apiVersion: tekton.dev/v1beta1
3+
kind: Pipeline
4+
metadata:
5+
name: ansibler-runner-pipeline
6+
spec:
7+
workspaces:
8+
- name: runner-dir
9+
tasks:
10+
- name: fetch-repository
11+
taskRef:
12+
name: git-clone
13+
workspaces:
14+
- name: output
15+
workspace: runner-dir
16+
params:
17+
- name: url
18+
value: |
19+
https://github.com/vinamra28/tektoncd-ansible-runner-example
20+
- name: revision
21+
value: 'master'
22+
- name: deleteExisting
23+
value: 'true'
24+
# ansible-runner
25+
- name: ansible-runner-run
26+
taskRef:
27+
name: ansible-runner
28+
runAfter:
29+
- fetch-repository
30+
workspaces:
31+
- name: runner-dir
32+
workspace: runner-dir
33+
params:
34+
- name: project-dir
35+
value: 'kubernetes'
36+
- name: args
37+
value: ['-p', 'list-pods.yml']
38+
39+
---
40+
apiVersion: tekton.dev/v1beta1
41+
kind: PipelineRun
42+
metadata:
43+
name: ansibler-runner-pipeline-run
44+
spec:
45+
serviceAccountName: ansible-deployer-account
46+
pipelineRef:
47+
name: ansibler-runner-pipeline
48+
workspaces:
49+
- name: runner-dir
50+
persistentVolumeClaim:
51+
claimName: ansible-playbooks-pvc

0 commit comments

Comments
 (0)