0
votes

I wish to write a playbook in ansible which will first transfer my package to remote hosts and then run a script. In detail, let's say I have apache package in local machine and need to scp/rsync it to remote nodes A & B. Then I have my script to install the package on A & B both, check whether it was installed properly followed by scrutinizing the config file etc. This script should run only if the transfer is successful.

Have written the below playbook which should meet above requirement. Please confirm if it needs further improvement. Thanks in advance !

Playbook:

---
 - hosts: droplets
   remote_user: root

   tasks:

    - name: Copy package to target machines
      synchronize: src=/home/luckee/apache.rpm dest=/var/tmp/

    - name: Run installation and verification script
      script: /home/luckee/apache_install.sh
      register: result

    - name: Show result
      debug: msg="{{ result.stdout }}"
...
1

1 Answers

1
votes

This way the installation script will only run if the copy tasks changed (was execuded in the process) and exited successfully:

 ---
 - hosts: droplets
   remote_user: root

   tasks:

    - name: Copy package to target machines
      synchronize: src=/home/luckee/apache.rpm dest=/var/tmp/
      register: result_copy


    - name: Run installation and verification script
      script: /home/luckee/apache_install.sh
      register: result_run
      when: result_copy.changed

    - name: Show result
      debug: msg="{{ result_run.stdout }}"
...