Ansible Playbook

This guide demonstrates the basic concepts around creating and executing an Ansible playbook service within torero.

Prerequisites

It is a prerequisite that a Git repository be setup with the Ansible playbook within it. Review the Create Repository command to understand on how to create a repository.

Create

torero create service ansible-playbook will perform a creation of the Ansible Playbook service. A more detailed guide of all creation options is available there but this guide should help get you started.

The command shown below creates an Ansible playbook service within the torero called simple-ansible. We are leveraging a repository that would have been previously configured called torero-resources.

>_ torero create service ansible-playbook simple-ansible --repository torero-resources --working-dir ansibleplaybooks --playbook hello-world.yml

It is important to stop and understand the structure of torero-resources before moving further.

torero-resources (git repo)

├── README.md
├── ansibleplaybooks
│   ├── hello-world.yml
│   ├── requirements.txt
│   └── requirements.yml
├── pythonscripts
└── opentofuplans

We specified that we want to use torero-resources via the --repository flag.

Notice that our Ansible playbook exists in a directory called ansibleplaybooks. We denoted this using the --working-dir flag.

Inside the ansibleplaybook directory, we have an actual playbook called hello-world.yml. We denoted this with the --playbook flag.


Verify Creation

We can view details about the previously created Ansible playbook service by running the torero describe service command.

>_ torero describe service simple-ansible
Output:

Successfully created the Ansible playbook(s)
Name:        simple-ansible
Repo Name:   torero-resources
Working Dir: ansibleplaybooks
Playbook(s): hello-world.yml
Decorator:   
Description: 
Tags:        
Runtime Arguments:

Execution

Executing an Ansible playbook is simple from torero by utilizing the run command. Depending on what the playbook is doing though we may want to pass variables in at runtime.

If we look at the playbook that was created hello-world.yml we can see there is a variable for caller.

---
- name: A Simple Hello World Example
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Just Say Hello
      debug:
        msg: "Hello Mr. torero this is from '{{ caller }}'"⏎

If the variable is not passed there will be an error, so we need to ensure that a variable is passed. This can be done using the --set flag.

>_ torero run ansible-playbook simple-ansible --set caller=documentation
Output:

Start Time:   2024-01-01T12:00:00Z
End Time:     2024-01-01T12:00:01Z
Elapsed Time: 1.372672s
Return Code: 0
Stdout:      
PLAY [A Simple Hello World Example] ********************************************

TASK [Just Say Hello] **********************************************************
ok: [localhost] => {
    "msg": "Hello Mr. torero this is from 'documentation'"
}

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


Stderr: [WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

Do note that you can add additional vars with the --set syntax when running any service.

torero run ansible-playbook simple-ansible --set caller=documentation --set another=one --set howabout=another

It is possible to put restrictions around the inputs that are accepted by an Ansible playbook by utilizing decorators. For more information on decorators, please refer to this guide.


CLI Reference

Ansible Playbook Create

Ansible Playbook Run

Services Get

Service Describe

Service Delete