Ansible 優缺點
自動化引擎,可以實現配置管理、應用部署、服務編排 及其他各種IT需求
依賴 Jinja2、paramiko、PyYAML python庫
安裝部署簡單:只需在主控端部署Ansible環境
基於SSH進行配置管理,充分利用現成的機制
Ansible不需要守護進程,維護簡單、系統更加安全可靠
Ansible簡單易用
Ansible功能強大:通過模塊實現各種功能
Ansible設計優秀,便於分享。
Ansible對雲計算和大數據平臺都有很好的支持。包含了大量與雲服務、AWS、OpenStack、Docker等相關的模塊
ansible 的基本使用
ansible使用python語言開發,可以直接使用pip安裝
pip install ansible
也可以使用yum和apt-get包管理工具進行安裝
ansible
ansible-doc
ansible-playbook
ansible-vault
ansible-console
ansible-galaxy
ansible-pull
在 Ansible 中,用戶通過 Ansible 編排引擎操作主機。其中,主機可以通過配置文件配置、調用雲計算接口獲取、或者訪問CMDB中的數據庫獲取
Ansible 的編排引擎由 Inventory、API、Modules(模塊)、Plugins 組成。
Ansible 的典型用法:工程師將需要在遠程主機上執行的操作寫在 Ansible Playbook 中,然後使用 Ansible 執行 Playbook 中的操作。
vim /etc/ansible/hosts
[web]
192.168.1.1 ansible_user=wt ansible_port = 20002
web2-3
vim /etc/ansible/ansible.cfg
[defaults]
remote_user = wt
remote_port = 20002
ansible web -m command -a “hostname”
-m 指定模塊名稱,默認模塊是 command
-a 指定模塊的參數
-i 指定inventory文件
ansible 的模塊包含多個參數,參數使用 “key=value” 的形式表示,各個參數之間用空格分隔。
ansible web -m ping 驗證web組中的主機用戶SSH是否已經正確配置
ansible web -m copy -a “src=/tmp/index.html dest=/html/index.html” 將本地文件拷貝到服務器中
ansible web -m file -a “dest=/html/index.html mode=500 owner=nginx group=nginx” -become 修改服務器中文件到權限,-become等於sudo
ansible web -m yum -a “name=git state=present” -become 爲遠程服務器安裝git軟件
Inventory 管理
在 Ansible 中,將可管理的服務器集合稱爲 Inventory。因此,Inventory 管理便是服務器管理。
hosts文件位置
1.默認讀取 /etc/ansible/hosts 文件;
2.通過命令行參數 -i 指定hosts文件;
3.通過ansible.cfg 文件中的inventory選項指定hosts文件
[web]
web[1:3].tongwen.com
[db]
[common:children]
web
db
通過common這個組名可以匹配到 web 和 db 兩個組的主機列表
Ansible 通過在組名稱後面加上 “:children” 的方式聲明這個組下面包含的是其他組的名稱,而不是普通的服務器地址。
除了通過組名稱來引用hosts文件中定義的主機以外,Ansible還支持通配符、正則表達式等更加靈活的方式來匹配服務器。
ansible <pattern_goes_here> -m <module_name> -a <arguments>
Ansible 匹配服務器時,可以是組名匹配,也可以是模式匹配
ansible web -m service -a “name=httpd state=restarted”
ansible web*.tongwen.com -m service -a “name=httpd state=restarted”
匹配規則: 可以結合 —list-hosts 選項驗證Ansible的主機匹配
規則 | 含義 |
web | 匹配目標組爲 web ,多個組以 “:” 分隔 |
192.168.1.1或web1.tongwen.com | 匹配目標IP地址或服務器名,如果有多個IP或服務器,以 : 分隔 |
all 或 * | 匹配所有服務器 |
web:!db | 匹配web組中且不在db組中的服務器 |
*.tongwen.com 或 192.168.* | 使用通配符匹配 |
web[0],web[1:],web[-1] | 使用索引或切片的方式匹配組中的服務器 |
~(web|db).*.tongwen.com | 以~開頭的匹配,表示使用正則表達式匹配 |
動態Inventory獲取
動態獲取服務器列表,可以減少服務器列表的維護操作
可以通過OpenStack的API或者CMDB數據庫中的紀錄獲取Inventory列表
Inventory 行爲參數
名稱 | 默認值 | 描述 |
ansible_host | 主機名稱 | SSH的目的主機名或IP |
ansible_user | 當前用戶 | SSH連接的用戶名 |
ansible_port | 22 | SSH連接的端口號 |
ansible_ssh_private_key_file | none | SSH連接使用的私鑰 |
ansible_connection | smart | Ansible使用的連接模式,smart、ssh 或 paramiko |
ansible_become | none | 類似Linux下的sudo |
ansible_become_user | none | 切換到哪個用戶執行命令 |
ansible_shell_type | sh | 執行命令所使用的shell |
ansible_python_interpreter | /usr/bin/python | 使用哪個Python解釋器 |
ansible_*_interpreter | none | 指定其他語言的解釋器 |
定義服務器變量:
hosts文件中,除了行爲參數,還可以定義普通的變量。
參數名必須爲字母、數字和下劃線的組合,並且 首字符必須爲字母。
127.0.0.1 mysql_port=3306
[db]
192.168.1.1 mysql_port=3307
ansible all -a “echo {{mysql_port}}”
127.0.0.1 mysql_port=3306
[db]
192.168.1.1
192.168.1.2
[db:vars]
mysql_port=3307
需要定義多個變量時:
爲每個服務器和羣組創建獨立的變量文件
將組的變量文件存放在名爲 group_vars 目錄下,目錄下的文件名與組的名稱相同,文件的擴展名.yml/.yaml或沒有擴展名
服務器的變量文件存放在名爲 host_vars 目錄下,文件名與服務器名稱相同。
Ansible 將依次在Playbook所在的目錄、hosts文件所在的目錄和 /etc/ansible/ 目錄下尋找 group_vars / host_vars 目錄。
vim /etc/ansible/group_vars/db.yml
mysql_port: 3307
此文件遵循YAML語法格式。
YAML 語法
YAML的語法規則如下:
YAML的第一行爲”---",表示這是一個YAML文件;
YAML中的字段大小寫敏感;
YAML與Python一樣,用縮進表示層級關係;
YAML的縮進不允許用Tab鍵,只運行使用空格,且空格的數目不重要,只要相同層級的元素左側對齊即可;
# 表示註釋,從這個字符一直到行尾都會被解析器忽略;
YAML 支持三種格式的數據,分別是:
對象: 鍵值對的集合,又稱爲映射,類似於Python中的字典
數組: 一組按次序排列的值,又稱爲序列(sequence),類似Python中的列表
純量: scalars,單個的、不可再分的值,如字符串、布爾值與數字
Ansible 的常用模塊
工作原理:
將模塊拷貝到遠程服務器
執行模塊定義的操作,完成對服務器的修改
在遠程服務器中刪除模塊
ansible-doc -l
ansible-doc -l file
ansible-doc file
常用的模塊:
1.ping 測試現有的SSH參數是否能夠順利連通遠程服務器。
ansible group -m ping
2.遠程命令模塊
command、raw、script、shell 模塊都可以實現在遠程主機上執行Linux命令。command 是默認模塊。
raw模塊相當於使用SSH直接執行Linux命令,不會進入到Ansible的模塊子系統中
shell模塊還可以執行遠程服務器上的shell腳本文件
ansible group -m shell -a “/root/sc/a.sh”
script模塊可以在遠程服務器上執行主控節點中的腳本文件,其功能相當於scp+shell的組合,執行完後會刪除遠程服務器上的腳本文件
ansible group -m script -a ’test.sh’
3.file 主要用於對遠程服務器上的文件進行操作(包括鏈接和目錄),修改文件的權限、屬主、創建刪除文件
path 執行文件、目錄的路徑
recurse 遞歸設置文件屬性,只對目錄有效
group 定義文件、目錄的組
mode 定義文件/目錄的權限
owner 定義文件/目錄的所有者
src 要被鏈接的源文件路徑,只應用與state爲link 的情況
dest 被鏈接到的路徑,只應用於state爲link的情況
force 在兩種情況下會強制創建軟鏈接,一種是源文件不存在但之後會建立的情況;一種是目標軟鏈接已經存在,需要先取消之前的軟鏈接,然後創建新的軟鏈接,默認值 no。
ansible group -m file -a “path=/data/release state=directory mode=0755” 創建目錄
ansible group -m file -a “path=/data/release state=touch mode=‘u=rw,g=r,o=r'" 修改權限
ansible group -m file -a “src=/etc/passwd dest=/tmp/passwd state=link owner=wt group=wt" 創建一個軟鏈接
ansible group -m file -a ’path=/tmp/passwd owner=root group=root mode=0644’ -become 修改文件的所有者
4.copy 用於將主控節點上的文件或目錄拷貝到遠程服務器上,類似scp
src 指定要複製到遠程主機的文件或目錄。如果路徑以 / 結尾,則只複製目錄裏的內容,如果沒有以 / 結尾,則複製包含目錄在內的整個內容
dest 文件複製的目的地,必須是一個絕對路徑,如果源文件是一個目錄,那麼dest指向的也必須是一個目錄
force 默認取值爲yes,表示目標主機包含此文件,但內容不同時,覆蓋。
backup 默認取值爲no,如果爲yes,在覆蓋前將原文件進行備份
directory_mode 遞歸設定目錄權限,默認爲系統默認權限
others 所有file模塊裏的選項都可以在這裏使用
ansible web -m copy -a “src=index.html dest=/data/release/html/index.html" 拷貝文件到服務器
ansible web -m copy -a “src=index.html dest=/data/release/html/index.html backup=yes force=yes” 拷貝文件到服務器,如果已存在,備份後覆蓋
ansible web -m copy -a ’src=index.html dest=/data/release/html/index.html owner=root group=root mode=644 force=yes’ -become
5.user/group useradd/userdel/usermod groupadd/groupdel/groupmod
name 需要操作的用戶名或組名
comment 用戶的詳細描述
createhome 創建用戶時,是否創建用戶的家目錄,默認爲yes
home 指定用戶的家目錄,需要和createhome 配合使用
groups 指定用戶的屬組
uid 設置用戶的uid
gid 設置羣組的gid
password 設置用戶的密碼
state 創建用戶or刪除用戶 present 和 absent
expires 用戶的過期時間
shell 指定用戶的shell環境
ansible web -m user -a ’name=wt comment=“wt user” uid=1001 group=root’ -become 創建一個用戶
ansible web -m user -a ’name=wt state=absent’ -become 刪除一個用戶
ansible web -m user -a ’name=wt comment=“wt user” generate_ssh_key=yes ssh_key_bits=2048’ -become 創建一個用戶並生成一對密鑰
ansible web -m group -a ’name=wt state=present gid=1001’ -become
ansible web -m group -a ’name=wt state=absent’ -become
6.apt debian ubuntu系統中安裝軟件/刪除軟件
name 包名
state 軟件包的狀態,latest、absent、present、build-sep, 默認爲present
autoremove 默認取值爲no,如果取值爲yes,將會移除不需要的軟件包
force 強制安裝或刪除軟件包
update_cache 作用與 apt-get update相同
deb deb文件的路徑
ansible web -m apt -a ’name=git state=present’ -become
ansible web -m apt -a ’name=git state=absent’ -become
ansible web -m apt -a ’update_cache=yes’ -become
7.get_url 從互聯網下載數據到本地,作用類似於Linux下的curl。
url 必傳選項,文件的下載地址
dest 必傳選項,文件保存的絕對路徑
mode 文件的權限
others 所有file模塊裏的選項都可以在這裏使用
checksum 文件的校驗碼
headers 傳遞給下載服務器的HTTP Headers
backup 如果本地已經存在同名文件,備份文件
timeout 下載的超時時間
ansible web -m get_url -a ‘url=http://xxx.com.xxx dest=/opt/xxx mode=0777 checksum=324988594fsj239432u'
8.unarchive
remote_src 默認爲no,用來表示解壓的文件在遠程服務器中還是存在控制節點上。yes 先將控制節點上的文件複製到遠程主機中,然後再進行解壓。
src 指定壓縮文件到路徑,該選項到取值取決與remote_src的取值,如果remote_src取值爲yes,則src指定的路徑是遠程服務器中壓縮包的地址,如果remote_src取值爲no,則src指向的是控制節點中的路徑
dest 該選項指定的是遠程服務器上的絕對路徑,表示壓縮文件解壓的路徑;
list_files 默認情況下該選項取值爲no,如果該選項取值爲yes,也會解壓文件,並且在ansible的返回值中列出壓縮包裏的文件
exclude 解壓文件時排除exclude選項指定的文件或目錄列表
keep_newer 默認取值爲False,如果該選項取值爲True,那麼當目標地址中存在同名的文件,並且文件必壓縮包中的文件更新時,不進行覆蓋
owner
group
mode
ansible web -m unarchive -a ’src=data.tar.gz dest=/tmp/data list_files=yes’
ansible web -m unarchive -a ’src=data.tar.bz2 dest=/tmp/data.tar.bz2’
ansible web -m unarchive -a ’src=/tmp/data.tar.bz2 dest=/tmp remote_src=yes’
9.git 在遠程服務器執行 git 相關操作
repo 遠程git 庫的地址,可以是git協議、ssh、http協議的git庫地址
dest 必選選項,git庫 clone 到本地服務器以後保存到絕對路徑
version 克隆遠程git庫的版本,取值可爲HEAD、分支多名稱、tag名稱,也可以是一個commit 的hash值
force 默認取值爲no,當該選項取值爲yes時,如果本地的git庫有修改,將會拋棄本地的修改
accept_hostkey 當該選項取值爲yes時,如果git庫的服務器不在know_hosts中,則添加到know_hosts中,key_file指定科隆遠程git庫地址時使用的私鑰
ansible web -m git -a ‘repo=https://github.com/kennethreitz/requests.git dest=/tmp/requests version=HEAD’ 將requests克隆到/tmp/requests目錄下
ansible web -a ‘python setup.py install chdir=/tmp/requests’ -become 從源碼安裝requests
ansible web -a ‘python -c “import requests”'
10.stat 獲取遠程服務器上的文件信息,類似Linux 下的stat命令,stat命令可以獲取time、ctime、mtime、checksum 、 size 、 uid、gid等
path 指定文件或目錄的路徑
ansible web -m stat -a “path=/etc/passwd”
11.cron cron是管理Linux下計劃任務的模塊
backup 默認 no,當爲yes時,表示修改之前先做備份
state 取值爲present或absent,用來確認該任務是創建還是刪除
name 任務的描述/名字
job 添加或刪除任務,主要取決於state 的取值
user 操作哪一個用戶的crontab
cron_file 如果指定該選項,則用該文件替換遠程主機上的cron.d 目錄下的用戶任務計劃
month weekday day minute hour 取值與crontab類似
minute 取值範圍0~59,*表示每分鐘運行,*/5 表示每5分鐘運行
ansible web -m cron -a ‘backup=yes name=“nginx_log split” minute=*/2 hour=* job=“ls /tmp > /dev/null” '
12.service 類似Linux 下的service命令,用來啓動、停止、重啓服務
name 服務的名稱,該選項爲必選項
state 取值爲 started、stopped、restarted、reloaded。 started和stopped爲冪等的,也就是已經啓動的服務,執行started不會有任何操作
sleep 重啓的過程中,先停職服務然後sleep幾秒再啓動
pattern 定義一個模式,Ansible首先通過status命令查看服務的狀態,以此判斷服務是否在運行,如果通過status查看服務狀態時沒有響應,Ansible會嘗試匹配ps命令的輸出,當匹配到相應模式時,認爲服務已經啓動,否則認爲服務沒有啓動
enabled 取值爲yes 或 no,用來設置服務是否開機啓動
ansible web -m apt -a “name=apache2 state=present” -become
ansible web -m service -a ’name=apache2 state=stopped’
ansible web -m service -a ’name=apache2 state=restarted’
13.sysctl 與linux 下 sysctl命令相似,控制Linux 內核參數
name 需要設置的參數
value 需要設置的值
sysctl_file sysctl.conf 文件的絕對路徑,默認路徑爲/etc/sysctl.conf
reload 取值爲yes 或 no,默認yes,表示設置完成後,執行sysctl -p 操作
ansible web -m sysctl -a ’name=vm.overcommit_memory value=1’ -become
14.mount 在遠程服務器上掛載磁盤,在掛盤操作時,如果指定的掛載點不存在,則創建該路徑
name 掛載點的路徑
state present、absent、mounted、unmounted。其中mounted與unmounted用來處理磁盤的掛載和卸載,並且會正確配置stab文件,present和absent只會設置stab文件,不會去操作磁盤
fstype 指定文件系統類型,當state取值爲present或mounted時,該選項爲必填選項
src 掛載點設備
ansible web -m mount -a ’name=/mnt/data src=/dev/vda fstype=ext4 state=mounted’
15.synchronize 對rsync命令的封裝
src 需要同步到遠程服務器的文件或目錄
dest 遠程服務器保存數據的路徑
archive 默認取值爲yes,相當於同時開啓recursive、links、perms、times、owner、group、-D選項
compress 默認爲yes,表示在文件同步過程中啓用壓縮
delete 默認爲no,當取爲yes時,表示刪除dest中存在而src中不存在的文件
ansible web -m synchronize -a ’src=/data/test dest=/tmp’
模塊的返回值
返回值的名稱 | 返回值的含義 |
changed | 幾乎所有的Ansible模塊都會返回該變量,表示模塊是否對遠程主機執行了修改操作 |
failed | 模塊未能執行完成,返回failed 爲 true |
msg | 模塊執行失敗的原因,常見的錯誤如ssh連接失敗,沒有權限執行模塊等 |
rc | 與命令行工具相關的模塊會返回rc,表示執行Linux命令的返回碼 |
stdout | 與rc類似,返回的是標準輸出的結果 |
stderr | 與rc類似,返回的是錯誤輸出的結果 |
backup_file | 所有存在backup選項的模塊,用來返回備份文件的路徑 |
results | 應用在Playbook中存在循環的情況,返回多個結果 |
Role
Playbook
vim test_playbook.yml
---
- host: web
become: yes
become_method: sudo
tasks:
- name: copy file
copy: src=/tmp/index.html dest=/html/index.html
- name: change mode
file: dest=/html/index.html mode=500 owner=nginx group=nginx
- name: ensure packages installed
yum: pkg={{ item }} state=present
with_items:
- telnet
- git
Playbook 中首先包含了一些聲明信息,如hosts關鍵字聲明該Playbook應用的服務器列表,-表示定義列表
become和become_method表示在遠程服務器通過sudo執行操作
Playbook最後包含了若干task,每一個task對應於前面的一條ad-hoc命令。具體執行時,多個task按順序執行
ansible-playbook test_palybook.yml
Ansible中,每個play必須包含以下兩項:
hosts 需要對哪些遠程服務器執行操作
tasks 需要在這些服務器上執行的任務列表
task的定義形勢:
module: options
action: module options (老版,已棄用)
- name: install apache
apt: name=apache2 update_cache=yes state=present
YAML 中使用 > 進行摺疊,參數較多時,Playbook 可以這麼寫:
- name: install apache
apt: >
name=apache2
update_cache=yes
state=present
也可以使用縮進子塊的形式:
- name: install apache2
apt:
name: apache2
update_cache: yes
state: present
前者代表一個比較長的字符串,後者是一個字典。在task中,name是可選的。
在Playbook 中導入其他 Playbook
vim all.yml
---
- include: db.yml
- include: web.yml
Playbook > Play > task
ansible-playbook all.yml
ansible-playbook & ansible 命令相同的子命令
-T —timeout: 建立SSH連接的超時時間
—key-file —private-key: 建立SSH連接時的私鑰
-i —inventory-file: 指定Inventory 文件,默認爲 /etc/ansible/hosts
-f —forks: 併發執行的進程數,默認5
—list-hosts: playbook 匹配的服務器列表
ansible-playbook 特有的命令選項
—list-tasks: 列出任務列表
—step: 每執行一個任務後停止,等待用戶確認, N,y,c。 no yes continue
—syntax-check: 檢查Playbook 的語法
-C —check: 檢查當前Playbook是否會修改遠程服務器,相當於測試Playbook的執行結果
權限:默認使用當前用戶進行連接遠程服務器執行操作
使用的用戶
---
- hosts: web
remote_user: root
tasks:
- name: test connection
ping:
remote_user: wt
使用sudo
---
- hosts: web
remote_user: wt
tasks:
- service: name=nginx state=started
become: yes
become_method: sudo
通知:notify、handler
handler 是 Ansible 提供的條件機制,與tasks比較類似,都是去執行某些操作。但handler只有在被notify觸發後纔會執行
在Playbook 中,如果 tasks 後面存在 notify 選項,那麼, 當 Ansible 識別到task 改變了系統的狀態,就會通過notify觸發handler
task 使用 handler 的名字作爲參數,以此來觸發特定的 handler。
---
- hosts: web
tasks:
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: write the apache config file
template: src=/data/httpd.j2 dest=/etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
handler 只會在所有task執行完後執行。一個handler被觸發多次,它也只會執行一次。
變量:
將變量定義在playbook的vars選項中
- hosts: web
vars:
mysql_port: 3307
將變量保存在獨立的文件,並通過 vars_files 選項引用
- hosts: web
vars:
favcolor: blue
vars_files:
- /vars/ex_vars.yml
tasks:
- name: this is just a placeholder
command: /bin/echo foo
保存變量的文件是一個簡單的YAML格式的字典:
somevar: some value
password: wt
將任務的執行結果存儲爲變量, 註冊變量,使用 register 獲取
---
- hosts: web
tasks:
- shell: /usr/bin/foo
register: foo_result
ignore_errors: True
- shell: /usr/bin/bar
when: foo_result.rc == 5
Facts 變量:Ansible 執行遠程部署之前從遠程服務器中獲取的系統信息,包括服務器名稱、IP、操作系統、分區信息、硬件信息等
ansible web -m setup 通過setup 模塊查看 Facts 變量的列表
可以在 playbook 中直接通過變量名字進行引用,也可以在jinja2模版中通過變量的名字引用變量。
---
- hosts: web
tasks:
- shell: echo {{ansible_os_family}}
register: myecho
- debug: var=myecho.stdout_lines
- name: install git on Debian Linux
apt: name=git state=installed
when: ansible_os_family == ‘Debian’
setup 模塊爲了輸出結果的可讀性,對模塊的輸出進行了歸類和整理。因此,當我們訪問複雜的子屬性時,需要使用嵌套結構
後去ipv4地址
ansible_eth0[“ipv4”][“address”]
ansible_eth0.ipv4.address
---
- hosts: web
gather_facts: yes
tasks:
- shell: echo {{ansible_eth0[“ipv4”][“address"]}}
register: myecho
- debug: var=myecho.stdout_lines
- shell: echo {{ansible_eth0.ipv4.address}}
register: myecho
- debug: var=myecho.stdout_lines
gather_facts 選項控制是否收集遠程服務器的信息,默認取值爲yes,不需要可以設爲no,提高ansible的部署效率
循環:
- name: install mysql
yum: name={{ item }} state=installed
with_items:
- mysql-server
- MySQL-python
- libselinux-python
條件:在 Playbook 中可以通過when選項執行條件語句
tasks:
- name: “shut down Debian flavored systems”
command: /sbin/shutdown -t now
when: ansible_os_family == “Debian”
when 選項支持多個條件語句,下面時一個YAML格式的多條件:
tasks:
- name: “shut down Ceontos6 systems”
command: /sbin/shutdonw -t now
when:
- ansible_distribution == “Centos”
- ansible_distribution_major_version == “6”
and / or
tasks:
- name: “shut down Centos 6 and Debian 7 systems”
command: /sbin/shutdown -t now
when: (ansible_distribution == “Centos” and ansible_distribution_major_version == “6”) or (ansible_distribution == “Debian” and ansible_distribution_major_version == “7”)
when 選項中可以使用Jinja2的過濾器
tasks:
- command: /bin/false
register: result
ignore_errors: True
- command: /bin/someting
when: result|failed
- command: /bin/someting_else
when: result|succeeded
when讀取變量的值
vars:
epic: True
tasks:
- shell: echo “This certainly is epic”
when: epic
when選項和循環一起使用
tasks:
- command: echo {{ item }}
with_item: [0,2,4,6,8,10]
when: item > 5
任務執行策略:
Ansible中,Playbook 的執行是以task爲單位進行的。Ansible 默認使用5個進程對遠程服務器執行任務。在默認情況的任務執行策略(linear)中,Ansible首先執行task1,並且等所有服務器都執行完了task1再開始執行task2,以此類推。 Ansible2.0開始,支持名爲free的任務執行策略,允許執行快的服務器提前完成Play的部署,不用等待其他服務器
---
- hosts: web
strategy: free
tasks:
Playbook 部署 Nginx
---
- hosts: web
become: yes
become_method: sudo
vars:
worker_processes: 4
worker_connections: 768
max_open_files: 65506
tasks:
- name: install nginx
apt: name=nginx update_cache=yes state=present
- name: copy nginx config file
template: src=/home/wt/ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
- name: copy index.html
template:
src: /home/wt/ansible/index.html.j2
dest: /usr/share/nginx/www/index.html
mode: 0644
notify: restart nginx
handlers:
- name: restart nginx
service: name=nginx state=restarted