1. 各目錄含義解釋
roles: <--所有的角色必須放在roles目錄下,這個目錄可以自定義位置,默認的位置在/etc/ansible/roles
project: <---具體的角色項目名稱,比如nginx、tomcat、php
files: <--用來存放由copy模塊或script模塊調(diào)用的文件。
templates: <--用來存放jinjia2模板,template模塊會自動在此目錄中尋找jinjia2模板文件。
tasks: <--此目錄應當包含一個main.yml文件,用于定義此角色的任務列表,此文件可以使用include包含其它的位于此目錄的task文件。
main.yml
handlers: <--此目錄應當包含一個main.yml文件,用于定義此角色中觸發(fā)條件時執(zhí)行的動作。
main.yml
vars: <--此目錄應當包含一個main.yml文件,用于定義此角色用到的變量。
main.yml
2. Ansible Roles示例
通過ansible roles安裝配置nfs服務,此處的roles不使用默認的路徑/etc/ansible/roles
1. 搭建結構
mkdir /mnt/myansible_roles
cd /mnt/myansible_roles
創(chuàng)建ansible.cfg
cat > ansible.cfg << EOF
[defaults]
inventory = hosts
sudo_user = root
transport = smart
remote_port = 22
remote_user = root
log_path = /mnt/myansible_roles/logs/ansible.log
deprecation_warnings = False
command_warnings = False
host_key_checking = False
interpreter_python = auto_legacy_silent
# gather_facts = no
EOF
創(chuàng)建hosts
cat > hosts << EOF
[nfsServer]
192.168.137.248
EOF
mkdir logs
mkdir -p roles/nfs-server/{tasks,vars,files,templates,handlers}
tree
.
├── ansible.cfg
├── hosts
├── logs
├── roles
│ └── nfs-server
│ ├── files
│ ├── handlers
│ ├── tasks
│ ├── templates
│ └── vars
└── site.yaml
2. 編寫nfs-server
cd /mnt/myansible_roles/roles/nfs-server
# 創(chuàng)建tasks/main.yaml
cat > tasks/main.yaml << EOF
- name: install nfs-utils rpcbind
yum:
name:
- nfs-utils
- rpcbind
state: installed
- name: create data directory
file:
path: "{{ data_dir }}"
state: directory
owner: nfsnobody
group: nfsnobody
# ignore_errors: True
#- name: create config file
# copy: src="exports" dest="/etc/"
# notify:
# - restart nfs
#- name: create config file
# copy:
# src: exports
# dest: /etc/
# notify:
# - restart nfs
#- name: create config file
# template: src="exports.j2" dest="/etc/exports"
# notify:
# - restart nfs
- name: create config file
template:
src: exports.j2
dest: /etc/exports
notify:
- restart nfs
- name: start process
service:
name: "{{ item }}"
enabled: yes
state: started
with_items:
- rpcbind
- nfs
- name: check server
#shell: /usr/sbin/showmount -e localhost
shell: /usr/sbin/showmount -e "{{ansible_eth0.ipv4.address}}"
register: result
- name: display result
debug: msg={{ result.stdout_lines }}
EOF
# 創(chuàng)建vars/main.yaml
cat > vars/main.yaml << EOF
data_dir: /sharding_data
EOF
# 創(chuàng)建handlers/main.yaml
cat > handlers/main.yaml << EOF
- name: restart nfs
service:
name: nfs
state: restarted
enabled: yes
EOF
# 創(chuàng)建files/exports
cat > files/exports << EOF
/sharding_data/ 192.168.137.0/24(rw,sync)
EOF
# 創(chuàng)建templates/exports.j2
cat > templates/exports.j2 << EOF
{{ data_dir }} 192.168.137.0/24(rw,sync)
EOF
.
├── files
│ └── exports
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── exports.j2
└── vars
└── main.yaml
3. 測試,執(zhí)行
cd /mnt/myansible_roles/
cat > site.yaml << EOF
---
- hosts: all
roles:
- nfs-server
EOF
# 語法檢查
ansible-playbook --syntax-check site.yaml
# 預執(zhí)行
ansible-playbook -C site.yaml
# 如果都沒問題就可以執(zhí)行
ansible-playbook site.yaml
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。