当前位置:网站首页>Ansible playbook和Ansible Roles(三)
Ansible playbook和Ansible Roles(三)
2022-06-12 21:43:00 【熬夜泡枸杞】
文章目錄
1. playbook判斷語句
判斷在Ansible任務中得使用頻率非常高。比如yum模塊可以檢測軟件包是否已被安裝,而在這個過程中我們不用做太多得人工幹預
但是也有不放呢任務需要進行判斷,比如:web服務器角色都需要安裝nginx倉庫,但其他服務器角色並不需要,此時就會用到when判斷。
比如:Centos與Ubuntu系統都需要安裝httpd服務,那麼就需要使用when判斷主機系統,然後調用不同得模塊執行。
實踐案例1: 根據不同操作系統,安裝相同的軟件包
[[email protected] when]# cat when.yml
- hosts: webservers
tasks:
- name: Install httpd Server
yum: name=httpd state=present
when: ansible_distribution == "CentOS"
- name: Install httpd Server
apt: name=httpd2 state=present
when: ansible_distribution == "Ubuntu"
實踐案例2:所有為web主機名的添加nginx倉庫,其餘的都跳過添加
1.如何添加yum倉庫
2.如何判斷,判斷什麼內容
---
[[email protected] when]# cat when.yml
- hosts: web
tasks:
- name: Add Nginx Repos
yum_repository: #模塊
name: nginx_tet #它代錶/etc/yum.repos.d/nginx_tet.repo
description: Nginx YUM repo #描述 (不加會報錯)
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no #把效驗本地給關掉
when: (ansible_hostname is match ("web*")) or (ansible_hostname is match ("lb*")) #根據官方的寫法
1.通過register將命令執行結果保存至變量,然後通過when語句進行判斷
- hosts: webservers
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
ignore_errors: yes
register: check_httpd
#- name: debug outprint #僅僅只是輸出結果
# debug: var=check_httpd
- name: Httpd Restart
service: name=httpd state=restarted
when: check_httpd.rc == 0
==========================================
(1)按照不同的主機名稱進行判斷
[[email protected] when]# cat when.yml
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
tasks:
- name: Print ip # 進行測試的
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- name: Reboot Server
command: reboot
when: ansible_fqdn == "web02"
(2)按照不同的IP地址進行判斷
[[email protected] when]# cat when.yml
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
tasks:
- name: Print ip
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- name: Reboot Server
command: reboot
when: ansible_default_ipv4.address == "10.0.0.7"
(3)按照獲取客戶端的某參數值得大小判斷
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
host_mem: "{
{ ansible_memtotal_mb }}"
tasks:
- name: Print ip
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- "{
{ host_mem }}"
- name: Reboot Server
command: reboot
when: ansible_memtotal_mb|int < "2000"
(4)列錶方式判斷 and關系 並且關系
[[email protected] when]# cat when.yml
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
host_mem: "{
{ ansible_memtotal_mb }}"
host_ver: "{
{ ansible_distribution }}"
tasks:
- name: Print ip
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- "{
{ host_mem }}"
- "{
{ host_ver }}"
- name: Reboot Server
file:
path: /root/web01.txt
state: touch
when:
- ansible_distribution == "CentOS"
- ansible_fqdn == "web01"
(6)或者關系判斷
[[email protected] when]# cat when.yml
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
host_mem: "{
{ ansible_memtotal_mb }}"
host_ver: "{
{ ansible_distribution }}"
tasks:
- name: Print ip
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- "{
{ host_mem }}"
- "{
{ host_ver }}"
- name: Reboot Server
file:
path: /root/web0102.txt
state: touch
when: ansible_distribution == "CentOS" or ansible_fqdn == "web02"
(7) 修改Nginx配置文件 把Nginx從ansible服務器拷貝到web服務器
拷貝過去需要啟動或者重啟 如果Nginx配置文件不對 不讓加載
1. 安裝Nginx
2. 拷貝文件到Nginx.conf 判斷文件是否正確 ngixn -t 變量注册接收結果
3. 啟動
4. 重新加載 在重新加載的地方進行when判斷
[[email protected] when]# cat nginx.yml
- hosts: web02
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: scp configure Nginx
copy:
src: ./nginx.j2
dest: /etc/nginx/nginx.conf
- name: Check Nginx Configure
command: /usr/sbin/nginx -t
register: result
ignore_errors: yes # 忽略錯誤繼續執行
- name: print result
debug:
msg: "{
{ result.rc }}"
- name: Reload Nginx
systemd:
name: nginx
state: reloaded
when: result.rc == 0 # 判斷nginx -t執行結果 是否為0 通過變量注册獲取的
-----------------------------
- name: Reload Nginx
systemd:
name: nginx
state: reloaded
when: result.rc is match "0"
(8)rsync服務進行主機名的判斷(rsync的優化)
[[email protected] rsync]# cat rsync.yml
- hosts: rsyncall
tasks:
- name: Install Rsyncd Server
yum:
name: rsync
state: present
- name: Create www Group
group:
name: www
gid: 666
- name: Create User www
user:
name: www
uid: 666
group: www
create_home: false
shell: /sbin/nologin
- name: Configure Rsync Server
copy:
src: rsync.j2
dest: /etc/rsyncd.conf
when: ansible_hostname == "backup"
- name: Auth Password
copy:
content: rsync_backup:123456
dest: /etc/rsync.passwd
mode: 0600
when: ansible_hostname == "backup"
- name: Create Dir /backup
file:
path: /backup
state: directory
owner: www
group: www
recurse: yes
when: ansible_hostname == "backup"
- name: Start Rsyncd Server
systemd:
name: rsyncd
state: started
enabled: yes
when: ansible_hostname == "backup"
- name: clinet Auth Password
copy:
content: 123456
dest: /etc/rsync.passwd
mode: 0600
when: ansible_hostname == "web01" or ansible_hostname == "web02"
(9)判斷http是否存活
- hosts: web02
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: scp configure Nginx
copy:
src: ./nginx.j2
dest: /etc/nginx/nginx.conf
- name: Check Nginx Configure
command: /usr/sbin/nginx -t
register: result
ignore_errors: yes
- name: Check HTTP Server
command: systemctl is-active httpd
register: result_http
ignore_errors: yes
- name: print result
debug: var=result_http
- name: Reload Nginx
systemd:
name: nginx
state: reloaded
when: result.rc == 0
2. playbook循環語句
有時候我們寫playbook得時候發現了很多task都要重複引用某個模塊,比如一次啟動10個服務,或者一次拷貝10個文件,如果按照傳統得寫法最少要寫10次,這樣會顯得playbook很臃腫。如果使用循環得方式來編寫playbook,這樣可以减少重複使用某個模塊。
實踐案例一、使用循環啟動多個服務
[[email protected] project2]# cat with.yml
- hosts: webservers
tasks:
- name: Start httpd mariadb
systemd: name={
{
item }} state=started
with_items:
- httpd
- mariadb
1.案例二、使用定義變量方式循環安裝軟件包。
- hosts: webservers
tasks:
- name: ensure a list of packages installed
yum: name= "{
{
packages }}" state=present
vars:
packages:
- httpd
- httpd-tools
#弃用的方式
- hosts: webservers
tasks:
- name: ensure a list of packages installed
yum: name= "{
{
item }}" state=present
with_items:
- httpd
- httpd-tools
實踐案例三、使用字典循環方式創建用戶和批量拷貝文件
[[email protected] ~]# cat loop-user.yml
- hosts: webservers
tasks:
- name: Add Users
user: name={
{
item.name }} groups={
{
item.groups }} state=present
with_items:
- {
name: 'testuser1', groups: 'bin' }
- {
name: 'testuser2', groups: 'root' }
[[email protected] project2]# cat with4.yml
- hosts: webservers
tasks:
- name: Copy Rsync configure and Rsync passwd
copy: src={
{
item.src }} dest={
{
item.dest }} mode={
{
item.mode }}
with_items:
- {
src: "./rsyncd.conf", dest: "/etc/rsyncd.conf", mode: "0644" }
- {
src: "./rsync.passwd", dest: "/tmp/rsync.passwd", mode: "0600" }
===============================================
官方推薦寫法:
[[email protected] when]# vim when.yml
- hosts: web
tasks:
- name: Started Server
yum: # systemd 起不來
name: "{
{ packages }}"
state: started
vars:
packages:
- httpd
- mariadb
也可以這樣寫:
[[email protected] when]# vim when.yml
- hosts: web
tasks:
- name: Started Server
yum: # systemd 起不來
name: "{
{ item }}"
state: started
with_items:
- httpd
- mariadb
(1)使用with_item循環列錶
[[email protected] file]# cat create_file.yml
- hosts: web
tasks:
- name: Create file
file:
path: "{
{ item }}"
state: touch
with_items:
- file1.txt
- file2.txt
(2)使用loop方式進行循環列錶
[[email protected] file]# cat create_file.yml
- hosts: web
tasks:
- name: Create file
file:
path: "{
{ item }}"
state: touch
loop:
- file1.txt
- file2.txt
(3)循環啟動列錶 一次啟動多個服務
[[email protected] file]# cat start.yml
- hosts: web
tasks:
- name: Install HTTPD Nginx Mariadb Server
yum:
name: "{
{ item }}"
state: present
loop:
- nginx
- mariadb-server
- name: Start HTTP Nginx Mariadb Sever
systemd:
name: "{
{ item }}"
state: started
loop:
- nginx
- mariadb
(4)拷貝兩個文件到目標
1.txt 到目標主機為600
2.txt 到目標主機為644
[[email protected] file]# cat scp.yml
- hosts: web
tasks:
- name: scp file dest web01 web02
copy:
src: "{
{ item }}"
dest: /root/
loop:
- 1.txt
- 2.txt
(5)scp多個文件到目標主機 權限不同
[[email protected] file]# cat scp.yml
- hosts: web
tasks:
- name: scp file dest web01 web02
copy:
src: "{
{ item.src }}"
dest: "{
{ item.dest }}"
owner: "{
{ item.owner }}"
group: "{
{ item.group }}"
mode: "{
{ item.mode }}"
loop:
- { src: 1.txt,dest: /root/,mode: '0600',owner: www,group: root }
- { src: 2.txt,dest: /opt/,mode: '0000',owner: root,group: www }
(6)rsync拷貝文件(rsync優化)
[[email protected] rsync]# vim rsync.yml
[[email protected] rsync]# cat rsync.yml
- hosts: rsyncall
tasks:
- name: Install Rsyncd Server
yum:
name: rsync
state: present
- name: Create www Group
group:
name: www
gid: 666
- name: Create User www
user:
name: www
uid: 666
group: www
create_home: false
shell: /sbin/nologin
- name: Configure Rsync Server
copy:
src: "{
{ item.src }}"
dest: "{
{ item.dest }}"
mode: "{
{ item.mode }}"
loop:
- { src: rsync.j2, dest: /etc/rsyncd.conf,mode: '0644' }
- { src: rsync_pass.j2, dest: /etc/rsync.passwd,mode: '0600' }
when: ansible_hostname == "backup"
- name: Create Dir /backup
file:
path: /backup
state: directory
owner: www
group: www
recurse: yes
when: ansible_hostname == "backup"
- name: Start Rsyncd Server
systemd:
name: rsyncd
state: started
enabled: yes
when: ansible_hostname == "backup"
- name: clinet Auth Password
copy:
content: 123456
dest: /etc/rsync.passwd
mode: 0600
when: ansible_hostname == "web01" or ansible_hostname == "web02"
3. playbook handlers
handler用來執行某些條件下的任務,比如當配置文件發生變化的時候,通過notify觸發handler去重啟服務。
在saltstack中也有類似的觸發器,寫法相對Ansible簡單,只需要watch,配置文件即可。
[[email protected] project2]# cat han.yml
- hosts: webservers
vars:
- http_port: 8083
tasks:
- name: Install Http Server
yum: name=httpd state=present
- name: configure httpd server
template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- Restart Httpd Server
- Restart PHP Server
- name: start httpd server
service: name=httpd state=started enabled=yes
handlers:
- name: Restart Httpd Server
systemd: name=httpd state=restarted
- name: Restart PHP Server
systemd: name=php-fpm state=restarted
3.handlers注意事項
1.無論多少個task通知了相同的handlers,handlers僅會在所有tasks結束後運行一次。
2.只有task發生改變了才會通知handlers,沒有改變則不會觸發handlers
3.不能使用handlers替代tasks
====================================================
handle注意事項:
1.無論多少個task通知了相同的handlers,handlers僅會在所有tasks結束後運行一次。
2.只有task發生改變了才會通知handlers,沒有改變則不會觸發handlers
3.不能使用handlers替代tasks
(1)handlers觸發修改
[[email protected] handlers]# cat handlers.yml
- hosts: web01
vars:
- http_port: 82
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: Configure Nginx Server
template: # 在劇本中可以使用變量,可以使用系統的變量,也可以使用自己定義的變量
src: nginx_conf.j2
dest: /etc/nginx/nginx.conf
notify: Reload Nginx
- name: Start Nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Reload Nginx
systemd:
name: nginx
state: reloaded
(2)觸發後重啟nginx和mariadb
[[email protected] handlers]# cat handlers.yml
- hosts: web01
vars:
- http_port: 83
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: Configure Nginx Server
template:
src: nginx_conf.j2
dest: /etc/nginx/nginx.conf
notify: Reload Nginx
- name: Start Nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Reload Nginx
systemd:
name: "{
{ item }}"
state: restarted
loop:
- nginx
- mariadb
(2)handlers觸發nginx和mariadb重啟 並且when判斷nginx文件是否正確
[[email protected] handlers]# cat handlers.yml
- hosts: web01
vars:
- http_port: 85
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: Configure Nginx Server
template:
src: nginx_conf.j2
dest: /etc/nginx/nginx.conf
notify: Reload Nginx
- name: Check Nginx Configuer
command: /usr/sbin/nginx -t
ignore_errors: yes
register: result
- name: Start Nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Reload Nginx
systemd:
name: "{
{ item }}"
state: restarted
loop:
- nginx
- mariadb
when: result.rc == 0
4. Playbook任務標簽
默認情况下,Ansible在執行一個playbook時,會執行playbook中定義得多有任務。Ansible的標簽(tags)功能可以給單獨任務甚至整個playbook打上標簽,然後利用這些標簽來指定要運行playbook中的個別任務,或不執行指定的任務。(可以進行調試)
1. 打標簽的方式有幾種,比如:
對一個task打一個標簽、對一個task打多個標簽、對多個task打一個標簽
2. 對task打完標簽應該如何使用
-t : 執行指定的tag標簽任務
--skip-tags : 執行--skip-tags之外的標簽任務
ansible-playbook tag.yml --list-tags 查看有多少個tags
tag標記(用於調試的場景下)
[[email protected] project2]# cat tag.yml
- hosts: webservers
vars:
- http_port: 8083
tasks:
- name: Install Http Server
yum: name=httpd state=present
tags:
- install_httpd
- httpd_server
- name: configure httpd server
template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
tags:
- confiure_httpd
- httpd_server
- name: start httpd server
service: name=httpd state=started enabled=yes
tags: service_httpd
handlers:
- name: Restart Httpd Server
systemd: name=httpd state=restarted
[[email protected] project2]# ansible-playbook tag.yml --list-tags
[[email protected] project2]# ansible-playbook tag.yml -t httpd_server
[[email protected] project2]# ansible-playbook tag.yml -t install_httpd,confiure_httpd
[[email protected] project2]# ansible-playbook tag.yml --skip-tags httpd_server
5. Playbook文件複用
include用來動態額包含tasks任務列錶include——tasks新版/include老版
Include包含
include(import_playbook)
include_tasks
[[email protected] project2]# cat task.yml
- hosts: webservers
vars:
- http_port: 801
tasks:
- include_tasks: task_install.yml
- include_tasks: task_configure.yml
- include_tasks: task_start.yml
handlers:
- name: Restart Httpd Server
systemd: name=httpd state=restarted
[[email protected] project2]# cat task_install.yml
- name: Install Http Server
yum: name=httpd state=present
[[email protected] project2]# cat task_configure.yml
- name: configure httpd server
template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
[[email protected] project2]# cat task_start.yml
- name: start httpd server
service: name=httpd state=started enabled=yes
6. Playbook忽略錯誤
默認playbook會檢查tasks執行的返回狀態,如遇到錯誤則會立即種植playbook的後續的tasks執行。然而有些時候playbook即使執行錯誤了也要讓其繼續執行
加入參數:ignore_error:yes 忽略錯誤
- 編寫playbook,當有task執行失敗則會立即終止後續task運行
忽略錯誤ignore_errors
[[email protected] ~]# cat f9.yml
---
- hosts: webservers
tasks:
- name: Ignore False
command: /bin/false
ignore_errors: yes
- name: touch new file
file: path=/tmp/bgx_ignore state=touch
7. Playbook异常處理
通常情况下,當task失敗後,play將會終止,任何在前面已經被tasks notify的handlers都不會被執行。如果你在play中設置了force_handlers: yes參數,被通知的handlers就會被强制執行。(有些特殊場景可能會使用到)
案例一: task執行失敗强制調用handlers
案例二:控制task報告的狀態,不一定必須時"changed"
异常處理
force_handlers: yes 强制調用handlers
changed_when: false 被管理主機沒有發生變化,可以使用參數將change狀態改為ok
changed_when: httpd_check.stdout.find('OK') #查看變量中的某個字符串
[[email protected] project2]# cat changed_when.yml
- hosts: webservers
vars:
- http_port: 8083
tasks:
- name: configure httpd server
template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
- name: Check HTTPD
shell: /usr/sbin/httpd -t
register: httpd_check
changed_when:
- httpd_check.stdout.find('OK')
- false
- name: start httpd server
service: name=httpd state=started enabled=yes
handlers:
- name: Restart Httpd Server
systemd: name=httpd state=restarted
failed_when
命令不依賴返回狀態碼來判定是否執行失敗,而是要查看命令返回內容來决定,比如返回內容中包括 failed 字符串,則判定為失敗。示例如下:
- name: this command prints FAILED when it fails
command: /usr/bin/example-command -x -y -z
register: command_result
failed_when: "'FAILED' in command_result.stderr"
小總結:
-------tasl任務處理相關流程與控制參數
when 判斷
item 循環
handlers 觸發器(需要task使用notify通知)
tags 標簽(調試使用)
include_tasks 包含task任務
ignore_errors 忽略錯誤
錯誤處理
force_handlers #擴展
changed_when false 抑制改變的狀態為ok(獲取系統的狀態信息時)
------------------------------重要(檢查服務的配置是否正常,正常則不處理,不正常則中斷)
register: httpd_check
changed_when:
- httpd_check.stdout.find('OK')
- false
8. Ansible Valut概述
Ansible Vault作為ansible的一項新功能可將例如passwords,keys等敏感數據文件進行加密,而非存放在明文的playbooks或roles中
8.1 Ansible Valut實踐
ansible加密模塊
[[email protected] project2]# ansible-vault --help
Usage: ansible-vault [create|decrypt|edit|encrypt|encrypt_string|rekey|view] [options] [vaultfile.yml]
加密一個文件
ansible-vault encrypt include.yml
查看一個文件
[[email protected] project2]# ansible-vault view include.yml
Vault password:
- import_playbook: han.yml
- import_playbook: when2.yml
修改加密的文件內容
[[email protected] project2]# ansible-vault edit include.yml
rekey 修改密碼
[[email protected] project2]# ansible-vault rekey include.yml
Vault password:
New Vault password:
Confirm New Vault password:
Rekey successful
執行加密的playbook
echo "1" >pass
chmod 600 pass
ansible-playbook include.yml --vault-password-file=pass
9. Ansible Jinja2模板
jinja模板
2.jinja 模板邏輯關系
{
% for i in EXPR %}...{
% endfor%} 作為循環錶達式
{
% if EXPR %}...{
% elif EXPR %}...{
% endif%} 作為條件判斷
--------------------------------------nginx
[[email protected] project2]# cat jinja_nginx.yml
- hosts: webservers
vars:
- http_port: 80
- server_name: www.oldboyedu.com
tasks:
- name: Copy Nginx COnfigure
template: src=./oldboyedu.conf.j2 dest=/etc/nginx/conf.d/oldboyedu_proxy.conf
[[email protected] project2]# cat oldboyedu.conf.j2
upstream {
{
server_name }} {
{
% for i in range(1,20) %}
server 172.16.1.{
{
i}}:{
{
http_port}};
{
%endfor%}
}
server {
listen {
{
http_port }};
server_name {
{
server_name }};
location / {
proxy_pass http://{
{
server_name }};
proxy_set_header Host $http_host;
}
}
--------------------------------------keepalived
[[email protected] project2]# cat jinja_keepalived.yml
- hosts: webservers
tasks:
- name: Copy Keepalived Configure
template: src=./kee.conf.j2 dest=/tmp/keepalived.conf
[[email protected] project2]# cat kee.conf.j2
global_defs {
router_id {
{
ansible_hostname }}
}
vrrp_instance VI_1 {
{
%if ansible_hostname =="web01" %}
state MASTER
priority 150
{
%elif ansible_hostname == "web02" %}
state BACKUP
priority 100
{
%endif%}
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
使用Ansible jinja IF 生成不同的mysql配置文件 (自定義變量)
[[email protected] project2]# cat jinja_mysql.yml
- hosts: webservers
gather_facts: no
vars:
PORT: 13306
# PORT: false #相當於開關
tasks:
- name: Copy MySQL Configure
template: src=./my.cnf.j2 dest=/tmp/my.cnf
[[email protected] project2]# cat my.cnf.j2
{
% if PORT %}
bind-address=0.0.0.0:{
{
PORT }}
{
% else %}
bind-address=0.0.0.0:3306
{
%endif%}
10. Ansible Roles
tasks目錄中只能有一個main.yml
role角色
[[email protected] ~]# cd /etc/ansible/roles/
[[email protected] roles]# mkdir nfs/{tasks,handlers,templates} -pv
mkdir: created directory ‘nfs’
mkdir: created directory ‘nfs/tasks’
mkdir: created directory ‘nfs/handlers’
mkdir: created directory ‘nfs/templates’
[[email protected] roles]# cat nfs/tasks/install.yml
- name: Install NFS-utils Server
yum: name=nfs-utils state=present
[[email protected] roles]# cat nfs/tasks/config.yml
- name: Configure Nfs-utils Server
template: src=./exports.j2 dest=/etc/exports owner=root group=root mode=0644
notify: Restart NFS Server
[[email protected] roles]# cat nfs/tasks/start.yml
- name: Start NFS Server
systemd: name=nfs state=started enabled=yes
[[email protected] roles]# cat nfs/tasks/main.yml
- include_tasks: install.yml
- include_tasks: config.yml
- include_tasks: start.yml
[[email protected] roles]# cat nfs-client/tasks/main.yml
- name: Mount NFS Server
mount: path=/opt src=172.16.1.7:/data fstype=nfs opts=defaults state=mounted
[[email protected] roles]#
[[email protected] roles]#
[[email protected] roles]# cat site.yml
- hosts: web01
roles:
- nfs
- hosts: web02
roles:
- nfs-client
----------------------------安裝memcached
[[email protected] roles]# mkdir memcached/{tasks,handlers,templates} -pv
mkdir: created directory ‘memcached’
mkdir: created directory ‘memcached/tasks’
mkdir: created directory ‘memcached/handlers’
mkdir: created directory ‘memcached/templates’
11. AnsibleGalaxy
12. 自我總結
條件語句:
1. 一個條件的簡單判斷
(1)根據主機判斷(when、match)
when ansible_distribution == "Ubuntu"
根據主機判斷-->match
when (ansible_hostname is match ("web")) or (ansible_hostname is natch("lb"))
(2)根據主機名進行判斷
when ansible_fqdn =="web02"
(3)根據不同的ip地址進行判斷
when ansible_default_ipv4.address == "10.0.0.7"
(4)根據內存值來進行大小判斷
when ansible_memtotal_mb | int < "2000"
(5)判斷nginx -t執行結果 是否為0,如果result.rc == 0 則執行正確(這個需要用到注册變量)
when result.rc == 0
result.rc is match "0"
2. 多個條件的判斷
(1)列錶方式判斷 and並且關系(或者兩個關系用列錶進行錶示)
when:
- ansible_distribution == "CentOS"
- ansible_fqdn == "web01"
when: ansible_default_ipv4.address == "10.0.0.7" and ansible_fqdn == "web1"
(2)或者or 關系判斷
when:ansible_distribution == "CentOS" or ansible_fqdn == "web02"
循環語句:
1. with_item循環列錶的寫法
file:
path: "{
{ item }}"
state: touch
with_items:
- file1.txt
- file2.txt
2. 使用loop方式進行循環列錶
file:
path: "{
{ item }}"
state: touch
loop:
- file1.txt
- file2.txt
3. 字典的方式(一個循環可以寫多個條件)
copy:
src: "{
{ item.src }}"
dest: "{
{ item.dest }}"
owner: "{
{ item.owner }}"
group: "{
{ item.mode }}"
mode: "{
{ item.mode }}"
loop:
- { src: 1.txt , dest:/root/ , mode: '0600' , owner: www , group: root }
- { src: 2.txt , dest: /opt/ , mode: '0000' , owner: root , group: www}
playbook handlers
1. 需要監控的地方
notify: Reload Nginx
2. 觸發監控
handlers:
- name Reload Nginx
systemd:
name: "{
{ item }}"
state: restarted
loop:
- nginx
- mariadb
when: result.rc == 0 # 檢查語法進行判斷
边栏推荐
- KDD2022 | GraphMAE:自监督掩码图自编码器
- zgc的垃圾收集的主要阶段
- 2023届校园招聘正式开启!OceanBase 想和你在这个春天约一场面试
- Vagrantbox reinstalling the vboxsf driver
- 关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
- Turing prize winner: what should I pay attention to if I want to succeed in my academic career?
- Zip compression decompression
- SQL tuning guide notes 12:configuring options for optimizer statistics gathering
- SQL调优指南笔记6:Explaining and Displaying Execution Plans
- SQL调优指南笔记17:Importing and Exporting Optimizer Statistics
猜你喜欢
Pixel level reconstruction and restoration technology to solve severe image blur
Oracle livelabs experiment: introduction to Oracle Spatial
How do complex systems detect anomalies? North Carolina UNCC and others' latest overview of graph based deep learning anomaly detection methods in complex distributed systems describes the latest prog
Kdd2022 | graphmae: self supervised mask map self encoder
NIO使用指南
Icml2022 | galaxy: active learning of polarization map
SQL tuning guide notes 14:managing extended statistics
Ansible playbook和Ansible Roles(三)
SQL调优指南笔记15:Controlling the Use of Optimizer Statistics
关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
随机推荐
MySQL体系结构及基础管理(二)
How to design a message box through draftjs
JUC并发工具包使用指南
SQL tuning guide notes 10:optimizer statistics concepts
数据批量写入
MySQL master-slave replication
Bubble sort
SQL调优指南笔记8:Optimizer Access Paths
SQL调优指南笔记9:Joins
PE安装win10系统
建立高可用的数据库
GNS安装与配置
@loadbalance annotation of resttemplate
【QNX Hypervisor 2.2 用户手册】4.3 获取host组件
Permission to query execution plan in Oracle Database
selenium操作元素遇到的异常
【目标检测】|Dive Deeper Into Box for Object Detection 基于FCOS新训练方法
回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
Oracle LiveLabs实验:Introduction to Oracle Spatial
SQL tuning guide notes 11:histograms