Shashank Agrawal
Shashank Agrawal

Reputation: 345

How to override some specific variables values in Ansible playbook

I have some multiple war files which I am downloading from nexus and creating the respective app folder on my remote host and copying the files. Now there are 2 files which have a different name as war file but need to create a directory with different name. What I am doing right now is this -

- name: Ensuring the web deployment folder is created on tomcat nodes for each web application
   file:
     path: "{{tomcat_server_release_location}}/Release{{my_release_version}}/{{item}}"
     state: directory
     mode: 0777
   with_items:
    - "{{ apps }}"

- name: Copying the release from Admin server to tomcat nodes
   copy: src={{admin_server_release_location}}/{{my_release_version}}/{{item}}-{{my_release_version}}.war dest={{tomcat_server_release_location}}/Release{{my_release_version}}/{{item}}
   with_items:
    - "{{ apps }}"

apps variable is defined like this - webapps: ['test1','test2','test3','test4']. Now test2 has a different name on the nexus it says test2-web.war but on the remote node I have to create a folder called just test2 and copy the war file in there.

Is there a way to override some variables at run time of playbook by using some condition or anything

Upvotes: 1

Views: 11864

Answers (2)

liad9122
liad9122

Reputation: 411

It is possible to override variables by doing something like this:

- name: copy war
  copy:
    src: "{{ src_war | default('/path/to/my/war') }}"
    dest: "{{ dest_war | default(src_war) }}"

Then at runtime you can set the variables src_war and dest_war either with extra vars, host/group_vars or any other way documented here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html

In your case I think it would be helpful to set a src & dest mapping:

- set_fact:
    apps:
      - {src: "test1"}
      - {src: "test2", dest: "test2-web"}
      - {src: "test3"}
      - {src: "test4"}

Then you can use this mapping to achieve your goal:

- name: Ensuring the web deployment...
  file:
    path: {{ tomcat_server_release_location }}/Release{{ my_release_version }}/{{ item.src }}
    state: directory
    mode: 0777
  with_items: "{{ apps }}"

- name: Copying the release...
  copy:
    src: "{{ admin_server_release_location }}/{{ my_release_version }}/{{ item.src }}-{{ my_release_version }}.war"
    dest: "{{ tomcat_server_release_location }}/Release{{ my_release_version }}/{{ item.dest | default(item.src) }}"
  with_items: "{{ apps }}"

Upvotes: 1

zeyrkelian
zeyrkelian

Reputation: 13

If you want to override at runtime use extra vars. From the docs --extra-vars "version=1.23.45"

Don't use conditions if you know a specific host should always be overwritten. I think placing it as a host var is best.

hosts: myhost: apps: - test1 - trst2

See https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable for all of the various locations you can put variables and how to determine which will be used

Upvotes: 1

Related Questions