- name: Jmeter test
 hosts: "{{host}}"
 vars:
 # REMOTE_HOSTS, replace the value in jmeter.properties. jmeter_servers, come from ansible-playbook arguments. Format: Master:port;Slave1:port;Slave2:port...
 REMOTE_HOSTS: "{{jmeter_slaves}}"
 # JMETER_MASTER, jmeter master server, also ansible master
 JMETER_MASTER: "{{jmeter_master}}"
 # HOSTS, bb appservers. bb_hosts, come from arguments
 HOSTS: "{{bb_hosts}}"
 # PORT, bb service port
 PORT: "{{bb_port}}"
 # PROTOCOL, http or https
 PROTOCOL: "{{bb_protocol}}"
 # THREADS, jmeter thread count
 THREADS: "{{jmeter_threads}}"
 # RAMPUP, whether rampup server
 RAMPUP: "{{is_rampup}}"
 # LOOPS, jmeter loop count for test plan
 LOOPS: "{{jmeter_loops}}"
 # TEST_ENTRY, which jmx file to be run
 TEST_ENTRY: "{{jmeter_test_entry}}"
 # USERS_PATH, user data file
 USERS_PATH: "{{user_data_file}}"
 GIT_REPO: "https://github.com/scm/perf/jmeter-ultra-acceptance.git"
 GIT_BRANCH: "{{git_branch}}"
 FUSION_HOME: "{{fusion_home}}"
 tasks:
 - name: clean up previous git cloned files
 file: path={{FUSION_HOME}}/stage/jmeter-ultra-acceptance state=absent
 - name: clone code from stash repository
 git: repo={{GIT_REPO}}
 dest={{FUSION_HOME}}/stage/jmeter-ultra-acceptance
 version={{GIT_BRANCH}}
 - name: Generate jmeter properties file
 template: src={{FUSION_HOME}}/stage/jmeter-ultra-acceptance/src/jmeter/jmeter.properties.template 
 dest={{FUSION_HOME}}/stage/jmeter-ultra-acceptance/src/jmeter/jmeter.properties
 - name: Generate user config file
 template: src={{FUSION_HOME}}/stage/jmeter-ultra-acceptance/src/jmeter/config/acceptance_user_config.properties.template
 dest={{FUSION_HOME}}/stage/jmeter-ultra-acceptance/src/jmeter/config/acceptance_user_config.properties
 - name: Check previous Jmeter process
 shell: ps -ef|grep gradle|grep -v grep| awk '{ print $2 }'
 register: ps_result
 - name: Kill all previous gradlew process
 shell: kill -9 `ps -ef|grep gradle|grep -v grep| awk '{ print $2 }'`
 when: ps_result.stdout|default("") != ""
 - name: Check lsof of jmeter-ultra-acceptance folder
 shell: lsof |grep /usr/local/fusion/stage/jmeter-ultra-acceptance |cut -b 11-16
 register: lsof_result
 - name: Kill all process which is using jmeter-ultra-acceptance
 shell: kill -9 `lsof |grep /usr/local/fusion/stage/jmeter-ultra-acceptance |cut -b 11-16`
 when: lsof_result.stdout|default("") != ""
 - name: Start slave jmeter server
 shell: "cd {{FUSION_HOME}}/stage/jmeter-ultra-acceptance/ && {{FUSION_HOME}}/stage/jmeter-ultra-acceptance/gradlew
 clean
 jmeterRun
 --refresh-dependencies
 -PuserProperties={{FUSION_HOME}}/stage/jmeter-ultra-acceptance/src/jmeter/config/acceptance_user_config.properties
 -PsystemProperties={{FUSION_HOME}}/stage/jmeter-ultra-acceptance/src/jmeter/jmeter.properties
 -Pslave=true
 -PtestEntry={{TEST_ENTRY}} "
 when: ansible_hostname != "{{JMETER_MASTER}}"
 async: 1000
 poll: 0 
 - name: Slave status check
 wait_for: port=1099 delay=5 timeout=30
 when: ansible_hostname != "{{JMETER_MASTER}}"
 - name: Start Master jmeter server
 shell: "cd {{FUSION_HOME}}/stage/jmeter-ultra-acceptance/ && {{FUSION_HOME}}/stage/jmeter-ultra-acceptance/gradlew
 clean
 jmeterRun
 --refresh-dependencies
 -PuserProperties={{FUSION_HOME}}/stage/jmeter-ultra-acceptance/src/jmeter/config/acceptance_user_config.properties
 -PsystemProperties={{FUSION_HOME}}/stage/jmeter-ultra-acceptance/src/jmeter/jmeter.properties
 -Pmaster=true
 -PtestEntry={{TEST_ENTRY}}" 
 when: ansible_hostname == "{{JMETER_MASTER}}"
 async: 1000
 poll: 0
 register: jmeter_master_process 
 - name: Jmeter master process status check
 async_status: jid={{ jmeter_master_process.ansible_job_id }}
 register: job_result
 until: job_result.finished
 retries: 30
 when: ansible_hostname == "{{JMETER_MASTER}}"