Apply task on two hosts instead of one if condition is met

Hi everyone,

I thought this was going to be easy but I experienced instant trouble trying to code it.
In my company, we do SAP ERP IT.

Context

I have a playbook to purge and refresh Dollar Universe-managed nodes, that are also SAP nodes. I have written a playbook to automatye this refresh/purge that we mostly run one playbook at the time. The fact is, we use a lot of --extra-vars, so this is why. We planified all the command lines in a crontab -l on the Ansible server, the manager node. So everything is automated.
But I have a problem. I have a new condition : two servers s are now in a master-worker configuration. The main instance is hostap1_AP1 (hostname_SID). The worker is hostpa01_AP1. If a maintenance window is applied on hostap1, the maintenance window should be put at the same time on hostpa01.
Please see the code below for clarification :slight_smile:

Code References (crontab, commands, …)

Command-line exemple in my crontab file :

00 12 5-25 1-3,7-9 0 cd /home/SAP/ansible/scripts/ && ./maintenance_dollaru.sh hostap1_AP1

This is going to launch a script that checks if the maintenance has already happened this year for some nodes, and already happened in the 6 last months for the most loaded ones (production nodes). If so, do nothing and exit script. Else, delete the lock file, launch the command ansible-playbook, and once the playbook has finished properly, create the lock file again. (As you’ve probably noticed, the times chosen for the maintenance window are oddly specific. We have many many conditions to respect…)
Here is an example of the command to launch the purge playbook :

ansible-playbook -i inventory -l hostap1_AP1 --extra-vars="label='Maintenance Dollar Universe' hdeb=07/21/2025,1200 hfin=07/21/2025,1400 ..." playbook_maintenance_sap_dollaru.yml

As I mentioned above, there are many ad-hoc variables. The label is the name of the maintenance window, hdeb and hfin are the beginning and end of the window. The format is, again, oddly specific : dd/mm/yyyy,HHMM.
Task of the maintenance window :

shell: |
   # Set environment 
   ./uxadd out exp type=G label="$LABEL" ostart="$HDEB" oend="$HFIN"
environment :
   LABEL : "{{ label }}"
   HDEB : "{{ hdeb }}"
   HFIN : "{{ hfin }}"

Main problem

I would like, when the target node is hostap1, and for this very specific task of the maintenance window, have a condition that checks if the current target node is indeed this one, and if it is, apply the task on hostpa01_AP1 as well. But only the task, as the other actions for this purge as very risky and we are on production nodes.
Is it possible?

I’m sorry for bad english. I hope you can understand my situation. If you need any further details or explanations, do not hesitate to ask. I’m only an apprenticeship in a work-study program, in a big & secured company. So I cannot do many manipulations on the nodes, especially production nodes, and I do not have an exhaustive view of all the systems. But I will do my best :slight_smile:
The situation seems pretty hopeless to me, so if you have any ideas, do not hesitate as well!
Thanks everyone

Something like this perhaps:

- name: Set environment on any node
  ansible.builtin.shell: |
     ./uxadd out exp type=G label="$LABEL" ostart="$HDEB" oend="$HFIN"
  environment:
     LABEL: "{{ label }}"
     HDEB: "{{ hdeb }}"
     HFIN: "{{ hfin }}"

- name: Set environment on node hostpa01_AP1
  when: inventory_hostname = hostap1_AP1
  delegate_to: hostpa01_AP1
  ansible.builtin.shell: |
     ./uxadd out exp type=G label="$LABEL" ostart="$HDEB" oend="$HFIN"
  environment:
     LABEL: "{{ label }}"
     HDEB: "{{ hdeb }}"
     HFIN: "{{ hfin }}"
2 Likes

Thanks a lot! I didn’t think about delegate_to for another host than localhost. Nice proposition! So easy :slight_smile:

1 Like