0

I need rundeck to parse an option based on the value selected in another option. I have an option ${option.env} and other options like ${option.id_dev}, ${option.id_qa}

I want to achieve something like below for extra-vars, so that "env" option value determines which id(dev or qa) to read.

ansible-playbook /build.yml -e id=${option.id_${option.env.value}}

Is this possible or Could I pass extra-vars like a conditional case based on env value ?. I'm using rundeck 3.0.X

Update : To give clear info, If I select 'dev' for the option 'env', I need to use its value like ${option.id_${option.env.value}} , so it translates to ${option.id_dev} to get other option in the command line

enter image description here

DxG
  • 147
  • 4
  • 17

1 Answers1

0

You can use cascade remote options in a tricky way. The explanation is at the end of this answer.

I made a little example to see how to achieve this:

The branches.json file (referenced on the job options as "branches"):

[
 {"name":"branch1", "value":"branch1.json"},
 {"name":"branch2", "value":"branch2.json"},
 {"name":"branch3", "value":"branch3.json"}
]

The branch1.json is the first tentative value of the branches option:

[
 {"name":"v1", "value":"1"},
 {"name":"v2", "value":"2"},
 {"name":"v3", "value":"3"}
]

The branch2.json is the second tentative value of the branches option:

[
 {"name":"v4", "value":"4"},
 {"name":"v5", "value":"5"},
 {"name":"v6", "value":"6"}
]

The branch3.json is the third tentative value of the branches option:

[
 {"name":"v7", "value":"7"},
 {"name":"v8", "value":"8"},
 {"name":"v9", "value":"9"}
]

Full job definition to test:

- defaultTab: summary
  description: ''
  executionEnabled: true
  id: ed0d84fe-135b-41ee-95b6-6daeaa94894b
  loglevel: INFO
  name: CascadeTEST
  nodeFilterEditable: false
  options:
  - enforced: true
    name: branches
    valuesUrl: file:/Users/myuser/branches.json
  - enforced: true
    name: level2
    valuesUrl: file:/Users/myuser/${option.branches.value}
  plugins:
    ExecutionLifecycle: null
  scheduleEnabled: true
  sequence:
    commands:
    - fileExtension: .sh
      interpreterArgsQuoted: false
      script: |+
        #!/bin/sh

        # getting the options
        first=@option.branches@
        second=@option.level2@

        # this just an example
        echo "this an example: ${first%%.*}.$second"

      scriptInterpreter: /bin/bash
    keepgoing: false
    strategy: node-first
  uuid: ed0d84fe-135b-41ee-95b6-6daeaa94894b

Explanation

As you see, the value of the first option is always a file name, if you take the value directly you always obtain an "option1.json" like string, so, the trick here is to cut the file extension and take only the name as the value, for that, I used this approach.

So, with the first selection value and the second one, you can do anything later in a script, for example, launch the ansible-playbook command in the inline script.

Check the example result.

UPDATED ANSWER:

The closest way is just using two options and concatenating them in the command step but isn't possible to get the option value inside the other one as you say.

Just like this:

- defaultTab: nodes
  description: ''
  executionEnabled: true
  id: 4e8df698-c7ca-4a10-9f70-bc68c1007a10
  loglevel: INFO
  name: NewJob
  nodeFilterEditable: false
  options:
  - enforced: true
    name: env
    value: qa
    values:
    - qa
    - prod
    - stage
    valuesListDelimiter: ','
  - enforced: true
    name: id_dev
    value: '1'
    values:
    - '1'
    - '2'
    - '3'
    valuesListDelimiter: ','
  plugins:
    ExecutionLifecycle: null
  scheduleEnabled: true
  sequence:
    commands:
    - exec: echo ${option.env}_${option.id_dev}
    keepgoing: false
    strategy: node-first
  uuid: 4e8df698-c7ca-4a10-9f70-bc68c1007a10

Result.

(the cascade option is another approach)

MegaDrive68k
  • 3,768
  • 2
  • 9
  • 51
  • Thanks for the details. I think your solution concat the values of options which can be used in the script later. But I need to use the value of option 1 to get the value of option 2 in the command line itself. Please look at the update I added. If there is a way to use the conditional statement to pass argument based on option 1, that will work as well. for eg) ansible-playbook /build.yml -e id=if ${option.env} is dev then ${option.id_dev} or ${option.id.qa} – DxG May 04 '22 at 17:43
  • Hi DxG, in the example, the second option depends on the first selection. If you select "branch2" in the first option you will obtain different values for the level2 option. That's the goal of cascade options. At the end, you can call the ansible-playbook in the inline script. But if you really need to call it from the command step, you can generate a couple of data values to pass it to your command later (complicating the workflow indeed). – MegaDrive68k May 04 '22 at 17:57
  • I understand the cascade options. My requirement is little different. When I choose option 1, I don't want to load option 2 values accordingly. Instead, Option 2 already has a fixed value. The value of option 1 is a substring of the name of option 2/Option 3. In my example, the name of option 2 is "id.dev" and its value is xyz. So when 'dev' is chosen as value, I somehow need to use it to retrieve value of id.dev. Here id.dev or id.qa are different option names and not values. Similar to this https://github.com/rundeck/rundeck/pull/3420 – DxG May 04 '22 at 19:01
  • I see, that's not possible right now, I left an update with the closest way to achieve it, anyway, the Cascade Option is another option if you really need a dynamic way. Hope it help! – MegaDrive68k May 04 '22 at 20:00