Chef If else condition on .yml playbook?

Hello all,

I am working on a Chef and Ansible Playbook in which I would like to know if I can add a conditional if/else.

Currently, the block of code is:

- name: set any required host vars
        ip: "{{ hostvars[inventory_hostname]['ansible_host']}}"
        chef_environment: production
        dc_oob: "{{ hostvars[inventory_hostname]['datacenter_oob']}}" # dc_oob: "oob-abc1230008"
        run_list: "recipe[fst_base::_bootly_setup]"
        provisioning_run_list: "recipe[fst_base::default],recipe[fst_apt::default],recipe[fst_base_extended::default],recipe[fst_app_provisioning_workflow::cache]"
        final_run_list: "recipe[fst_base::default],recipe[fst_apt::default],recipe[fst_base_extended::default]"
        vault_json: "{{ vaults_json_output.stdout | from_json }}"

But in this line:
dc_oob: "{{ hostvars[inventory_hostname]['datacenter_oob']}}" # dc_oob: "oob-abc1230008" instead of changing this by editing the .yml file everytime, I am wondering if I can add some condition in which I get option/prompt for entering that piece of data e.g. "oob-abc1230007" or "oob-abc1230008"

Make sense?

since this is an ansible playbook it's going to be restricted to the bounds that ansible has on .yml and conditionals.

When passed to chef if there is say an attribute with string interpolation you can set multiple values then based on a conditional. Since it'd get written to a .rb file and loaded/interpreted by chef during it's run I'd think it would work.

I have never tried anything like this so this is all hypothetical.

1 Like

Thank you very much, I will consider testing your hypothesis as well, and I will let you know what worked, I really appreciate the response.