1

I have a need to run ansible on a bunch of servers which are active/passive clusters. So, I have an inventory file like

[QA:children]
QA_01_CLUSTER


[QA_01_CLUSTER]
ip-10-361-412-14
ip-10-361-412-30

So serv1qa01 and serv2qa01 could be active/passive in that cluster or the other way around.

I am trying to write a playbook where it will look at which server is secondary, execute the tasks before executing them on the primary. I have an api running on these servers which returns whether the server is primary or secondary. I tried something like this

---
- hosts: all
  serial: 1
  tasks:
   - name: run curl command to determine primary/secondary
     command: /tmp/status.sh
     register: apis_op
   - name: run curl command to determine primary/secondary
     debug: var=apis_op.stdout_lines

   - name: If node is primary add to primary
     add_host:
       name: "{{ ansible_hostname }}"
       groups: temp_primary
       when: apis_op.stdout == "primary"
   - name: run curl command to determine primary/secondary
     debug: var=groups

   - name: If node is secondary add to secondary
     add_host:
       name: "{{ ansible_hostname }}"
       groups: temp_secondary
       when: apis_op.stdout == "secondary"
   - name: run curl command to determine primary/secondary
     debug: var=groups

- hosts: temp_secondary
  tasks:

   - name: Run schell script
     command: /tmp/testthis.sh
     become: yes
     become_user: root

- hosts: temp_primary
  tasks:

   - name: Run schell script
     command: /tmp/testthis.sh
     become: yes
     become_user: root

Here's the entire output with the -v option.

PLAY [all] **********************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host ip-10-361-412-14 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [ip-10-361-412-14]

TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"changed": true, "cmd": ["/tmp/status.sh"], "delta": "0:00:00.038672", "end": "2021-03-29 21:29:15.693979", "rc": 0, "start": "2021-03-29 21:29:15.655307", "stderr": "", "stderr_lines": [], "stdout": "\"secondary\"", "stdout_lines": ["\"secondary\""]}

TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14] => {
    "apis_op.stdout_lines": [
        "\"secondary\""
    ]
}

TASK [If node is primary add to primary] ****************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"add_host": {"groups": ["temp_primary"], "host_name": "ip-10-361-412-14", "host_vars": {"when": "apis_op.stdout == \"primary\""}}, "changed": true}

TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14] => {
    "groups": {
        "all": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "hack": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "temp_primary": [
            "ip-10-361-412-14"
        ],
        "ungrouped": []
    }
}

TASK [If node is secondary add to secondary] ************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"add_host": {"groups": ["temp_secondary"], "host_name": "ip-10-361-412-14", "host_vars": {"when": "apis_op.stdout == \"secondary\""}}, "changed": true}

TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14] => {
    "groups": {
        "all": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "hack": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "temp_primary": [
            "ip-10-361-412-14"
        ],
        "temp_secondary": [
            "ip-10-361-412-14"
        ],
        "ungrouped": []
    }
}

PLAY [all] **********************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host ip-10-361-412-30 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [ip-10-361-412-30]

TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
changed: [ip-10-361-412-30] => {"changed": true, "cmd": ["/tmp/status.sh"], "delta": "0:00:00.038760", "end": "2021-03-29 21:29:17.776237", "rc": 0, "start": "2021-03-29 21:29:17.737477", "stderr": "", "stderr_lines": [], "stdout": "\"primary\"", "stdout_lines": ["\"primary\""]}

TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-30] => {
    "apis_op.stdout_lines": [
        "\"primary\""
    ]
}

TASK [If node is primary add to primary] ****************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-30] => {"add_host": {"groups": ["temp_primary"], "host_name": "ip-10-361-412-30", "host_vars": {"when": "apis_op.stdout == \"primary\""}}, "changed": true}

TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-30] => {
    "groups": {
        "all": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "hack": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "temp_primary": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "temp_secondary": [
            "ip-10-361-412-14"
        ],
        "ungrouped": []
    }
}

TASK [If node is secondary add to secondary] ************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-30] => {"add_host": {"groups": ["temp_secondary"], "host_name": "ip-10-361-412-30", "host_vars": {"when": "apis_op.stdout == \"secondary\""}}, "changed": true}

TASK [run curl command to determine primary/secondary] **************************************************************************************************************************************************************************************
ok: [ip-10-361-412-30] => {
    "groups": {
        "all": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "hack": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "temp_primary": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "temp_secondary": [
            "ip-10-361-412-14",
            "ip-10-361-412-30"
        ],
        "ungrouped": []
    }
}

PLAY [temp_secondary] ***********************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14]
ok: [ip-10-361-412-30]

TASK [Run schell script] ********************************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"changed": true, "cmd": ["/tmp/testthis.sh"], "delta": "0:00:00.040347", "end": "2021-03-29 21:29:20.079991", "rc": 0, "start": "2021-03-29 21:29:20.039644", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [ip-10-361-412-30] => {"changed": true, "cmd": ["/tmp/testthis.sh"], "delta": "0:00:00.040470", "end": "2021-03-29 21:29:20.117223", "rc": 0, "start": "2021-03-29 21:29:20.076753", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

PLAY [temp_primary] *************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [ip-10-361-412-14]
ok: [ip-10-361-412-30]

TASK [Run schell script] ********************************************************************************************************************************************************************************************************************
changed: [ip-10-361-412-14] => {"changed": true, "cmd": ["/tmp/testthis.sh"], "delta": "0:00:00.041023", "end": "2021-03-29 21:29:22.107751", "rc": 0, "start": "2021-03-29 21:29:22.066728", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
changed: [ip-10-361-412-30] => {"changed": true, "cmd": ["/tmp/testthis.sh"], "delta": "0:00:00.041487", "end": "2021-03-29 21:29:22.145928", "rc": 0, "start": "2021-03-29 21:29:22.104441", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
ip-10-361-412-14           : ok=11   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ip-10-361-412-30           : ok=11   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


I have simplified the playbook above but basically I want to run the tasks on secondary before running it on primary for HA reasons.

But this is not working. It's running the playbook in parallel and basically executing tasks on both the hosts at the same time.

How can I force it to run on secondary first before running on primary? I tried to get creative with the serial module or the inventory but is there a dynamic way of doing this?

user3164720
  • 73
  • 1
  • 7
  • 1
    For the cURL (BTW, think about `uri` module) you don't mind doing in parallel, so no need for serial. Then, plays of a playbook are executed in sequence, so first all the secondary group (in batches of 5 by default), then the primary group. What make you say it's not working as expected? Can you add the output of execution to your question? Can you add a debug of the dynamic groups to check they are ok? Are you using a specific `free` strategy? – zigarn Mar 24 '21 at 06:54
  • I don't belive plays are run in paraller, please output. – Jiri B Mar 24 '21 at 09:10
  • So, when I tried the playbook, I had serv1qa01 as primary and serv2qa01 as secondary. The playbook went through the logic fine, but skipped the secondary logic on serv1qa01 as expected and executed the primary tasks. I was hoping the playbook would look at serv2qa01 identify that it is secondary and execute the task there before executing it on serv1qa01. – user3164720 Mar 24 '21 at 13:03
  • Here's the command I ran `ansible playbook -i serv1qa01,serv2qa01 playbook.yml` It ran through the logic, identified serv1qa01 as a primary and then proceeded to execute the playbook on it. I wanted it to execute the playbook on serv2qa01 before executing on serv1qa01 – user3164720 Mar 24 '21 at 17:36
  • So, I tried a few different things. I changed the serial to 1. To test this out, I removed the curl command and created a simple shell script that will output primary for one server and secondary for the other. I feel like the issue is with the register of the output and the when module that's matching the output. Now the playbooks are being executed twice on each server. @zigarn – user3164720 Mar 29 '21 at 00:31
  • Please add the output of execution as asked (edit your question for that) instead of explaining what's going on. Also add a debug of the groups (`debug: var=temp_secondary`), that's the only way to check things are on the right track. From your comments, i would simply say that the condition to compute the secondary group was wrong and that's why the secondary server was ignored. With the output, we should then see a message "no host matching", confirming (or not) this hypothesis. By debugging the groups, you can check your condition is correct. – zigarn Mar 29 '21 at 05:52
  • Also, add a debug of `api_ops` – zigarn Mar 29 '21 at 05:53
  • @zigarn I have added the debugs as suggested and posted the entire output from the playbook. – user3164720 Mar 29 '21 at 21:35
  • `"\"primary\""` and `"\"secondary\""` are printed by your output. So it does not print `primary` but `"primary"` so your conditions should read `when: apis_op.stdout == '"primary"'` and `when: apis_op.stdout == '"secondary"'` – β.εηοιτ.βε Mar 29 '21 at 21:37
  • Still does not work. :( The shell script I wrote on the two servers basically just outputs "primary" or "secondary" – user3164720 Mar 29 '21 at 22:09
  • 2
    You have an indentation issue with your `when`: they have to be at the same level of indentation than `add_host`. Now they are considered as parameter of the module and set as variable of the host. So you're adding all hosts to all groups as there is is no condition in the tasks. Also, would be better to debug the same variable than the one you're trying to use in when (`stdout` vs `stdout_lines`) – zigarn Mar 30 '21 at 05:31
  • 1
    Also you didn't totally follow the comment of @β.εηοιτ.βε as you're missing the single quotes around the double quotes. – zigarn Mar 30 '21 at 05:33
  • It was the indentation. Thanks a lot for all the help folks. – user3164720 Apr 02 '21 at 14:15

0 Answers0