# How to Create Ansible Custom Job in Spinnaker

## **What you’ll learn:**

1. How to configure and set up Ansible stage in Spinnaker as a custom stage job
2. How to execute Ansible playbooks through.

## **Prerequisites:**

1. Understanding of spinnaker and spinnaker custom job.
2. Running spinnaker with Kubernetes cluster account configured in it.

Get detailed information about [Spinnaker Custom Job](https://www.spinnaker.io/guides/operator/custom-job-stages/)**.**

## **Ansible Custom Stage Architecture:**

The custom stage architecture is shown in the image below:

![Custom Stage Architechture](https://lh5.googleusercontent.com/B1uZbxAJcoL2Y_sNvhTAfNQ8wFuTM2MfixMXuk_H5QnIS3YHH13fTIMLdngzKaCPhvBgvu-s5Pw22tZ0-FxyfMjBFwa7sk44_QNASLuYdb91Fu0cZ2mHP8cWiEN7nEmg8SZUKAKG)

Follow the steps given below:

1. Create the **configmap** with the name **ansible-config** which will be mapped in **orca-loca.yml**. The configmap name can be anything as per your choice. In this document we are using the name as **ansible-config**. configmap is available [here](https://github.com/opsmxdemo/ansible-demo/blob/master/ansible-job/configmap).

{% hint style="info" %}
Note: mention your namespace in configmap.
{% endhint %}

2\. Apply the configmap as shown below:

```
# kubectl apply -f configmap
```

After applying, check whether the configmap is created using the command below:

```
# kubectl get cm
```

3\. Pass the credentials in k8s secrets where you will be setting the git username, password, Ansible node user and password. Use the following command to create the secrets and add appropriate credentials:

```
kubectl create secret generic ansible-secrets --from-literal=gitusername=<USERNAME> --from-literal=gitpassword=<PASSWORD> --from-literal=nodeuser=<USER> --from-literal=userpassword=<USERPASSWORD> -n <namespace>
```

In the above command, **ansible-secret** is the name of secrets in k8s will be mapped in **orca-local.yml** in manifest. Here we are using **secretname** as ansible-secret, you can use any name as per your choice.

Once done, check whether it is created or not using the following command:

```
# kubectl get secrets
```

4\. Create **orca-local.yml** file in **/home/spinnaker/.hal/default/profiles** which is in Halyard po&#x64;**.** If it already exists you can add the content below except the first three lines. **orca-local.yml** is available [**here**](https://github.com/opsmxdemo/ansible-demo/blob/master/ansible-job/orca-local.yml)**.**

Apply the changes to reflect in Spinnaker UI using the following command:

```
# hal deploy apply
```

## **Spinnaker UI Prerequisites needed for Ansible job:**

**Ansible playbook -** For sample playbook which creates a directory. Sample playbook is available [here](https://github.com/opsmxdemo/ansible-demo/blob/master/ansibleplaybooks/createdir.yml).

{% hint style="info" %}
**Note:-** Specify hosts name in the playbook.
{% endhint %}

&#x20;**Inventory file -** For sample inventory file. Sample inventory is available her&#x65;**.**

{% hint style="info" %}
**Note:-** Specify hosts name mentioned in Playbook and server IP.
{% endhint %}

Follow the steps given below:

1. Create new pipeline and create a stage here where **ansiblejob** is located in the drop-down list and select the **ansibleJob** as shown below:

![](https://lh5.googleusercontent.com/86ehE3kNPWB6lk5jq2cz6_UyjjE__c37q59BvAf3uMTUbJkESELTFkFh0QMX4Bf8s1yiMuDcWi5jVplvd0A09L8uwlkchxlTVRUzCle6udAbW6v0nIR1ilszM6F19npXaEd9pilP)

2\. It will ask for the repo URL, ansible file location and inventory file. Mention the entire file path from the repo and the other details. Refer to the image below:

![](https://lh6.googleusercontent.com/69Gt3nuvzRVrKM4cHGIFK0TyQuAc53fsEz6-7UBfdrA5Sjf7FPg0QBMwZ7NiYPtOa8VedWx90o9Vzf0edZUf7bQn4oDBC0tE746u_BJhTaEIeGe2U0JizHpxHnw2CBSOWnxvPhyE)

3\. Save and run the pipeline.
