返回
Featured image of post ansible 入門指南 - Control Machine

ansible 入門指南 - Control Machine

Ansible 在 Control Machine 操作 Managed node 前置作業要準備什麼?

安裝

$ brew install ansible
$ ansible --version
ansible [core 2.15.3]
  ... 略

資料夾架構

記得 group_varshost_vars 這兩個資料夾不能改名字,不然會吃不到變數

入門架構

入門用此架構學習

├── hosts              # Managed node
├── site.yml           # ansible 入口
├── ansible.cfg        # ansible 設定檔
├── docker-compose.yml # Docker 版的 Managed node
├── dockerfile         # ubuntu docker image
├── group_vars         # group Variables
│   └── all
└── host_vars          # hosts Variables
    └── all
# 快速使用
$ ansible-playbook site.yml

進階架構

多環境推薦此架構

├── site.yml                # ansible 入口
├── ansible.cfg             # ansible 設定檔
├── inventories             # inventory Managed node 切分
│   └── prod                # production Managed node
│       ├── hosts 
│       ├── group_vars      # group Variables
│       │   ├── group1
│       │   └── group2
│       └── host_vars       # hosts Variables
│           ├── host1
│           └── host2
└── roles                   # 拆成多個 roles
    └── common              # 此資料夾名稱為一個 role
        ├── defaults        # 預設階級
        │   └── main.yml
        ├── files           # 引用之文檔
        │   └── shell.sh
        ├── handlers        # handlers 文件
        │   └── main.yml
        ├── meta            # role 依賴
        │   └── main.yml
        ├── tasks           # tasks 文件
        │   └── main.yml
        ├── templates       # template by j2
        │   └── ntp.conf.j2
        └── vars            # variables 文件
            └── main.yml
# 快速建置資料夾
$ mkdir inventories roles
$ touch site.yml
$ mkdir -p inventories/prod/group_vars
$ mkdir -p inventories/prod/host_vars
$ touch inventories/prod/hosts
$ touch inventories/prod/group_vars/group1
$ touch inventories/prod/host_vars/host1
$ ansible-config init > ansible.cfg
$ ansible-galaxy init roles/common
# 快速使用
$ ansible-playbook -i ./inventories/prod site.yml

ansible.cfg

定義 ansible 執行時設定檔

# 官方預設範例
$ ansible-config init > ansible.cfg

使用 Docker 當作搭建 Managed Node - ansible.cfg撰寫方式

[defaults]
# Managed Node User
remote_user = root
# 不詢問加入SSH金鑰
host_key_checking = False

使用 GCE 當作搭建 Managed Node - ansible.cfg撰寫方式

[defaults]
remote_user = ansible

hosts

使用 Docker 當作搭建 Managed Node - hosts撰寫方式

# 常用舉例:
# ansible_host - Managed Node Host
# ansible_port - Managed Node Port
# ansible_password - Managed Node Password
# ansible_ssh_private_key_file - SSH Private Key file

[sample]
frontend  ansible_host=0.0.0.0  ansible_port=2221 ansible_password=root
backend   ansible_host=0.0.0.0  ansible_port=2222 ansible_password=root

使用 GCE 當作搭建 Managed Node - hosts撰寫方式

選 gce e2-micro 做為練習 一個月預估費用 7.11美,gcp 首次帳號提供300美一年使用

# 常用舉例:
# ansible_host - Managed Node Host
# ansible_port - Managed Node Port
# ansible_password - Managed Node Password
# ansible_ssh_private_key_file - SSH Private Key file

[sample]
app  ansible_host=[External-IP]  ansible_port=[SSH-Port] ansible_ssh_private_key_file=ansible

host 進階架構整理技巧

若像上方範例ansible_hostansible_portansible_password有共同變數可改放置group_varshost_vars

# inventories/prod/group_vars/sample
ansible_host: 0.0.0.0
ansible_port: 2222
ansible_password: root
# inventories/prod/hosts
[sample]
frontend
backend
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus