# 配置 ansible.cfg 與 inventory
### 什麼是 inventory file?
- 定義一個目錄 (inventory) 並讓 Ansible 參照,這樣 Ansible 才會知道該對哪一台主機做什麼事。
```
# 同一個目標需在同一行!!!!!!
#名稱(需和visudo中設定名稱相同)
[群組名]
名稱 ansible_host=IP_address ansible_connection=ssh ansible_ssh_pass=登入密碼 ansible_user=登入用戶
# 組變量
[群組名:vars]
ansible_connection=ssh
ansible_ssh_pass=vagrant
ansible_user=vagrant
```
**運行 ansible-playbook 的時候同時加上 -i 以及 --private-key 的參數來告訴 Ansible 我們的 inventory file 跟 SSH 金鑰的存放位置嗎?我們並不想要在每次運行都要輸入這一大串資料,因此我們可以在 ansible.cfg 裡定義 inventory 跟 private_key_file 來方便我們管理**
``` shell script
ansible-playbook \
--private-key /Users/tsoliang/Desktop/workspace/.vagrant/machines/server/virtualbox/private_key \
-i inventory playbook.yml
```
---
### ansible.cfg放置設定檔位置
- ./ansible.cfg:
- 優先找 ansible 執行指令工作目錄下的檔名
- ~/.ansible.cfg:
- 使用者個人家目錄下的隱藏檔,個人設定
- /etc/ansible/ansible.cfg:
- 前面兩個都不存在,才會使用這一個!
### 在 ansible.cfg 裡定義 inventory 跟 private_key_file 來方便我們管理這類資訊:
``` shell script
[defaults]
private_key_file = /path/to/private_key
inventory = /path/to/inventory
roles_path = /path/to/roles
```
### 設定完成後只須簡單一行指令
``` shell script
ansible-playbook playbook.yml
```
# 爽~蘇胡
###### tags: `Ansible part 1`