สร้าง Droplet DigitalOcean ด้วย Ansible


สิ่งที่ต้องมีในเครื่องที่จะสั่ง Run Ansible

  • python
  • ansible

Structure Project

-|ansible
 |-inventory
 	|- hosts
 |-playbook.yml
  • inventory/hosts sh [do_server] localhost ansible_connection=local
  • playbook.yml
---
- name: creating dropltes in DO
  hosts: do_server

  vars:
    ansible_python_interpreter: /usr/bin/python
    do_token: "<api token>"

  tasks:
    - name: Create droplet
      digital_ocean_droplet:
        state: present
        name: "<name>"
        region: "<region>"
        size: "<size>"
        image: "<image>"
        unique_name: yes
        oauth_token: "{{ do_token }}"

Example

---
- name: creating dropltes in DO
  hosts: do_server

  vars:
    ansible_python_interpreter: /usr/bin/python
    do_token: "xxx"

  tasks:
    - name: Create droplet
      digital_ocean_droplet:
        state: present
        name: "test-create-droplet"
        region: "sgp1"
        size: "1gb"
        image: "ubuntu-20-04-x64"
        unique_name: yes
        oauth_token: "{{ do_token }}"

จากนั้น สั่งทำงาน playbook

$ ansible-playbook -i inventory playbook.yml

...

PLAY [creating dropltes in DO] ********************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [localhost]

TASK [Create droplet] **************************************************************************************************
changed: [localhost]

PLAY RECAP *************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

image1.png