0

Below is a snippet of yaml I am using. I want to call yaml say abc.yml if a variable ENV is set to, say 'abc', and xyz.yml if the variable ( passed as a parameter ) is set to 'xyz'. How do I use if condition in the yaml ?

 script:
   - ansible-playbook -i <dir> pqr.yaml --verbose -f 10 -e "ENV=${ENV}.... "
   - ansible-playbook -i <dir> abc.yaml --verbose -f 10 -e "ENV=${ENV}.... "
   - ansible-playbook -i <dir> xyz.yaml --verbose -f 10 -e "ENV=${ENV}.... "
   - ansible-playbook -i <dir> hjk.yaml --verbose -f 10 -e "ENV=${ENV}.... "
Shivani
  • 219
  • 1
  • 14

2 Answers2

1

Well you can, using conditions.

The example could be something like below:

  • I used an input parameter as example, any variable will do
  • I separated the scripts in jobs
  • Adding condition will take care of any validation and can therefore skip the job
parameters:

  - name: whatToRunVar
    displayName: 'What should it be: abc or xyz?'
    default: ''
    type: string

jobs:
  - job: pqr
    steps:
    - script: ansible-playbook -i <dir> pqr.yaml --verbose -f 10 -e "ENV=${ENV}.... "

  - job: abc
    steps:
    - script: ansible-playbook -i <dir> abc.yaml --verbose -f 10 -e "ENV=${ENV}.... "
    condition: contains('${{ parameters.whatToRunVar}}', 'abc')

  - job: xyz
    steps:
    - script: ansible-playbook -i <dir> xyz.yaml --verbose -f 10 -e "ENV=${ENV}.... "l
    condition: contains('${{ parameters.whatToRunVar}}', 'xyz')
    
  - job: hjk
    steps:
    - script: ansible-playbook -i <dir> hjk.yaml --verbose -f 10 -e "ENV=${ENV}.... "


Submitting abcde as parameter results in: run with abcde

promicro
  • 1,280
  • 7
  • 14
  • Can we add jobs: in between multiple scripts ? Actually there are multiple ansible-playbok lines in the script and I require the condition check on only 2 ( as mentioned in the question). – Shivani Dec 07 '22 at 07:37
  • Add other jobs in between won't be a problem, as long as they don't have a condition. The condition part of job abc or xyz is letting it run or not, it won't affect other jobs – promicro Dec 07 '22 at 07:58
  • ok. And instead of equal condition, I want to check for a substring condition. How do I do that ? – Shivani Dec 07 '22 at 08:06
  • It's not working that way. Adding jobs: section inside scripts: is giving errors. Any other way to achieve this ? – Shivani Dec 07 '22 at 08:14
  • I hope it gives you an idea now. I want the conditional check just for abc and xyz – Shivani Dec 07 '22 at 08:31
  • updated the example, with all 4 ansible-playbook scenarios, implemented `contains` and even tested it. What do you think @Shivani? – promicro Dec 07 '22 at 08:43
  • I guess you didn't get the scenario. The conditional step is to be used only for abc and pqr. Rest of the steps are mandatory irrespective of the ENV paramater. – Shivani Dec 07 '22 at 09:28
  • Maybe I completely misunderstood you, but in the question you write abc as well as xyz to be evaluated. In my answer I just added the parameter as an example so I could test it, another variable will also work. With `condition` you can skip any jobs and so achieve your goal. – promicro Dec 07 '22 at 10:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250249/discussion-between-promicro-and-shivani). – promicro Dec 07 '22 at 22:05
1

The following piece of code did the trick:

 script:
- if [[ ${ENV} =~ "abc" ]]; then MODULE="abc"; else MODULE="xyz"; fi
- ansible-playbook -i $CI_PROJECT_DIR/ansible/${HOSTFILE} ${MODULE}_installation.yml --verbose -f 10 -e "ENV=${ENV}...
Shivani
  • 219
  • 1
  • 14