Reputation: 71
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?
Upvotes: 0
Views: 1175
Reputation: 12029
Q: "How can I force it to run on secondary first before running on primary?"
For an example inventory
[clusters:children]
qa
[qa]
server1qa01.example.com
server2qa01.example.com
a minimal example playbook
---
- hosts: clusters
become: false
gather_facts: false
pre_tasks:
# Taks to mimic Gathering Custom Facts
# Result can be true or false
- name: Gathering Facts (Custom)
set_fact:
PRIMARY: "{{ 2 | random }}"
register: REST_API
- debug:
var: REST_API.ansible_facts.PRIMARY
tasks:
- name: Run on SECONDARY node first
debug:
msg: "SECOND"
when: not PRIMARY | bool
- name: Run on PRIMARY node after
debug:
msg: "PRIME"
when: PRIMARY | bool
will result into the required behavior and output of
PLAY [clusters] *********************
TASK [Gathering Facts (Custom)] *****
ok: [server1qa01.example.com]
ok: [server2qa01.example.com]
TASK [debug] ************************
ok: [server1qa01.example.com] =>
REST_API.ansible_facts.PRIMARY: '0'
ok: [server2qa01.example.com] =>
REST_API.ansible_facts.PRIMARY: '1'
TASK [Run on SECONDARY node first] **
ok: [server1qa01.example.com] =>
msg: SECOND
TASK [Run on PRIMARY node after] ****
ok: [server2qa01.example.com] =>
msg: PRIME
PLAY RECAP **************************
server1qa01.example.com : ok=3
server2qa01.example.com : ok=3
Further Documentation
Upvotes: 0