Skip to content

Custom Role

  1. First gain access to our dev server.
  2. Run the startup script. In this instance, we will call it crz-mis-test which is our repo name. This will create a base ansible setup along with your role.
  3. CD into the ansible directory from there you can run tree -d roles to see all of the directories that you now have.
    [plXXXX@VM:lssb0n1a ~]$./custom_skel.sh crz-mis-test
    [plXXXX@VM:lssb0n1a ~]$ cd ansible/
    [plXXXX@VM:lssb0n1a ansible]$ ll
    total 32
    -rw-r--r-- 1 plXXXX users 646 Aug 15  2019 @!
    -rw-r--r-- 1 plXXXX users  99 Mar  5 09:20 ansible.cfg
    drwxr-xr-x 2 plXXXX users  57 Mar 31 15:48 library
    drwxr-xr-x 2 plXXXX users 196 Apr 13 15:34 playbooks
    drwxr-xr-x 4 plXXXX users  48 Apr 14 09:55 roles
    -rw-r--r-- 1 plXXXX users 587 Jan  7 14:15 input.json
    [plXXXX@VM:lssb0n1a ansible]$ tree -d roles/
    roles/
    └── crz-mis-test
        ├── defaults
        ├── handlers
        ├── meta
        ├── tasks
        ├── templates
        └── vars
    
  4. Since this section is only showing how it's done, we will assume that you know ansible and can write your code from here.
    However, I will show a hello world in the format that it should fit into the custom role.
    You don't use playbooks for custom roles, so everything must fit into place with var files, json inputs and the like.

tasks/main.yml

---
# tasks file for crz-standard-playbook
- name: hello world
  debug: 
    msg: "Hello {{ tree }}"

vars/main.yml

---
# vars file for crz-standard-playbooktree: "{{ custom_role.config.tree }}"

input.json

{
    "custom_role": {
        "config": {
            "tree": "oak"
        },
        "hostnames": [
            “lssb0n1a”
        ],
        "roles": [
            "crz-mis-test"
            ]
    }
}

playbooks/test.yml

---
- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Add Host to Dynamic Inventory
      tags: add_hosts
      add_host:
        name: "{{ item }}"
        group: test
      with_items:
        - "{{ custom_role.hostnames }}"

- hosts: test
  roles:
    - crz-mis-test
5. Once you have these files setup, you can test your role.
[plXXXX@VM:lssb0n1a ansible]$ ansible-playbook playbooks/test.yml --extra-vars="@input.json"
6. When this is all completed, please engage Cary or Ted again and we can help setup molecule testing and get the repo into bitbucket.