Ansible Service
  • 29 Oct 2024
  • Dark
    Light
  • PDF

Ansible Service

  • Dark
    Light
  • PDF

Article summary

Ansible Playbook

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

Prerequisites

  • You will need a Git repository setup with the Ansible playbook within it.

  • Review the Create Repository command to understand how to create a repository.

Create Ansible Playbook

The iagctl create ansible-playbook command will create the Ansible Playbook service.

More detail on all creation commands is available here.

The command shown below creates an Ansible playbook service within IAG5 called simple-ansible that will leverage a previously configured repository called gateway-resources.

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

Ansible Playbooks Directory

Important information to understand the structure of gateway-resources is presented below.

gateway-resources has been specified for use via the --repository flag

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

Notice that the Ansible Playbook exists in a directory called ansibleplaybooks and is denoted using the --working-dir flag.

Inside the ansibleplaybook directory there is a playbook called hello-world.yml. This is denoted with the --playbook flag.

Verify Ansible Playbook Service

Details about the previously created Ansible playbook service can be viewed by running the iagctl describe service command.

More detail on all iagctl-describe commands is available here.

>_ iagctl describe ansible-playbook simple-ansible
Output:

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

Execute Ansible Playbook

Executing an Ansible playbook is simple from IAG5 by utilizing the run ansible-playbook command.

Depending on what the playbook is doing, variables can be passed in at runtime.

Looking at the playbook that was created, hello-world.yml, users 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. gateway this is from '{{ caller }}'"⏎

Ensure the variable is passed using the --set flag. If the variable is not passed there will be an error.

>_ iag5 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. gateway 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'

Users can add additional vars with the --set syntax when running any service.

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

Decorators

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 References

For command references see → Command References Index

  • iagctl create service ansible-playbook

  • iagctl run ansible-playbook

  • iagctl get services

  • iagctl describe service

  • iagctl delete service


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.