Home > Blockchain >  Ruby - change value based on condition
Ruby - change value based on condition

Time:07-03

How can I set the hosts value to be the IP address of the object if the default value is target_vm. If default value is localhost the the hosts value should be localhost.

I'm trying something like this..

hosts=> $evm.root['vm'].floating_ip_addresses if hosts == 'target_vm' else hosts,

Here is the function that I want to modify:

def run_async(env_vars, extra_vars, playbook_path, hosts: ["localhost"], credentials: [], verbosity: 0, become_enabled: false)
  run_via_cli(
    hosts => $evm.root['vm'].floating_ip_addresses || hosts,
    credentials,
    env_vars,
    extra_vars,
    :ansible_runner_method => "start",
    :playbook              => playbook_path,
    :verbosity             => verbosity,
    :become_enabled        => become_enabled
  )
end

CodePudding user response:

As i understand you want a one line if-else statement. If that so you can do it like this:

run_via_cli(hosts => hosts == 'target_vm' ? $evm.root['vm'].floating_ip_addresses : hosts)
  • Related