0

In my school's computer lab, I am tasked to manage a cluster of servers recently and have discovered ansible to be of great help. Being new to it, I need help with the following scenario:

The servers in the cluster all should be configured nearly identically based on a certain condition.

To apply some variables to all the machines in the cluster, I have learned to use group_vars files with the inventory. Nevertheless, there is a situation that I have not figured out what to do.

Depending on the specific operation that these servers are expected to handle, some variables should be set to values as appropriate for the desired condition; the value of each condition can be set to an variable. After some deliberation, I have came to the conclusion the easiest way is to use ansible-playbook's -e option (i.e. external variables).

But where I got stuck is to make the corresponding variables in the group_vars files to take on new values based on the given operation condition variable's value.

Let's call the operation condition variable opv. The variables in a group_vars file let's say var0, var1, var2

  • for opv=0, var0=v00, var1=v01, var2=v02
  • for opv=1, var0=v10, var1=v11, var2=v12
  • for opv=2, var0=v20, var1=v21, var2=v22 etc.

Is there an elegant way to accomplish the above? Thanks in advance for any tips/hints. I am willing to dig. So, just pointers are great.

micassac
  • 3
  • 3

1 Answers1

0

You should switch to yaml format instead of ini, so you can easily using vars and even jinja templating in your group_vars.

#group_vars/all.yml

var0: "v{{ opv }}0"
var1: "v{{ opv }}1"
var2: "v{{ opv }}2"
hd.deman
  • 1,268
  • 12
  • 17
  • thanks for the suggestion. I will try it out and share my results here. – micassac Nov 07 '18 at 23:59
  • I believe combining your hint, together with the pointers in https://stackoverflow.com/questions/41094864/is-it-possible-to-write-ansible-hosts-inventory-files-in-yaml, I have a way to tackle the issue in an elegant way now. Will accept your answer. Thanks. – micassac Nov 08 '18 at 00:12