環境都為Centos 7.4主機
192.168.241.100管理節點 192.168.241.101被控端 192.168.241.102被控端
官方使用文檔說明:https://docs.ansible.com/ansible/latest/user_guide/playbooks.html
首先建立一個工作目錄:
[roo[email protected] ~]# mkdir playbook
[[email protected] ~]# cd playbook/
編寫一個簡單的下載下傳nginx服務并且啟動設定成開機啟動的nginx.yaml檔案(代碼中出現中文就有會出錯)
- hosts: webservers
remote_user: root
gather_facts: false
tasks:
- name: nginx latest
yum: pkg=nginx state=latest
- name: start nginx
systemd: name=nginx state=started enabled=yes
運作我們的yaml檔案:
[[email protected] playbook]# ansible-playbook nginx.yaml
yaml檔案也可以寫成如下:
---
- hosts: webservers
vars:
http_port: 80
server_name: www.ctnrs.com
remote_user: root
gather_facts: false
tasks:
- name: 安裝nginx最新版
yum: pkg=nginx state=latest
- name: 寫入nginx配置檔案
template: src=/srv/httpd.j2 dest=/etc/nginx/nginx.conf
notify:
- restart nginx
- name: 確定nginx正在運作
service: name=httpd state=started
handlers:
- name: restart nginx
service: name=nginx state=reloaded
定義變量:
給是以組進行定義變量的步驟:
1.首先我們需要在建立一個目錄(預設引用的目錄)用來存放定義變量檔案
[[email protected] ~]# mkdir /etc/ansible/group_vars
我們建立一個yml的變量檔案(裡面存放的都是自己給所有組定義的變量)
[[email protected] group_vars]# vim all.yml
love : 520
ldy : 1314
我們測試一下是否能輸出變量:
檢視所有組的變量資訊
[[email protected] group_vars]# ansible all -m debug -a "var=love"
[[email protected] group_vars]# ansible all -m debug -a "var=ldy"
檢視單獨webservers變量資訊
[[email protected] group_vars]# ansible webservers -m debug -a "var=love"
[[email protected] group_vars]# ansible webservers -m debug -a "var=ldy"
針對于webeservers主機組的變量定義
[[email protected] group_vars]# vim /etc/ansible/group_vars/webservers.yml
我們進行驗證一下k8s變量
[[email protected] group_vars]# ansible webservers -m debug -a " var=k8s"
在Playbook中定義變量
- hosts: webservers
vars:
http_port: 8080
server_name: www.ldy.com
編寫一個nginx.yaml檔案
[[email protected] playbook]# vim nginx.yaml
- hosts: webservers
remote_user: root
gather_facts: false
vars:
http_port: 80
server_name: www.ldy.com
tasks:
- name: nginx latest
yum: pkg=nginx state=latest
- name: start nginx
systemd: name=nginx state=started enabled=yes
進行安裝
Register變量
- shell: /usr/bin/uptime
register: result
- debug:
var: result
[[email protected] playbook]# vim nginx.yaml
第一個debug是輸出http_port的端口80, 第二個debug是列印執行shell指令後把結果儲存在注冊的變量result中然後利用debug進行列印出來(如果直接列印result的話會列印字典格式,很多沒有用的資訊,然我們隻要stdout對應的鍵值,是以·來引用)
- hosts: webservers
remote_user: root
gather_facts: false
vars:
http_port: 80
tasks:
- name: start nginx
systemd: name=nginx state=started enabled=yes
- debug: var=http_port
- name:
shell: |
netstat -anput | grep 80
register: result
- debug: var=result.stdout
文法檢查與調試
文法檢查:ansible-playbook --check /path/to/playbook.yaml
測試運作,不實際操作:ansible-playbook -C /path/to/playbook.yaml
debug子產品在執行期間列印語句,對于調試變量或表達式,而不必停止play。與'when:'指令一起調試更佳。
任務控制
如果你有一個大的playbook,那麼能夠在不運作整個playbook的情況下運作特定部分可能會很有用。
tasks:
- name: 安裝nginx最新版
yum: pkg=nginx state=latest
tags: install
- name: 寫入nginx配置檔案
template: src=/srv/httpd.j2 dest=/etc/nginx/nginx.conf
tags: config
使用:
ansible-playbook example.yml --tags "install" 執行一個tag任務
ansible-playbook example.yml --tags "install,config" 執行多個tag任務
ansible-playbook example.yml --skip-tags "install" 跳過指定的tag任務
例如,我運作reg變量的任務:
- hosts: webservers
remote_user: root
gather_facts: false
vars:
http_port: 80
tasks:
- name: start nginx
systemd: name=nginx state=started enabled=yes
- debug: var=http_port
- name:
shell: |
netstat -anput | grep 80
register: result
tags: reg
- debug: msg={{result}}
tags: reg
- debug: msg={{group_names}}
[roo[email protected] playbook]# ansible-playbook nginx.yaml --tags=reg
流程控制
[[email protected] playbook]# vim nginx.yaml
echo 123輸出的123會儲存在result中,然後如果result==123則執行reg這個任務(when裡家條件判斷為真執行,為假跳過)
- hosts: webservers
remote_user: root
gather_facts: false
vars:
http_port: 80
tasks:
- name: start nginx
systemd: name=nginx state=started enabled=yes
- debug: var=http_port
- name:
shell: |
echo 123
register: result
tags: reg
- debug: msg={{result}}
tags: reg
when: result.stdout == '123'
- debug: msg={{group_names}}
[r[email protected] playbook]# ansible-playbook nginx.yaml --tags=reg
循環:
批量建立檔案使用循環操作:{{item}}值是預設的。如果使用的是循環的話
- name: touch file
file: path=/tmp/{{item}} state=touch
with_items:
- "ldy520.txt"
- "ldy521.txt"
- "ldy1314.txt"
tags: touchfile
建立檔案名稱為 "ldy520.txt" ldy521.txt" "ldy1314.txt"會在兩台主機上進行建立
我們執行touchfile建立檔案的任務
[[email protected] playbook]# ansible-playbook nginx.yaml --tags=touchfile
執行後的結果檢視:
常用的循環語句:
語句 | 描述 |
---|---|
with_items | 标準循環 |
with_fileglob | 周遊目錄檔案 |
with_dict | 周遊字典 |
實作個批量拷貝功能:
從管理節點/tmp目錄下把.html結尾的檔案拷貝到/mnt目錄下
[[email protected] playbook]# vim nginx.yaml
- name: copy file
copy: src={{item}} dest=/mnt
with_fileglob:
- "/tmp/*.html"
tags: touchfile
執行任務:
我們在管理節點先建立這幾個檔案
[[email protected] playbook]# touch /tmp/{a1,b1,c1}.html
我們執行任務:
檢視執行後的結果是否正确
[[email protected] playbook]# ansible webservers -m shell -a " ls -l /mnt/*.html"
寫一個nginx虛拟主機的配置檔案,把它作為模闆給其他主機:
首先我們需要在建立一個目錄
[[email protected] playbook]# mkdir config
[[email protected] playbook]# cd config/
[[email protected] config]# vim nginx.conf
{% set domain_name = domain %} #定義一個變量而這個domain_name是本地使用的變量,domain變量是在yaml檔案中定義的變量。是以相當于是yaml檔案的變量值傳到了doamin_name中
server {
listen 80;
server_name {{ domain_name}}; #這裡必須加{{}}不然不會識别成變量而是識别成字元串
location / {
root /usr/share/html;
}
}
~
在nginx.yaml檔案中
[[email protected] playbook]# vim nginx.yaml
vars:
http_port: 80
domain: "www.ldy.com"
- name: create virtualhost
template: src=config/nginx.conf dest=/etc/nginx/conf.d #相當于模闆從src目錄下到dest
tags: nginx
執行任務後:
[roo[email protected] playbook]# ansible-playbook nginx.yaml --tags=nginx
我們檢視結果
[[email protected] playbook]# ansible webservers -m shell -a " cat /etc/nginx/conf.d/nginx.conf"
動态擷取主機清單中的ip位址:
{% set domain_name = domain %}
upstream web {
{% for host in groups['webservers'] %}
server {{ hostvars[host].inventory_hostname }};
{% endfor %}
}
server {
listen 80;
server_name {{ domain_name}};
location / {
root /usr/share/html;
}
}
執行任務:
[ro[email protected] playbook]# ansible-playbook nginx.yaml --tags=nginx
檢視是否動态生成了我們主機清單中的ip位址: