0

I am new to ansible and I am wondering how I should design my workflow with ansible in an ansible kind of way.

The Task: I have a workflow/process (not ansible yet) for setting an application up. This workflow has to do serveral tasks on different hosts and to store some configuration in git repositories. Depending on the tenend (e.g. A and B) of my application, it has to setup different stuff on different hosts. My workflow from top to down:

# host tenand task description
1 localhost A & B Create configurations and store it in an git repo and push to bitbucket
2 localhost A & B Setup an git repo with basic configs and push to bitbucket
3 localhost A & B Setup some other configs and post it with HTTP to a service
4.1 host a,b,c A Create a basic filesystem (directory and files)
4.2 host a,d B Create a basic filesystem (directory and files)
5.1 host a,b,c A Restart a deamon
5.2 host a,d B Restart a deamon

Thoughts: I did create a playbook in exactly this manner and tried to differ between the hosts depending on the tenand an in its own configuration file. However thats exactly the way I am used to by an workflow orchestraton tool. Is it the ansible way too?
Do I have to change my perspective to the hosts and should create a playbook for each host and decide during the runtime if there are tasks for this host?

1 Answer 1

0

It depends mostly on How one build his inventory and has organized hosts and group variables. As well what kind of services and configuration needs to be addressed and if there are dependencies between them.

One of many possibilities is for an inventory file example.com

[tenants:children]
A
B

[A]
a.example.com
b.example.com
c.example.com

[B]
a.example.com
d.example.com

a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  pre_tasks:

  - name: Create configurations, store it in a Git repo and push it into Bitbucket
    debug:
      msg: "1"

  - name: Setup a Git repo with basic configs and push it into Bitbucket
    debug:
      msg: "2"

  - name: Setup some other configs and post it with HTTP to a service
    debug:
      msg: "3"

- hosts: A
  become: false
  gather_facts: false

  tasks:

  - name: Create a basic filesystem (directory and files)
    debug:
      msg: "4.1"

  - name: Restart a daemon
    debug:
      msg: "5.1"

- hosts: B
  become: false
  gather_facts: false

  tasks:

  - name: Create a basic filesystem (directory and files)
    debug:
      msg: "4.2"

  - name: Restart a daemon
    debug:
      msg: "5.2"

Called via

ansible-playbook --inventory example.com main.yml

it will result into an output of

PLAY [localhost] ************************************************************************************************************************************************

TASK [Create configurations, store it in a Git repo and push it into Bitbucket] *********************************************************************************
ok: [localhost] =>
  msg: '1'

TASK [Setup a Git repo with basic configs and push it into Bitbucket] *******************************************************************************************
ok: [localhost] =>
  msg: '2'

TASK [Setup some other configs and post it with HTTP to a service] **********************************************************************************************
ok: [localhost] =>
  msg: '3'

PLAY [A] ********************************************************************************************************************************************************

TASK [Create a basic filesystem (directory and files)] **********************************************************************************************************
ok: [a.example.com] =>
  msg: '4.1'
ok: [b.example.com] =>
  msg: '4.1'
ok: [c.example.com] =>
  msg: '4.1'

TASK [Restart a daemon] *****************************************************************************************************************************************
ok: [a.example.com] =>
  msg: '5.1'
ok: [b.example.com] =>
  msg: '5.1'
ok: [c.example.com] =>
  msg: '5.1'

PLAY [B] ********************************************************************************************************************************************************

TASK [Create a basic filesystem (directory and files)] **********************************************************************************************************
ok: [a.example.com] =>
  msg: '4.2'
ok: [d.example.com] =>
  msg: '4.2'

TASK [Restart a daemon] *****************************************************************************************************************************************
ok: [a.example.com] =>
  msg: '5.2'
ok: [d.example.com] =>
  msg: '5.2'
2
  • 1
    It might be worth to mention why did you used pre_tasks in the first play or replace it with tasks. Since there is no roles, I guess it might be misinterpreted by novices. Feb 27 at 9:02
  • @AlexanderPletnev, right, a hint regarding pre_tasks should be linked here as it will help one if going to implement one of the many other possibilities with Roles.
    – U880D
    Feb 27 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.