運維工具介紹
Ansible
Ansible優點:
Agentless (去中心化)
Stupied Simple (上手簡單,學習曲線平滑)
SSH by default (安全,無需安裝客戶端)
YAML no code,定制劇本 (語法簡單,維護方便)
自動工具衍生過程:
早期(很復雜,使用復雜,安裝復雜,配置復雜):chef cfengine(C語言) func
中期(向輕量型發展):puppet (ruby)
近代(輕量工具): saltstack ansible (Python)
Ansible使用場景:
ls cd pwd top touch cp mv rm mkdir ln useradd userdel use* group* passwd* cat head tail tac
more less set ifconfig scp rsync
(1)文件傳輸:
mv cp scp rsync
(2)命令執行:
剩下的其它命令
安裝http服務:
安裝
配置
啟動服務(本機的服務啟動) tasks
讓服務對用戶提供服務(路由器配置,iptables)
Ansible企業實際應用場景分析:
(1)Dev開發環境
使用者:程序員
功能:程序員開發軟件測試BUG的環境
管理者:千萬不能是運維,一定是要程序員自已
(2)測試環境
使用者:qa測試工程師
功能:測試經過Dev環境測試通過的軟件的功能
管理者:運維
備注:測試環境往往有多套(測試環境滿足測試功能即可,不宜過多)
(1)測試同學希望測試環境有多套
公司的產品多產品線并發,即多個版本,意味著多個版本同步測試
(2)通常情況下,測試環境有多少套和產品線數量保持一樣
(3)發布環境(代碼發布機,有些公司堡壘機(安全屏障))
使用者:運維
功能:發布代碼至生產環境
管理者:運維(有經驗的)
發布機:往往需要有2臺(主備)
(4)生產環境
使用者:運維,只能是運維(極少數情況下會開放權限給核心開發人員,
極少數公司將該環境權限完全開放給開發人員并讓開發人員維護)
功能:對用戶提供公司產品的服務
管理者:只能是運維
生產環境服務器數量:一般比較多,且應用非常重要。
往往需要自動工具協助部署配置應用。機器數量具體和公司業務規模相關。
(5)灰度環境(生產環境的一部分)
使用者:運維
功能:在全量發布代碼前將代碼的功能面向少量精準用戶發布的環境
案例:
大家手頭100臺服務器,先發布其中的10臺服務器,這10臺服務器就是灰度服務器
管理者:只能是運維
為什么灰度環境:往往該版本功能變更較大,為保險起見特意先讓一部分用戶優化
體驗該功能,待這部分用戶使用沒有重大問題的時候,再全量發布至所有服務器。
基于key認證:
ssh-keygen 然后一直回車 生成成功
ssh-cpio-id root@172.18.12.11(目標主機) 回車輸入密碼
然后使用ssh 172.18.12.11 驗證,不需要輸入密碼即成功
Ansible安裝
yum install ansible -y
ansible –verison ##查看安裝的版本號
Ansible 1.9和2.0的區別:
(1) 最大區別是2.0的API調用方式發生變化,1.9原有的api調用方式在2.0不再支持
(2) 2.0增加了重要的功能: ansible-console
(3) Ansible 2.0的模塊數量增加很多,500+個模塊數量
Ansible命令集
/usr/bin/ansible # Ansibe AD-Hoc 臨時命令執行工具,常用于臨時命令的執行
/usr/bin/ansible-doc # Ansible 模塊功能查看工具
/usr/bin/ansible-galaxy # 下載/上傳優秀代碼或Roles模塊 的官網平臺,基于網絡的
/usr/bin/ansible-playbook # Ansible 定制自動化的任務集編排工具
/usr/bin/ansible-pull # Ansible遠程執行命令的工具(使用較少,海量機器時使用,對運維的架構能力要求較高)
/usr/bin/ansible-vault # Ansible 文件加密工具
/usr/bin/ansible-console # Ansible基于Linux Consoble界面可與用戶交互的命令執行工具
Ansible高級自定義插件目錄(需要python基礎)
/usr/share/ansible_plugins
action ((可能使用較多))
callback (可能使用較多)
connection
filter
lookup
vars
調用ping模塊檢測172.16.0.68是否存活
ansible 172.16.0.68 -m ping
返回信息如下:
172.16.0.68 | success >> {
“changed”: false,
“ping”: “pong”
}
也可以這樣使用: ansible “172.16.0.6” -m ping
– Ansible模塊查看
ansible-doc -l
– Ansible command創建文件
ansible 172.16.0.6 -m command -a “touch /tmp/stanley.md”
有特殊符號的需使用shell 模塊 : -m shell
Ansible命令詳解:
Usage: ansible <host-pattern> [options]
Options:
-a MODULE_ARGS, –args=MODULE_ARGS
module arguments # 指定Ansible調用的模塊
–ask-become-pass ask for privilege escalation password # Ansible su切換用戶的時候使用該參數輸入密碼
-k, –ask-pass ask for SSH password # 以密碼認證
–ask-su-pass ask for su password (deprecated, use become) # su的時候使用該 參數
-K, –ask-sudo-pass ask for sudo password (deprecated, use become) # sudo的時候
–ask-vault-pass ask for vault password # ansible-valut 加密文件
-B SECONDS, –background=SECONDS # 后臺等待X秒
run asynchronously, failing after X seconds
(default=N/A)
-b, –become run operations with become (nopasswd implied)
–become-method=BECOME_METHOD
privilege escalation method to use (default=sudo),
valid choices: [ sudo | su | pbrun | pfexec | runas ]
–become-user=BECOME_USER
run operations as this user (default=None) # su到哪個用戶
-C, –check don’t make any changes; instead, try to predict some
of the changes that may occur # 不執行命令只做命令執行檢查
-c CONNECTION, –connection=CONNECTION
connection type to use (default=smart)
-e EXTRA_VARS, –extra-vars=EXTRA_VARS # 調用外部變量
set additional variables as key=value or YAML/JSON
-f FORKS, –forks=FORKS # Ansible一次命令執行并發的線程數
specify number of parallel processes to use
(default=5)
-h, –help show this help message and exit
-i INVENTORY, –inventory-file=INVENTORY # Ansible調用的Hosts文件,默認/etc/ansible/hosts
specify inventory host file
(default=/etc/ansible/hosts)
-l SUBSET, –limit=SUBSET # 限定主機列表中的某臺機器執行
further limit selected hosts to an additional pattern
–list-hosts outputs a list of matching hosts; does not execute
anything else ## 列出主機列表中的主機
-m MODULE_NAME, –module-name=MODULE_NAME # Ansible 調用的執行模塊
module name to execute (default=command)
-M MODULE_PATH, –module-path=MODULE_PATH
specify path(s) to module library (default=None)
-o, –one-line condense output
-P POLL_INTERVAL, –poll=POLL_INTERVAL
set the poll interval if using -B (default=15)
–private-key=PRIVATE_KEY_FILE
use this file to authenticate the connection
-S, –su run operations with su (deprecated, use become) #使用su和become結合使用
-R SU_USER, –su-user=SU_USER # su的話切換至哪個用戶
run operations with su as this user (default=root)
(deprecated, use become)
-s, –sudo run operations with sudo (nopasswd) (deprecated, use
become) # 執行sudo命令
-U SUDO_USER, –sudo-user=SUDO_USER # sudo
desired sudo user (default=root) (deprecated, use
become)
-T TIMEOUT, –timeout=TIMEOUT # 執行命令的超時時間
override the SSH timeout in seconds (default=10)
-t TREE, –tree=TREE log output to this directory
-u REMOTE_USER, –user=REMOTE_USER
connect as this user (default=root)
–vault-password-file=VAULT_PASSWORD_FILE ## ansible-vault加密的密碼文件
vault password file
-v, –verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging) # 顯示詳細信息 -vvvv
–version show program’s version number and exit ## 顯示版本號
Ansiblie命令執行過程 (-vvvv)
- 加載自己的配置文件 默認/etc/ansible/ansible.cfg
- 加載自己對應的模塊文件,如command
- 通過ansible將模塊或命令生成對應的臨時py文件,并將該文件傳輸至遠程服務器的
對應執行用戶的家目錄的.ansible/tmp/XXX/XXX.PY文件 - 給文件+x執行
- 執行并返回結果
- 刪除臨時py文件,sleep 0退出
Ansible核心組件:
ANSIBLE PLAYBOOKS:任務劇本(任務集),編排定義Ansible任務集的配置文件,由Ansible順序依次執行,通常是JSON格式的YML文件;
INVENTORY:Ansible管理主機的清單;/etc/ansible/hosts
MODULES:Ansible執行命令的功能模塊,多數為內置的核心模塊,也可自定義; (ansible-doc -l獲取Ansible的所有模塊列表)
PLUGINS:模塊功能的補充,如連接類型插件、循環插件、變量插件、過濾插件等,該功能不常用。
API:供第三方程序調用的應用程序編程接口;
ANSIBLE:該部分圖中表示的不明顯,組合INVENTORY、API、MODULES、PLUGINS的綠框大家可以理解為是ansible命令工具,其為核心執行工具;Ansible命令執行來源:
(1) USER,普通用戶,即SA (SYSTEM ADMINISTRATOR)。
(2) CMDB API 調用
(3) PUBLIC/PRIVATE CLOUD API調用
(4)USER-> Ansible Playbook -> Ansibile
Ansible-playbook(劇本):
a> 將已有編排好的任務集寫入Ansible-Playbook
b> 通過ansible-playbook命令分拆任務集至逐條ansible命令,按預定規則逐條執行。Ansible 和 Ansible-playbook的使用場景:
Ansible 主要用于臨時命令使用場景
Ansible-playbook 主要用于長期規劃好的,大型項目的場景,需要有前提的規劃。Ansible主要操作對象:
HOSTS(主要學習)
NETWORKINGAnsible組件調用關系:(大家自己總結)
Ansible 實戰演練
ansible 172.16.0.6* -m command -a ‘ls /tmp’
– Ansible group 定義:
[groupname] 定義Inventory 組,后面可跟ip ,也可跟hostname
[mageduweb]
172.16.0.68
172.16.0.69
Ansible 配置文件(絕大多數配置保持默認就好)
(1)[defaults]
inventory = /etc/ansible/hosts # 主機列表配置文件
library = /usr/share/my_modules/ # 庫文件存放目錄
remote_tmp = $HOME/.ansible/tmp # 生成的臨時py命令文件存放在遠程主機的目錄
local_tmp = $HOME/.ansible/tmp # 本機的臨時命令執行目錄
forks = 5 # 默認并發數
poll_interval = 15 # 默認的線程池
sudo_user = root # 默認sudo 用戶
ask_sudo_pass = True
ask_pass = True
transport = smart
remote_port = 22
module_lang = C
module_set_locale = False
host_key_checking = False ### 檢查對應服務器的host_key
(2)[privilege_escalation]
(3)[paramiko_connection]
(4)[ssh_connection]
(5)[accelerate]
(6)[selinux]
(7)[colors]
Ansible模塊:
– command模塊:使用ansible自帶模塊執行命令 如果要用 > < | & ‘ ‘ 使用shell模塊 core
ansible mageduweb -m command -a ‘ls /tmp’
ansible mageduweb -m command -a ‘cat /tmp/stanley.md | grep echo’ ##執行失敗,即使不返回失敗結果也不會正常生效
– creates
a filename or (since 2.0) glob pattern, when it already exists, this
step will not be run.
[Default: None]
– shell 模塊: 調用bash執行命令 類似 cat /tmp/stanley.md | awk -F’|’ ‘{print $1,$2}’ &> /tmp/stanley.txt 這些復雜命令,即使使用shell也會失敗,解決辦法:寫到腳本時,copy到遠程,執行,再把需要的結果拉回執行命令的機器(執行Ansible命令的機器往往稱為:Master機或者中控機或者堡壘機)。
ansible mageduweb -m shell -a ‘echo magedu | passwd –-stdin stanley’
– copy: 復制本地文件至遠程服務器,并且能夠改屬性等
ansible mageduweb -m copy -a ‘src=/root/125.sh dest=/root/p.sh owner=stanley backup=no mode=600’
cron:定時執行任務的模塊
* * * *
分 時 日 月 周
// 增加一條crontab
ansible mageduweb -m cron -a ‘minute=”/5″ job=”/usr/sbin/ntpdate 172.16.0.1 &> /dev/null” name=”SynctimeStanley”‘
// 刪除名為SynctimeStanley的crontab任務
ansible mageduweb -m cron -a ‘state=absent name=”SynctimeStanley”‘
– fetch模塊: 從遠程服務器拉取文件至本機,
只能fetch文件,不能fetch目錄,如果拉目錄,先tar/zip 再拉到本機即可
ansible mageduweb -m fetch -a ‘src=/root/p.sh dest=/root/ppp.sh’
遠程主機目錄 本地主機目錄
– file模塊: 設置文件屬性
ansible mageduweb -m file -a ‘path=/root/p.sh mode=755’ # 設置文件屬性,
ansible mageduweb -m file -a ‘src=/root/p.sh dest=/tmp/pp.sh mode=644
state=touch’ # 復制文件并且設置文件屬性
create a directory if it doesn’t exist #建立目錄
– file: path=/etc/some_directory state=directory mode=0755
ansible 172.18.12.12 -m hostname -a ‘name={{ hostname }}{{ http_port }}’
#定義主機名,自己的變量優先于組變量
– pip 模塊: Python的包管理工具
ansible mageduweb -m pip -a ‘name=pip2’
– ping 模塊: 檢測主機存活
– yum模塊: 安裝軟件
ansible mageduweb -m yum -a ‘name=httpd state=latest’ //安裝httpd包
ansible mageduweb -m shell -a ‘ss -tnl | grep 80 ‘
ansible mageduweb -m command -a ‘ss -tnl ‘
ansible mageduweb -m command -a ‘systemctl start httpd.service’
ansible mageduweb -m command -a ‘systemctl stop httpd.service’
– service模塊:
ansible mageduweb -m service -a ‘name=httpd state=started’
ansible mageduweb -m service -a ‘name=httpd state=stopped’
– user模塊:
ansible mageduweb -m user -a ‘name=magedu home=/tmp/magedu/ shell=/bin/bash uid=2000 comment=”test user” group=root’
Ansible系列命令
ansible ansible-doc ansible-playbook ansible-vault
ansible-console ansible-galaxy ansible-pull
– ansible-galaxy
用法:
ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [–help] [options] …
去 https://galaxy.ansible.com/ 上傳或下載優秀的Playbook(roles)
* 列出所有已安裝的galaxy
ansible-galaxy list
* 安裝galaxy
ansible-galaxy install geerlingguy.redis
* 刪除galaxy
ansible-galaxy remove geerlingguy.redis
– ansible-playbook用法
Usage: ansible-playbook playbook.yml
– ansible-valut用法
Usage: ansible-vault [create|decrypt|edit|encrypt|rekey|view] [–help] [options] vaultfile.ym
ansible-vault encrypt hello.yml # 加密
ansible-vault decrypt hello.yml # 解密
ansible-vault view hello.yml # 查看
– ansible-console用法
root@mageduweb (2)[f:10] $ service name=httpd state=stopped
root@mageduweb (2)[f:10] $
執行用戶@當前操作的主機組 (當前組的主機數量)[f:并發數]$
設置并發數: forks n 例如: forks 10
切換組: cd groupname 例如: cd mageduweb
列出當前組主機列表: list
列出所有的內置模塊: ?
執行狀態:
綠色: 執行成功并且不需要做改變的操作
黃色:執行成功并且對目標主機做變更
紅色:執行失敗
ansible all -m ping -u bruce -b –become-user batman
Ansible-playbook 劇本
—
– hosts: mageduweb
remote_user: root
tasks:
– name: add group nginx
user: name=nginx state=present
– name: add user nginx
user: name=nginx state=present group=nginx
– name: Install Nginx
yum: name=nginx state=present
– name: Start Nginx
service: name=nginx state=started enabled=yes
* 安裝http腳本實現:
!/bin/bash
安裝Apache
yum install –quiet -y httpd httpd-devel # command: yum install –quiet -y httpd httpd-devel
復制配置文件
cp /path/to/config/httpd.conf /etc/httpd/conf/httpd.conf -f
cp/path/to/httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf -f
啟動Apache,并設置開機啟動
service httpd start
chkconfig httpd on
* 安裝httpd ansible-playbook實現
—
– hosts: all
tasks:
– name: “安裝Apache”
command: yum install –quiet -y httpd httpd-devel
– name: “復制配置文件”
command: cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
command: cp /tmp/httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf
– name: “啟動Apache,并設置開機啟動”
command: service httpd start
command: chkconfig httpd on
* Playbook 核心組件
Hosts #執行的遠程主機列表
Tasks #任務集
Varniables #內置變量或自定義變量在playbook中調用
Templates #模板,可替換模板文件中的變量及可實現一些簡單邏輯的文件。
Handlers #和nogity結合使用,為條件觸發操作,滿足條件方才執行否則不執行
* tags 標簽
指定某條任務執行
ansible-playbook –tags=user useradd.yml
—
– hosts: mageduweb
remote_user: root
tasks:
– name: add group nginx
tags: user
user: name=nginx state=present
– name: add user nginx
user: name=nginx state=present group=nginx
– name: Install Nginx
yum: name=nginx state=present
– name: Start Nginx
service: name=nginx state=started enabled=yes
* handlers 和 notify 結合使用觸發條件,讓playbook在滿足一定觸發條件時才去執行某條task
—
– hosts: mageduweb
remote_user: root
tasks:
– name: add group nginx
tags: user
user: name=nginx state=present
– name: add user nginx
user: name=nginx state=present group=nginx
– name: Install Nginx
yum: name=nginx state=present
– name: config
copy: src=/root/config.txt dest=/etc/nginx/config.txt
notify:
– Restart Nginx
– Check Nginx Process
handlers:
– name: Restart Nginx
service: name=nginx state=restarted enabled=yes
– name: Check Nginx Process
shell: ss -tnl | grep 80
playbook 變量使用
- 變量來源:
(1)ansible setup facts遠程主機的所有變量都可以用
(2)自定義變量
a. 在/etc/ansible/hosts 定義變量,在主機組中的主機單獨定義,優先級高于組中公共變量
b. 在/etc/ansible/hosts 定義變量,針對主機組中的所有主機集中定義變量
c. 通過命令行指定變量,優先級最高 - 變量調用方式:
{{ variable_name }} 通過{{ }} 調用變量,且變量和{}兩頭之間必須有空格
額外介紹的是:
在playbook中調用變量時,有時”{{ variable_name }}”需要要雙引號引起來方可生效,有時候必須不能用””引起來 - 變量定義:
key=value
例:
http_port=80
templates
功能:
根據模塊文件動態生成對應的配置文件
(1) templates文件必須存放于目錄名為templates下,且命名為 .j2 結尾
(2)yaml/yml playbook文件需和templates目錄平級,目錄結構如下:
./
├── temnginx.yml
└── templates
└── nginx.conf.j2
* template 流語句語法
案例1:
// templates/nginx.conf.j2
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost }}
}
{% endfor %}
// temnginx.yml
—
– hosts: mageduweb
remote_user: root
vars:
nginx_vhosts:
– web1
– web2
– web3
# nginx_vhosts:
# – listen: 8080
tasks:
– name: template config to remote hosts
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
* 生成的結果:
server {
listen web1
}
server {
listen web2
}
server {
listen web3
}
案例2:
shell腳本與Jinja語法對比:
— shell腳本中寫法
for i in {1..10}
do
con
done
if [ xx ];then
con
elif
con
else
con
fi
— Jinja寫法:
// templates/nginx.conf.j2
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost.listen }}
}
{% endfor %}
* 生成的結果
server {
listen 8080
}
* playbook調用文件
// temnginx.yml
—
– hosts: mageduweb
remote_user: root
vars:
nginx_vhosts:
– listen: 8080
tasks:
– name: template config to remote hosts
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
* 案例3
// temnginx.yml
—
– hosts: mageduweb
remote_user: root
vars:
nginx_vhosts:
– web1:
listen: 8080
server_name: “web1.magedu.com”
root: “/var/www/nginx/web1/”
– web2:
listen: 8080
server_name: “web2.magedu.com”
root: “/var/www/nginx/web2/”
– web3:
listen: 8080
server_name: “web3.magedu.com”
root: “/var/www/nginx/web3/”
## 案例1
# nginx_vhosts:
# – web1
# – web2
# – web3
## 案例2
tasks:
– name: template config to remote hosts
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
// templates/nginx.conf.j2
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost.listen }}
server_name {{ vhost.server_name }}
root {{ vhost.root }}
}
{% endfor %}
* 生成結果:
server {
listen 8080
server_name web1.magedu.com
root /var/www/nginx/web1/
}
server {
listen 8080
server_name web2.magedu.com
root /var/www/nginx/web2/
}
server {
listen 8080
server_name web3.magedu.com
root /var/www/nginx/web3/
}
* 案例4
// templates/nginx.conf.j2
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost.listen }}
{% if vhost.server_name is defined %}
server_name {{ vhost.server_name }}
{% endif %}
root {{ vhost.root }}
}
{% endfor %}
// temnginx.yml
—
– hosts: mageduweb
remote_user: root
vars:
nginx_vhosts:
– web1:
listen: 8080
#server_name: “web1.magedu.com”
root: “/var/www/nginx/web1/”
– web2:
listen: 8080
server_name: “web2.magedu.com”
root: “/var/www/nginx/web2/”
– web3:
listen: 8080
server_name: “web3.magedu.com”
root: “/var/www/nginx/web3/”
## 案例1
# nginx_vhosts:
# – web1
# – web2
# – web3
## 案例2
tasks:
– name: template config to remote hosts
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
* 執行命令
ansible-playbook temnginx.yml
* 生成的結果
server {
listen 8080
root /var/www/nginx/web1/
}
server {
listen 8080
server_name web2.magedu.com
root /var/www/nginx/web2/
}
server {
listen 8080
server_name web3.magedu.com
root /var/www/nginx/web3/
}
ansible-playbook when條件判斷
—
– hosts: mageduweb
remote_user: root
tasks:
– name: add group nginx
tags: user
user: name=nginx state=present
– name: add user nginx
user: name=nginx state=present group=nginx
– name: Install Nginx
yum: name=nginx state=present
– name: restart Nginx
service: name=nginx state=restarted
when: ansible_distribution_major_version == “6”
ansible-playbook with_items 列表
—
– hosts: mageduweb
remote_user: root
tasks:
– name: create rsyncd file
copy: src={{ item }} dest=/tmp/{{ item }}
with_items:
– a
– b
– c
– d
– name: yum install httpd
yum: name={{ item }} state=present
with_items:
– apr
– apr-util
– httpd
* with_itmes 嵌套子變量
—
– hosts: mageduweb
remote_user: root
tasks:
– name: add several users
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
– { name: ‘testuser1’ , groups: ‘wheel’}
– { name: ‘testuser2’ , groups: ‘root’}
ansible-playbook 變量定義其它內容
Roles (角色)
針對大型項目使用Roles進行編排。
調用角色的.yml的文件必須和roles同級目錄
roles目錄結構:
playbook.yml
roles/
project/
tasks/
files/
vars/ # 不經常用
default/ # 不經常用
templates/
handlers/
meta/ # 不經常用
userconf.yml
roles/
└── nginx
├── files
│ └── main.yml
├── tasks
│ ├── groupadd.yml
│ ├── install.yml
│ ├── main.yml
│ ├── restart.yml
│ └── useradd.yml
└── vars
└── main.yml
saltstack
puppet
class 面向對象,寫程序一個較為高級的使用。
* 完整的roles架構
// nginx-role.yml 頂層任務調用yml文件
—
0- hosts: mageduweb
remote_user: root
roles:
– role: nginx
~
cat roles/nginx/tasks/main.yml
—
– include: groupadd.yml
– include: useradd.yml
– include: install.yml
– include: restart.yml
– include: filecp.yml
cat roles/nginx/tasks/install.yml
—
– name: Install Nginx
yum: name=nginx state=present
ls roles/nginx/files/
main.yml tom.md
* roles playbook tags使用
ansible-playbook –tags=”nginx,httpd,mysql” nginx-role.yml
// nginx-role.yml
—
– hosts: mageduweb
remote_user: root
roles:
– { role: nginx ,tags: [ ‘nginx’, ‘web’ ] }
– { role: httpd ,tags: [ ‘httpd’, ‘web’ ] }
– { role: mysql ,tags: [ ‘mysql’, ‘db’ ] }
– { role: marridb ,tags: [ ‘mysql’, ‘db’ ] }
– { role: php }
—
– hosts: mageduweb
remote_user: root
roles:
– { role: nginx ,tags: [ ‘nginx’, ‘web’ ] ,when: ansible_distribution_major_version == “6” }
– { role: httpd ,tags: [ ‘httpd’, ‘web’ ] }
– { role: mysql ,tags: [ ‘mysql’, ‘db’ ] }
– { role: marridb ,tags: [ ‘mysql’, ‘db’ ] }
– { role: php }
其它自己研究的功能
* 委任(指定某一臺機器做某一個task)
delegate_to
local_action (專指針對ansible命令執行的機器做的變更操作)
* 交互提示
prompt
* 暫停(java)
wait_for
* Debug
debug: msg=”This always executes.”
* Include
* Template 多值合并
* Template 動態變量配置
推薦資料
http://galaxy.ansible.com
https://galaxy.ansible.com/explore#/
http://github.com/
http://ansible.com.cn/
https://github.com/ansible/ansible
https://github.com/ansible/ansible-examples
原創文章,作者:shewei,如若轉載,請注明出處:http://www.www58058.com/76792