我需要在一堆主动/被动集群服务器上运行ansible。所以,我有一个像
这样的库存文件[QA:children]
QA_01_CLUSTER
[QA_01_CLUSTER]
ip-10-361-412-14
ip-10-361-412-30
因此 serv1qa01 和 serv2qa01 在该集群中可以是主动/被动的,或者反之亦然。
我正在尝试编写一个剧本,它将查看哪个服务器是辅助服务器,在主服务器上执行任务之前执行任务。 我有一个在这些服务器上运行的 api,它返回服务器是主服务器还是辅助服务器。 我尝试过这样的事情
---
- 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
这是带有 -v 选项的完整输出。
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
我已经简化了上面的剧本,但基本上我想在辅助设备上运行任务,然后再在主设备上运行任务,因为 HA 原因。
但这不起作用。它并行运行剧本,基本上同时在两台主机上执行任务。
如何强制它先在辅助节点上运行,然后再在主节点上运行?我尝试利用串行模块或库存发挥创意,但有没有一种动态的方法来做到这一点?
问:“如何强制它先在辅助节点上运行,然后再在主节点上运行?”
库存示例
[clusters:children]
qa
[qa]
server1qa01.example.com
server2qa01.example.com
一个最小的示例手册
---
- 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
将导致
所需的行为和输出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
更多文档