Skip to content

Instantly share code, notes, and snippets.

@maprangzth
Last active February 25, 2018 04:03
Show Gist options
  • Select an option

  • Save maprangzth/c0a04d6f0a221bd4f80fb1475fb01b38 to your computer and use it in GitHub Desktop.

Select an option

Save maprangzth/c0a04d6f0a221bd4f80fb1475fb01b38 to your computer and use it in GitHub Desktop.
มาลองเขียน Ansible Inventory File ให้อยู่ในรูปแบบ YAML/JSON กันเถอะ

Ansible Logo

สำหรับท่านที่ศึกษาและใช้งาน Ansible ก็คงจะรู้จักและคุ้นเคยกับ INI Inventory file กันเป็นอย่างดีอยู่แล้ว หน้าตามันก็จะประมาณนี้:

[centos]
centos1 ansible_port=2222
centos[2:3]

[centos:vars]
ansible_user: root

[ubuntu]
ubuntu[1:3]

[ubuntu:vars]
ansible_become=true
ansible_become_pass=password

[linux:children]
centos
ubuntu

ในเมื่อ Ansible เองก็รองรับทั้งที่เป็น YAML และ JSON แล้วทำไมเราไม่ลองกันล่ะครัชชช!!!

YAML Format

Filename: hosts.yaml or hosts.yml

---
centos:
    hosts:
        centos1:
            ansible_port: 2222
        centos2:
        centos3:
    vars:
        ansible_user: root
ubuntu:
    hosts:
        ubuntu1:
        ubuntu2:
        ubuntu3:
    vars:
        ansible_become: true
        ansible_become_pass: password
linux:
    children:
        centos:
        ubuntu:

JSON Format

Filename: hosts.json

{
    "centos": {
        "hosts": {
            "centos1": {
                "ansible_port": 2222
            },
            "centos2": null,
            "centos3": null
        },
        "vars": {
            "ansible_user": "root"
        }
    },
    "ubuntu": {
        "hosts": {
            "ubuntu1": null,
            "ubuntu2": null,
            "ubuntu3": null
        },
        "vars": {
            "ansible_become": true,
            "ansible_become_pass": "password"
        }
    },
    "linux": {
        "children": {
            "centos": null,
            "ubuntu": null
        }
    }
}

มีเว็บที่ช่วย Convert JSON to YAML ด้วยนะ

สรุป

ท้ายที่สุดแล้วไม่ว่าจะเป็น Inventory File แบบไหนพอเอาไปรันผลที่ลัพธ์ที่ได้มันก็จะออกมาเหมือนกันนั่นแหละ ฉะนั้น... เลือกเขียนให้ตรงกับความถนัดของแต่ละท่านจะดีกว่านะ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment