Newbie question on ansible inventory?

100 views
Skip to first unread message

Aaron Eldridge

unread,
Sep 14, 2015, 8:20:07 AM9/14/15
to Ansible Project
Hi there, Im just starting with ansible so I appologise if this is a stupid question.

I have am wanting to try do use ansible to deploy .war web app's to several QA servers. Each QA server hosts serveral instances of the app's across multiple instances of tomcat per server (production is 1 per server).

I was wanting to write a play book that would connect to each QA server, copy the war files to each insteance on tomcat in on the server then move onto the next box.

The problem I originally had was I put each app as its own inventory item (with the same host name), I found that this did not work. So I now have just the 4 QA servers in there now.

Is there a good way do declare what app should be on what tomcat under each server in the inventory or should that be in the playbook?

Thank you for reading.

Aaron Eldridge

unread,
Sep 14, 2015, 9:37:13 AM9/14/15
to Ansible Project
At the moment I have a playbook file that looks like this (though Im sure there is a more eligant way to do this):

- hosts: QA
  tasks:
   - name: copy war file
     action: copy src=/staging/staging.war dest=/tmp/staging.war

- hosts: AGGQA
  tasks:
   - name: rename app1 war file
     shell: mv /usr/local/tomcat1/webapps/app1.war /usr/local/tomcat1/webapps/app1.war.{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}
     when: ansible_hostname == "qa01"
   - name: rename app2 war file
     shell: mv /usr/local/tomcat2/webapps/app2.war /usr/local/tomcat2/webapps/app2.war.{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}
     when: ansible_hostname == "qa01"
   - name: rename app3 war file
     shell: mv /usr/local/tomcat1/webapps/app3.war /usr/local/tomcat1/webapps/app3.war.{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}
     when: ansible_hostname == "qa02"
   - name: rename app4 war file
     shell: mv /usr/local/tomcat2/webapps/app4.war /usr/local/tomcat2/webapps/app4.war.{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}
     when: ansible_hostname == "qa02"

  - name: copy new app1 war file  
     shell: cp /tmp/staging.war /usr/local/tomcat1/webapps/app1.war
     when: ansible_hostname == "qa01"
  - name: copy new app2 war file  
     shell: cp /tmp/staging.war /usr/local/tomcat2/webapps/app2.war
     when: ansible_hostname == "qa01"
  - name: copy new app3 war file  
     shell: cp /tmp/staging.war /usr/local/tomcat1/webapps/app3.war
     when: ansible_hostname == "qa02"
  - name: copy new app4 war file  
     shell: cp /tmp/staging.war /usr/local/tomcat3/webapps/app4.war
     when: ansible_hostname == "qa02"

  - name: Set permission's on new app1 war file  
     shell: chown tomcat:tomcat /usr/local/tomcat1/webapps/app1.war
     when: ansible_hostname == "qa01"
  - name: Set permission's on new app2 war file  
     shell: chown tomcat:tomcat /usr/local/tomcat2/webapps/app2.war
     when: ansible_hostname == "qa01"
  - name: Set permission's on new app3 war file  
     shell: chown tomcat:tomcat /usr/local/tomcat1/webapps/app3.war
     when: ansible_hostname == "qa02"
  - name: Set permission's on new app4 war file  
     shell: chown tomcat:tomcat /usr/local/tomcat2/webapps/app4.war
     when: ansible_hostname == "qa02"

Frank Perks

unread,
Sep 14, 2015, 12:11:10 PM9/14/15
to Ansible Project
Hi,

The easiest way imo is to define groups in your inventory. Group the servers that use the same war file togther, and then add a corresponding var file for that group in group_vars. In group vars define a list of the wars on that server. 

E.g.: qa01.yml

war_files:
 
- /something/app1.war
 
- /something/app2.war


Then in your playbook you can simply just call:

tasks:
 
- name: rename war files
   shell
: cp /tmp/staging.war /usr/local/tomcat1/webapps/{{ item }}
   with_items
: war_files


Depending on which server you are on, it will deploy the specific war files defines in your corresponding group vars
Reply all
Reply to author
Forward
0 new messages