PlayBook-(1)
YAML語言介紹
參考連結: http://docs.ansible.com/ansible/YAMLSyntax.html ansible裡面的配置檔案是通過YMAL檔案來實作的。下面是YMAL語言的特點:
- 可讀性高
- 和腳本語言互動好
- 使用的是實作語言的資料類型
- 有一個一緻的資訊模型
- 基于流來處理
- 表達能力強,可拓展性比較好
YAML語言文法特點:
- 下面是一個介紹YAML的 小例子:
- name: Tom
- age: 35
- spouce:
- name: Tik
- age: 34
- children:
- - name: JK
- age: 19
- - name: HK
- age: 12
說明:表示Tom今年35歲,有一個幸福的四口之家,妻子叫做Tik,今年34歲;倆孩子JK和HK活潑可愛。
接下來就把這上面的代碼儲存成一個test.yaml的檔案放到和test.py同級的目錄下,并且用python2.7執行即可。 python代碼:
- #!/usr/bin/python
- import yaml
- file = open('test.yaml')
- read_file = yaml.load(file)
- print read_file
執行結果:會傳遞出一個字典的格式出來
- [[email protected] ~]# python test.py
- {'age': 35, 'spouce': {'age': 34, 'name': 'Tik'}, 'name': 'Tom', 'children': [{'age': 19, 'name': 'JK'}, {'age': 12, 'name': 'HK'}]}
- YAML檔案最常見的層次和結構,對應的就是python中的字典和清單兩種類型:
還是用上面的python讀取檔案的例子來試試,生成清單
- [[email protected] ~]# cat test2.yaml #yaml檔案
- - apple
- - banana
- - orange
- - pear
- [[email protected] ~]# cat test2.py #python檔案
- #!/usr/bin/python
- import yaml
- file = open('test2.yaml')
- read_file = yaml.load(file)
- print read_file
- [[email protected] ~]# python test2.py #執行結果
- ['apple', 'banana', 'orange', 'pear']
再來一發玩玩吧:
- node_a:
- counttime: 300
- external:
- iface: eth0
- port: 5566
- internal:
- iface: eth1
- port: 5577
- broadcast:
- client: 1000
- server: 99
- node_b:
- 0:
- ip: 10.1.1.1
- name: b1
- 1:
- ip: 10.1.1.2
- name: b2
不行,老哥最後來一發
- name: Example Developer
- job: DeveLoper
- skill: Elipt
- employed: True
- foods:
- - apple
- - orange
- - mango
- language:
- ruby: Elit
- python: Elit
- dotnet: Lame
PlayBook
需要一個yaml格式的檔案編排一個任務去執行。
- 組成部分(核心元素):
- hosts
- users
- 任務
- 變量
- 模闆:包含模闆文法的文本檔案
- 處理器:有特定條件觸發的任務
- 角色
- 基本元件:
- hosts:運作指定任務的目标主機
- remote_user:遠端主機上執行任務的使用者
- sudo_user:可選方式
- tasks:任務清單
- 子產品,子產品參數
- 格式:
- action:module arguments
- module:arguments
- 簡單的小案例:
- 建立使用者的小playbook:
- - hosts: all
- remote_user: root
- tasks:
- - name: create a user user3 #任務名稱
- user: name=user3 system=true uid=307 #執行任務
- - name: create a user user4 #任務名稱
- user: name=user4 system=true uid=308
- 檢查playbook
- [[email protected] ~]# ansible-playbook --check first.yaml
- PLAY [all] *********************************************************************
- TASK [setup] *******************************************************************
- ok: [172.1.1.7]
- ok: [172.1.1.12]
- TASK [create a user user3] *****************************************************
- changed: [172.1.1.7]
- changed: [172.1.1.12]
- TASK [create a user user4] *****************************************************
- changed: [172.1.1.7]
- changed: [172.1.1.12]
- PLAY RECAP *********************************************************************
- 172.1.1.12 : ok=3 changed=2 unreachable=0 failed=0
- 172.1.1.7 : ok=3 changed=2 unreachable=0 failed=0
- [[email protected] ~]# ansible-playbook --list-hosts first.yaml
-
- playbook: first.yaml
-
- play #1 (all): all TAGS: []
- pattern: [u'all']
- hosts (2):
- 172.1.1.12
- 172.1.1.7
- 運作playbook
- [[email protected] ~]# ansible-playbook first.yaml #執行
-
- PLAY [all] *********************************************************************
-
- TASK [setup] *******************************************************************
- ok: [172.1.1.12]
- ok: [172.1.1.7]
-
- TASK [create a user user3] *****************************************************
- changed: [172.1.1.7]
- changed: [172.1.1.12]
-
- TASK [create a user user4] *****************************************************
- changed: [172.1.1.7]
- changed: [172.1.1.12]
-
- PLAY RECAP *********************************************************************
- 172.1.1.12 : ok=3 changed=2 unreachable=0 failed=0
- 172.1.1.7 : ok=3 changed=2 unreachable=0 failed=0
安裝httpd使其監聽在8080端口, yaml檔案如下
- - hosts: webservers
- remote_user: root
- tasks:
- - name: install httpd
- yum: name=httpd state=present
- - name: copy configure file
- copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf
- - name: start service
- service: name=httpd state=started
- - name: execute ss command
- shell: ss -tnl | grep 80
準備好httpd的配置檔案: 測試執行:ansible-playbook --check httpd.yaml 正式執行:ansible-playbook httpd.yaml
- handlers:在特定條件下觸發條件,重新開機服務
- 接收到其他任務的通知時才被觸發
- task:任務的狀态在運作後為chenged時,可通過notify通知給相應的handles
- 通過tags打标簽,可以結合ansible-playbook -t 标簽名稱 yaml檔案執行
改變配置檔案之後重新開機httpd服務:
- - hosts: webservers
- remote_user: root
- tasks:
- - name: install httpd
- yum: name=httpd state=present
- - name: copy configure file
- copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf
- notify: restart httpd
- - name: start service
- service: name=httpd state=started
- handlers:
- - name: restart httpd
- service: name=httpd state=restarted
- tag:打标簽機制---通過執行标簽對應的任務片段執行對應的task
- - hosts: webservers
- remote_user: root
- tasks:
- - name: install httpd
- yum: name=httpd state=present
- tags: insthttpd
- - name: copy configure file
- copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf
- tags: instconf
- notify: restart httpd
- - name: start service
- service: name=httpd state=started
- tags: restarthttpd
- handlers:
- - name: restart httpd
- service: name=httpd state=restarted
測試:ansible-playbook --check -t restarthttpd httpd3.yaml 執行:ansible-playbook -t restarthtpd httpd3.yaml
- variables:變量,隻能以字母開頭
- facts:用setup子產品來擷取facts的;可以直接調用
- ansible-playbook指令的指令行中的自定義變量
- -e VARS,--extra-vars=VARS
- 通過roles也能傳遞變量
- Host Inventory:實作想不通的主機傳遞不同的變量
- 向不同的主機傳遞不同的變量:IP/HOSTNAME varrable=value1 value2
- 向組中的主機傳遞相同的變量:
- [groupnames:vars]
variable=value
- inventory參數:用于定義遠端連接配接目标主機需要的參數,而非傳遞給playbook的參數
- ansible_ssh_host
- ansible_ssh_porty
- ansible_ssh_pass
- ansible_ssh_user
- ansible_sudo_pass
- 模闆:
實作自定義安裝軟體包{{ pkgnames }}:
- - hosts: webservers
- remote_user: root
- tasks:
- - name: install {{ pkgs }}
- yum: name={{ pkgs }} state=present
測試:ansible-playbook --check -e pkgs=memcached install_var.yaml 執行:ansible-playbook -e pkgs=memcached install_var.yaml 實作自定義修改主機名的操作: 1. 首先修改/etc/ansbile/hosts檔案:
- [[email protected] ~]# vim /etc/ansible/hosts
- [webservers]
- 172.1.1.12 hname=www1
- 172.1.1.7 hname=www2
2.其次編寫hostname.yaml檔案
- [[email protected] ~]# vim hostname.yaml
- - hosts: webservers remote_user: root tasks: - name: set hostname hostname: name={{ hname }}
3.測試: ansible-playbook --check hostname.yaml 4.執行: ansible-playbook hostname.yaml
playbook-(2)
playbook的其他元素
變量
- 變量:一定要記得{{ varname }}
- ansible facts
- ansible-playbook -e "var=value"
- host variable: host inventory
- group variable:
[groupname:vars] var=value
- roles
- 變量的調用:{{ variable }}
模闆(templates)
- 文本檔案,嵌套有腳本(使用模闆程式設計語言編寫)
- jinjs2:基于Python
- 字面量:
- 字元串:使用單引号或者雙引号
- 數字:整數和浮點數
- 清單
- 元組
- 字典
- 布爾值
- 算術運算:+ - * / // % **n
- 比較操作:== != > < >= <=
- 邏輯運算:and or not
- 字面量:
- template子產品:基于模闆的方式生成一個檔案複制到遠端主機
- *src:jinjia2檔案路徑(server端)
- *dest:用戶端檔案存放路徑
擷取虛拟CPU數量:
- [[email protected] ~]# ansible all -m setup | grep ansible_processor_vcpus
- "ansible_processor_vcpus": 1,
- "ansible_processor_vcpus": 1,
nginx使用epel源安裝執行個體: 1.在主要端下載下傳好nginx的epel檔案:
- [[email protected] ~]# cat nginx.repo
- [nginx]
- name=nginx repo
- baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
- gpgcheck=0
- enabled=1
2.然後epel檔案推送到被控端
- [[email protected] ~]# ansible all -m copy -a "src=~/nginx.repo dest=/etc/yum.repos.d/"
3.主要端執行安裝指令
- [[email protected] ~]# ansible all -m yum -a "name=nginx state=present"
4.配置檔案模闆推送 * 遠端複制一個nginx.conf配置檔案來當做模闆檔案,并重命名
- [[email protected] ~]# scp 172.1.1.12:/etc/nginx/nginx.conf ./
- nginx.conf 100% 643 0.6KB/s 00:00
- [[email protected] ~]# mv nginx.conf nginx.conf.j2
*編輯模闆檔案:
- [[email protected] ~]# egrep -v "^#|^$" nginx.conf.j2 user nginx; worker_processes {{ ansible_processor_vcpus }}; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; server { listen {{ http_port }}; server_name localhost; charset koi8-r; location / { root /usr/share/nginx/html; index index.html index.htm; } } }
*編輯hosts檔案:
- [[email protected] ~]# cat /etc/ansible/hosts
- # This is the default ansible 'hosts' file.
- #
- # It should live in /etc/ansible/hosts
- #
- # - Comments begin with the '#' character
- # - Blank lines are ignored
- # - Groups of hosts are delimited by [header] elements
- # - You can enter hostnames or ip addresses
- # - A hostname/ip can be a member of multiple groups
-
- [webservers]
- 172.1.1.12 hname=www1
- 172.1.1.7 hname=www2
-
- [webservers:vars]
- http_port=8080
或者寫成這樣以便區分:
- [[email protected] ~]# cat /etc/ansible/hosts
- # This is the default ansible 'hosts' file.
- #
- # It should live in /etc/ansible/hosts
- #
- # - Comments begin with the '#' character
- # - Blank lines are ignored
- # - Groups of hosts are delimited by [header] elements
- # - You can enter hostnames or ip addresses
- # - A hostname/ip can be a member of multiple groups
-
- [webservers]
- 172.1.1.12 http_port=80
- 172.1.1.7 http_port=8080
*編輯yaml檔案:
- [[email protected] ~]# vim nginx.yaml
- - hosts: webservers
- remote_user: root
- tasks:
- - name: install nginx
- yum: name=nginx state=present
- - name: install conf file
- template: src=~/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- notify: restart nginx
- tags: instconf
- - name: start nginx service
- service: name=nginx state=started
- handlers:
- - name: restart nginx
- service: name=nginx state=restarted
*檢測執行:
- [[email protected] ~]# ansible-playbook --check nginx.yaml
-
- PLAY [webservers] **************************************************************
-
- TASK [setup] *******************************************************************
- ok: [172.1.1.7]
- ok: [172.1.1.12]
-
- TASK [install nginx] ***********************************************************
- ok: [172.1.1.7]
- ok: [172.1.1.12]
-
- TASK [install conf file] *******************************************************
- ok: [172.1.1.7]
- ok: [172.1.1.12]
-
- TASK [start nginx service] *****************************************************
- changed: [172.1.1.7]
- changed: [172.1.1.12]
-
- PLAY RECAP *********************************************************************
- 172.1.1.12 : ok=4 changed=1 unreachable=0 failed=0
- 172.1.1.7 : ok=4 changed=1 unreachable=0 failed=0
沒有報錯的話就直接執行:ansible-playbook nginx.yaml
- 條件判斷:
- when語句:在task中使用,jinja2文法格式
- tasks格式如下:
- tasks:
- - name: install conf to centos7
- templete: src=~/nginx.c7.j2 dest=/etc/nginx/
- when: ansible_distribution_major_version == "7"
- - name: install conf to centos7
- templete: src=~/nginx.c6.j2 dest=/etc/nginx/
- when: ansible_distribution_major_version == "6"
根據作業系統的不同來配置和推送不同的配置檔案: *根據不同的作業系統拉取不同的配置檔案:
- [[email protected] ~]# scp 172.1.1.7:/etc/nginx/nginx.conf ./nginx.conf.c6.j2
- nginx.conf 100% 700 0.7KB/s 00:00
- [[email protected] ~]# scp 172.1.1.12:/etc/nginx/nginx.conf ./nginx.conf.c7.j2
- nginx.conf 100% 702 0.7KB/s 00:00
*調整 nginx . conf . c6 . j2的配置檔案;仍然要修改其worker_processor 對應的變量為: worker_processes {{ ansible_processor_vcpus }}; listen {{ http_port }};
*調整nginx.yaml配置:
- [[email protected] ~]# vim nginx.yaml
- - hosts: webservers
- remote_user: root
- tasks:
- - name: install nginx
- yum: name=nginx state=present
- - name: install conf file
- template: src=~/nginx.conf.c7.j2 dest=/etc/nginx/nginx.conf
- when: ansible_distribution_major_version == "7"
- notify: restart nginx
- tags: instconf
- - name: install conf file
- template: src=~/nginx.conf.c6.j2 dest=/etc/nginx/nginx.conf
- when: ansible_distribution_major_version == "6"
- notify: restart nginx
- tags: instconf
- - name: start nginx service
- service: name=nginx state=started
- handlers:
- - name: restart nginx
- service: name=nginx state=restarted
*執行檢查程式:ansible-playbook --check nginx.yaml *無報錯開始執行程式:ansible-playbook nginx.yaml
- 循環:疊代操作,需要執行重複執行的任務
- 對疊帶項的引用,固定變量名為"item"
- 而後,要在task中使用with_items給定要疊代的元素清單
- with_items的形式有兩種:
- 清單方法
- 字典方法
事例:
- - name: install some package
- yum: name={{ items }} state=present
- with_items:
- - nginx
- - memcached
- - php-fpm
疊代安裝程式事例: *編寫yaml檔案:
- - host : all
- remote_user: root
- tasks:
- - name: install some packages
- yum: name={{ item }} state=present
- with_items:
- - nginx
- - memcached
- - php-fpm
開始測試:ansible-playbook --check loop_test.yaml 執行任務:ansible-playbook loop_test.yaml 疊代建立使用者群組,并進行關聯( with_items的形式有兩種的具體應用) *編寫yaml檔案:
- [[email protected] ~]#vim with_items.yaml
- - host: all
- remote_user: root
- tasks:
- - name: add some groups
- group: name={{ item }} state=present
- with_items:
- - group11
- - group12
- - group13
- - name: add some users
- user: name={{ item.name }} group={{ item.group }} state=present
- with_items:
- - { name: 'user11',group: 'group11' }
- - { name: 'user11',group: 'group11' }
- - { name: 'user11',group: 'group11' }
*測試執行:ansible-playbook --check with_items . yaml
*無報錯最終執行:ansible-playbook with_items.yaml
playbook-(3)
角色
- 在網絡中的主機如果要協調的過程,實作不同的分組之間的公共任務的執行,以減少重複執行。比如時間同步;
- 每一個角色就是一個目錄;
(1): 角色的集合:
- roles/
- mysql/
- httpd/
- nginx/
- memcached/
(2): 每個角色,以特定的層級目錄結構進行 組織
- mysql/
- files/ #存放由copy或者script子產品等調用的檔案
- templates/ #template子產品查找所需要的模闆檔案位置
- tasks/ #至少包含一個main.yaml檔案,其他檔案的執行需要通過include包含
- handlers/ #至少包含一個main.yaml的檔案, 其他檔案的執行需要通過include包含
- vars/ # 至少包含一個main.yaml的檔案, 其他檔案的執行需要通過include包含
- meta/ #至少包含一個main.yaml的檔案,定義目前角色的特殊設定及其依賴關系
- default/ #設定預設變量時使用此目錄中的main.yaml檔案
(3): 在playbook中調用角色:通過roles指定的任務,然後從上面的兩個對應的角色集合中找到自己的角色和對應的任務進行執行
- - hosts: webservers
- remoute_user: root
- roles:
- - mysql
- - ngins
- - memcached
- - redis
執行個體:建立一個nginx執行個體
(1)task檔案
- [[email protected] ~]# mkdir /etc/ansible/roles/nginx/{files,tasks,templates,handlers,vars,default,meta} -pv
- [[email protected] ~]# cd /etc/ansible/roles/nginx/
- [[email protected] nginx]# vim tasks/main.yaml
- - name: install nginx package
- yum: name=nginx state=present
- - name: install conf file
- template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- - name: start nginx
- service: name=nginx state=started enabled=true
(2)templates檔案
- [[email protected] ~]# vim /etc/ansible/roles/nginx/templates/nginx.conf.j2
- user nginx;
- worker_processes {{ ansible_processor_vcpus }};
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- #tcp_nopush on;
- keepalive_timeout 65;
- #gzip on;
-
- server {
- listen {{ http_port }};
- server_name localhost;
-
- charset koi8-r;
-
- location / {
- root /usr/share/nginx/html;
- index index.html index.htm;
- }
- }
- }
(3)寫一個role檔案
- [[email protected] ~]# mkdir /ansible
- [[email protected] ansible]# vim nginx.yaml
- - hosts: webservers
- remote_user: root
- roles:
- - nginx
(4)删除被控主機的nginx服務和配置
- [[email protected] ~]# rpm -e nginx
- warning: /etc/nginx/nginx.conf saved as /etc/nginx/nginx.conf.rpmsave
- warning: file /etc/nginx/conf.d/default.conf: remove failed: No such file or directory
- [[email protected] ~]# rm -fr /etc/nginx/
- [[email protected] ~]# rpm -e nginx
- error: package nginx is not installed
(5)測試和執行
- [[email protected] ~]# cd /ansible/
- [[email protected] ansible]# ansible-playbook --check nginx.yaml #測試
- [[email protected] ansible]# ansible-playbook nginx.yaml #執行
(6)觸發器的建立
- [[email protected] ansible]# vim /etc/ansible/roles/nginx/handlers/main.yaml
- - name: restart nginx
- service: name=nginx state=restarted
(7)tasks建立通知
- [[email protected] ansible]# vim /etc/ansible/roles/nginx/tasks/main.yaml
- - name: install nginx package
- yum: name=nginx state=present
- - name: install conf file
- template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- notify: restart nginx
- - name: start nginx
- service: name=nginx state=started enabled=true
(8)模拟配置檔案發生改變(修改j2檔案 )
- [[email protected] ansible]# vim /etc/ansible/roles/nginx/templates/nginx.conf.j2
- user nginx;
- worker_processes {{ ansible_processor_vcpus-1 }};
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- #tcp_nopush on;
- keepalive_timeout 65;
- #gzip on;
-
- server {
- listen {{ http_port }};
- server_name localhost;
-
- charset koi8-r;
-
- location / {
- root /usr/share/nginx/html;
- index index.html index.htm;
- }
- }
- }
(9) 測試和執行
- [[email protected] ~]# cd /ansible/
- [[email protected] ansible]# ansible-playbook --check nginx.yaml #測試
- [[email protected] ansible]# ansible-playbook nginx.yaml #執行
添加通知
(1)修改tasks配置
- [[email protected] ansible]# vim /etc/ansible/roles/nginx/tasks/main.yaml
- - name: install nginx package
- yum: name=nginx state=present
- - name: install conf file
- template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- notify: restart nginx
- tags: instconf
- - name: start nginx
- service: name=nginx state=started enabled=true
(2) 測試和執行
- [[email protected] ~]# cd /ansible/
- [[email protected] ansible]# ansible-playbook -t instconf --check nginx.yaml #測試
- [[email protected] ansible]# ansible-playbook -t instconf nginx.yaml #執行
var變量的使用
- [[email protected] ansible]# vim /ansible/useradd.yaml
- - hosts: webservers
- remote_user: root
- vars:
- - groupname: testgroup1
- - username: testuser1
- tasks:
- - name: create group
- group: name={{ groupname }} state=present
- - name: create user
- user: name={{ username }} state=present
測試和執行
- [[email protected] ~]# cd /ansible/
- [[email protected] ansible]# ansible-playbook -t instconf --check useradd.yaml #測試
- [[email protected] ansible]# ansible-playbook -t instconf useradd.yaml #執行
在playbook中定義變量的方式:
還是結合上述的ngin執行個體: (1)定義vars變量檔案
- [[email protected] ansible]# vim /etc/ansible/roles/nginx/vars/main.yaml
- username: daemon
(2)修改j2檔案:
- [[email protected] ansible]# vim /etc/ansible/roles/nginx/templates/nginx.conf.j2
- user {{ username }};
- worker_processes {{ ansible_processor_vcpus-1 }};
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- #tcp_nopush on;
- keepalive_timeout 65;
- #gzip on;
-
- server {
- listen {{ http_port }};
- server_name localhost;
-
- charset koi8-r;
-
- location / {
- root /usr/share/nginx/html;
- index index.html index.htm;
- }
- }
- }
(3) 測試和執行
- [[email protected] ~]# cd /ansible/
- [[email protected] ansible]# ansible-playbook -t instconf --check nginx.yaml #測試
- [[email protected] ansible]# ansible-playbook -t instconf nginx.yaml #執行在
在playbook中調用角色的方法:
(1)在playbook中調用角色:通過roles指定的任務,然後從上面的兩個對應的角色集合中找到自己的角色和對應的任務進行執行
- - hosts: webservers
- remoute_user: root
- roles:
- - mysql
- - ngins
- - memcached
- - redis
(2)通過變量指明方法:其中鍵role用于指定角色名稱;後續的k/v用于傳遞變量給角色 還可以基于條件測試實作角色調用,
- - hosts: webservers
- remote_user: root
- roles:
- - { role: nginx,username :nginx }
第二種方法的執行個體(不指定條件):
- [[email protected] ~]# vim /ansible/nginx.yaml
- - hosts: webservers
- remote_user: root
- roles:
- - { role: nginx,username :nginx }
執行程式:
- [[email protected] ~]# ansible-playbook --check nginx.yaml #執行
-
- PLAY [webservers] **************************************************************
-
- TASK [setup] *******************************************************************
- ok: [172.1.1.7]
- ok: [172.1.1.12]
-
- TASK [install nginx] ***********************************************************
- ok: [172.1.1.7]
- ok: [172.1.1.12]
-
- TASK [install conf file] *******************************************************
- ok: [172.1.1.7]
- ok: [172.1.1.12]
-
- TASK [start nginx service] *****************************************************
- ok: [172.1.1.7]
- ok: [172.1.1.12]
-
- PLAY RECAP *********************************************************************
- 172.1.1.12 : ok=4 changed=0 unreachable=0 failed=0
- 172.1.1.7 : ok=4 changed=0 unreachable=0 failed=0
-
- [[email protected] ~]# ansible all -m shell -a "ps -ef | grep nginx" #傳回結果
- 172.1.1.7 | SUCCESS | rc=0 >>
- root 10412 1 0 10:21 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
- nginx 10414 10412 0 10:21 ? 00:00:00 nginx: worker process
- root 12949 12948 0 11:00 pts/1 00:00:00 /bin/sh -c ps -ef | grep nginx
- root 12951 12949 0 11:00 pts/1 00:00:00 grep nginx
-
- 172.1.1.12 | SUCCESS | rc=0 >>
- root 4080 1 0 18:06 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
- nginx 4081 4080 0 18:06 ? 00:00:00 nginx: worker process
- root 5013 5008 0 18:54 pts/2 00:00:00 /bin/sh -c ps -ef | grep nginx
- root 5015 5013 0 18:54 pts/2 00:00:00 grep nginx
第二種方法的執行個體(指定條件)
- - hosts: webservers
- remote_user: root
- roles:
- - { role: nginx,username: nginx,when:"ansible_distribution_major_version == '7'" }按條件安裝
按條件安裝memcached軟體: 建立角色:
- [[email protected] ~]# mkdir -pv /etc/ansible/roles/memcached/tasks
- [[email protected] tasks]# cd /etc/ansible/roles/memcached
- [[email protected] memcached]# vim tasks/main.yaml
- - name: install package
- yum: name=memcached state=present
- - name: start memcached
- service: name=memcached state=started enabled=true
建立執行入口檔案
- [[email protected] memcached]# cd /ansible/
- [[email protected] memcached]# vim nginx_memcached.yaml
- - hosts: all
- remote_user: root
- roles:
- - { role: nginx, when: ansible_distribution_major_version == '7' }
- - { role: memcached, when: ansible_hostname == 'memcached' }
模闆檔案的建立:
- [[email protected] ansible]# cd /etc/ansible/roles/memcached/
- [[email protected] memcached]# mkdir templates
- [[email protected] memcached]# scp 172.1.1.7:/etc/sysconfig/memcached ./templates/
- [[email protected] memcached]# vim templates/memcached
- PORT="11211"
- USER="memcached"
- MAXCONN="1024"
- CACHESIZE="{{ ansible_memtotal_mb//4 }}"
- OPTIONS=""
- [[email protected] memcached]# cd templates/
- [[email protected] templates]# mv memcached memcached.j2
-
再次修改角色檔案:
- [[email protected] templates]# vim ../tasks/main.yaml
- - name: install package
- yum: name=memcached state=present
- - name: install conf
- template: src=memcached.j2 dest=/etc/sysconfig/memcached
- notify: restart memcached
- tags: memconf
- - name: start memcached
- service: name=memcached state=started enabled=true
建立handlers檔案:
- [[email protected] memcached]# mkdir handlers
- [[email protected] memcached]# vim handlers/main.yaml
- - name: restart memched
- service: name=memcached state=restarted
執行測試:
- [[email protected] ansible]# ansible-playbook --check nginx_memcached.yaml
- [[email protected] ansible]# ansible-playbook -t memconf --check nginx_memcached.yaml
驗證:
- [[email protected] ansible]# ansible all -m shell -a "cat /etc/sysconfig/memcached"
- 172.1.1.7 | SUCCESS | rc=0 >>
- PORT="11211"
- USER="memcached"
- MAXCONN="1024"
- CACHESIZE="245"
- OPTIONS=""
站點:www.ansible.com.cn