1

我需要在一堆主动/被动集群的服务器上运行 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 的原因,基本上我想先在辅助服务器上运行任务,然后再在主服务器上运行它。

但这不起作用。它并行运行剧本,基本上同时在两台主机上执行任务。

在主服务器上运行之前,如何强制它先在辅助服务器上运行?我试图通过串行模块或库存来获得创意,但有没有一种动态的方式来做到这一点?

4

0 回答 0