Vasyl Stepulo
Vasyl Stepulo

Reputation: 1603

Pass correct variable to task, depends on other variable in Ansible

I'm passing IP of the server in jenkinsfile to playbook this way:

withEnv(["ENV_SERVER=192.168.100.70"]) {
    sh "ansible-playbook mbo-v2.yml --limit 127.0.0.1 -u deploy"

Then, I need to pass unique suffix to filename, depends on value of ENV_SERVER variable:

- name: copy parameters
  shell: ssh deploy@{{ ENV_SERVER }} sudo cp -f /{{ params_APP_PATH }}/{{ params_NGINX_VHOST }}/app/config/parameters.yml.{{ params_UML_SUFFIX }}.dist /{{ params_APP_PATH }}/{{ params_NGINX_VHOST }}/app/config/parameters.yml
  become: true
  become_user: deploy

How to define multiple "when" parameter to pass correct params_UML_SUFFIX variable? E.g.:

 if ENV_SERVER == '192.168.100.70' then params_UML_SUFFIX == 'gib-production'
 if ENV_SERVER == '192.168.110.80' then params_UML_SUFFIX == 'lux-production'
 if ENV_SERVER == '192.168.120.90' then params_UML_SUFFIX == 'ara-production'

Upvotes: 1

Views: 42

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68134

Create a dictionary, e.g.

params_UML_SUFFIX:
  192.168.100.70: gib-production
  192.168.110.80: lux-production
  192.168.120.90: ara-production

and use it in the run-string, e.g.

- name: copy parameters
  shell: ssh ... parameters.yml.{{ params_UML_SUFFIX[ENV_SERVER] }}.dist ...
  become: true
  become_user: deploy

Upvotes: 1

Related Questions