📜 Как определить группы кластеров в инвентари Ansible

by itisgood

Определим группы кластеров в инвентаризации Ansible.

Определите хосты, которые будут содержать несколько кластеров приложений.

$ cat hosts 
[application:children]
application_cluster_development
application_cluster_production

[application_cluster_development]
dev_server_1 ansible_host=127.0.0.1
dev_server_2 ansible_host=127.0.0.1
dev_server_3 ansible_host=127.0.0.1

[application_cluster_production]
pro_server_1 ansible_host=127.0.0.1
pro_server_2 ansible_host=127.0.0.1
pro_server_3 ansible_host=127.0.0.1

Определите конфигурацию кластера по умолчанию.

cat group_vars/application/application.yml 
---
application__pool: 5

Определите конфигурацию для конкретного кластера.

$ cat group_vars/application_cluster_production/application_cluster.yml 
---
application__pool: 10

Плейбук:

$ cat playbook.yml 
---
- hosts: application
  tasks:
    - name: Determine application cluster group
      set_fact:
        application_cluster_group: '{{ group_names | select("match","application_cluster_*") | first }}'

    - name: Display host groups
      debug:
        var: group_names

    - name: Display application cluster group
      debug:
        var: application_cluster_group

    - name: List cluster servers 
      debug:
        var: groups[application_cluster_group]

    - name: Display per cluster variable
      debug:
        var: application__pool

Выполните созданный плейбук чтобы увидеть, как это работает.

$ ansible-playbook -i hosts playbook.yml
PLAY [application] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************
ok: [dev_server_1]
ok: [pro_server_1]
ok: [pro_server_2]
ok: [dev_server_3]
ok: [dev_server_2]
ok: [pro_server_3]

TASK [Determine application cluster group] ***********************************************************************************************************************************************************
ok: [dev_server_1]
ok: [dev_server_2]
ok: [dev_server_3]
ok: [pro_server_1]
ok: [pro_server_2]
ok: [pro_server_3]

TASK [Display host groups] ***************************************************************************************************************************************************************************
ok: [dev_server_1] => {
    "group_names": [
        "application",
        "application_cluster_development"
    ]
}
ok: [dev_server_2] => {
    "group_names": [
        "application",
        "application_cluster_development"
    ]
}
ok: [dev_server_3] => {
    "group_names": [
        "application",
        "application_cluster_development"
    ]
}
ok: [pro_server_1] => {
    "group_names": [
        "application",
        "application_cluster_production"
    ]
}
ok: [pro_server_2] => {
    "group_names": [
        "application",
        "application_cluster_production"
    ]
}
ok: [pro_server_3] => {
    "group_names": [
        "application",
        "application_cluster_production"
    ]
}

TASK [Display application cluster group] *************************************************************************************************************************************************************
ok: [dev_server_1] => {
    "application_cluster_group": "application_cluster_development"
}
ok: [dev_server_2] => {
    "application_cluster_group": "application_cluster_development"
}
ok: [dev_server_3] => {
    "application_cluster_group": "application_cluster_development"
}
ok: [pro_server_1] => {
    "application_cluster_group": "application_cluster_production"
}
ok: [pro_server_2] => {
    "application_cluster_group": "application_cluster_production"
}
ok: [pro_server_3] => {
    "application_cluster_group": "application_cluster_production"
}

TASK [List cluster servers] **************************************************************************************************************************************************************************
ok: [dev_server_1] => {
    "groups[application_cluster_group]": [
        "dev_server_1",
        "dev_server_2",
        "dev_server_3"
    ]
}
ok: [dev_server_2] => {
    "groups[application_cluster_group]": [
        "dev_server_1",
        "dev_server_2",
        "dev_server_3"
    ]
}
ok: [dev_server_3] => {
    "groups[application_cluster_group]": [
        "dev_server_1",
        "dev_server_2",
        "dev_server_3"
    ]
}
ok: [pro_server_1] => {
    "groups[application_cluster_group]": [
        "pro_server_1",
        "pro_server_2",
        "pro_server_3"
    ]
}
ok: [pro_server_2] => {
    "groups[application_cluster_group]": [
        "pro_server_1",
        "pro_server_2",
        "pro_server_3"
    ]
}
ok: [pro_server_3] => {
    "groups[application_cluster_group]": [
        "pro_server_1",
        "pro_server_2",
        "pro_server_3"
    ]
}

TASK [Display per cluster variable] ******************************************************************************************************************************************************************
ok: [dev_server_1] => {
    "application__pool": 5
}
ok: [dev_server_2] => {
    "application__pool": 5
}
ok: [dev_server_3] => {
    "application__pool": 5
}
ok: [pro_server_1] => {
    "application__pool": 10
}
ok: [pro_server_2] => {
    "application__pool": 10
}
ok: [pro_server_3] => {
    "application__pool": 10
}

PLAY RECAP *******************************************************************************************************************************************************************************************
dev_server_1               : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
dev_server_2               : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
dev_server_3               : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
pro_server_1               : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
pro_server_2               : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
pro_server_3               : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Это просто и удобно.

Например, используйте следующий код для различения нескольких кластеров Kafka, таких как kafka_cluster_logging, kafka_cluster_webapp и так далее.

kafka_cluster_group: '{{ group_names | select("match","kafka_cluster_*") | first }}'

см. также:

 

Loading

You may also like

Leave a Comment