Bakey.us - Blog

Deterministic Ansible

Recently I had to deploy AppDynamics agents out to some systems that ran both tomcat and JBOSS workloads, and here’s how I used one playbook to rule them all.


1 - Figure out which platform I was dealing with

#check to see if we're tomcat or jboss
- name: Check to see if tomcat user exists
  stat:
    path: /home/tomcat8
  register: tomcat8

- name: check to see if jbossadm user exists
  stat:
    path: /home/jbossadm
  register: jbossadm

As you can see, I’m doing a simple stat module check on the home directory of the user running the java workload here. In our use case those 2 users would never coexist on a server.

2 - Swap ownership on files/folders based on our findings above

#creates directory path
- name: Create the directories for site specific configurations owned by tomcat8
  file:
    path: /usr/local/appdynamics/appagent/
    state: directory
    owner: tomcat8
    group: tomcat8
    mode: 0755
  when: tomcat8.stat.exists

- name: Create the directories for site specific configurations owned by jbossadm
  file:
    path: /usr/local/appdynamics/appagent/
    state: directory
    owner: jbossadm
    group: jbossadm
    mode: 0755
  when: jbossadm.stat.exists

So, in each case we’re creating the same folder path and changing linux file system ownership based on which platform we’re running this ansible playbook on.

The stat module and when conditions are pretty powerful tools you can use to make Ansible super flexible without having to pass a bunch of extra parameters in to control which tasks get executed in your playbooks.