8.判断语句
- 1.centos和ubuntu系统都需要安装httpd, 判断系统.
- 2.安装软件仓库,只有web组的安装webtatic其他的主机全部跳过.
- 3.TASK任务, TASK1任务执行成功,才会执行TASK2
#根据不同的系统,安装不同的服务
- hosts: webservers
tasks:
- name: CentOS Installed Httpd Server
yum:
name: httpd
state: present
when: ( ansible_distribution == "CentOS" )
- name: Ubuntu Installed Httpd Server
yum:
name: httpd2
state: present
when: ( ansible_distribution == "Ubuntu" )
[[email protected] project1]# cat f16.yml
- hosts: all
tasks:
- name: Add Nginx Yum Repository
yum_repository:
name: nginx
description: Nginx Repository
baseurl: http://nginx.org/packages/centos/7/$basearch/
when: ( ansible_hostname is match ("web*"))
[[email protected] project1]# cat f17.yml
- hosts: webservers
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
register: Check_Httpd
ignore_errors: yes
#判断Check_Httpd.rc是否等于0,如果为0则执行任务,否则不执行
- name: Restart Httpd Server
systemd:
name: httpd
state: restarted
when: ( Check_Httpd.rc == 0 )
9.循环语句
#一次启动多个服务
[[email protected] project1]# cat f18.yml
- hosts: webservers
tasks:
- name: Systemd Nginx Status
systemd:
name: "{{ item }}" #调用的变量也不变,也是固定
state: started
#固定的语法格式
with_items:
- nginx
- php-fpm
#一次拷贝多个文件
[[email protected] project1]# cat f19.yml
- hosts: webservers
tasks:
- name: Configure nginx.conf
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
mode: '{{ item.mode }}'
with_items:
- { src: ./file/nginx.conf.j2, dest: /etc/nginx/nginx.conf, mode: '0644' }
- { src: ./file/kold.oldxu.com.conf.j2, dest: /etc/nginx/conf.d/kold.oldxu.com.conf, mode: '0600' }
#创建多个用户,一次创建多个? 3个用户 TASK
[[email protected] project1]# cat f20.yml
- hosts: webservers
tasks:
- name: Create User
user:
name: "{{ item }}"
with_items:
- test1
- test2
- test3
- test4
#1.创建tt1 --> bin tt2 -->root tt3 --->adm 附加组
[[email protected] project1]# cat f20.yml
- hosts: webservers
tasks:
- name: Create User
user:
name: "{{ item.name }}"
groups: "{{ item.groups }}"
with_items:
- { name: tt1, groups: bin }
- { name: tt2, groups: root }
- { name: tt3, groups: adm }
1.标准循环 --->居多
item
with_items:
- test
2.字典循环: --->居多
itme.name
with_items:
- { name: test }
3.变量循环
- hosts: webservers
tasks:
- name: ensure a list of packages installed
yum: name={{ packages }} state=present
vars:
packages:
- httpd
- httpd-tools
10.handlers
[[email protected] project1]# cat f22.yml
- hosts: webservers
tasks:
- name: Installed Nginx and PHP Packages
yum:
name: nginx
state: present
- name: Configure nginx.conf
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#监控-->changed状态-->通知-->handlers--->name-->Restart Nginx Server
notify: Restart Nginx Server
#notify:
# - Restart Nginx Server
# - Restart php Server
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
#当nginx或php配置文件发生变更才会触发此操作
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
#3.handlers注意事项
1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。
2.只有task发生改变了才会通知handlers,没有改变则不会触发handlers.
3.不能使用handlers替代tasks、因为handlers是一个特殊的tasks。
变量->facts-->判断-->循环
- 2.安装Redis (bind 本地IP地址) facts bind {{ ansible_eth1.ipv4.address }}
- 1.安装Rsyncd服务 (循环) rsyncd.conf 644 rsync.passwd 600
- 3.安装NFS (配置文件,创建目录,客户端挂载) 变量
- 4.安装Nginx+PHP
- 1.变量
- 2.facts
- 3.register
- 4.when 判断语句 ( facts )
- 5.with_items 循环
- 1.标准循环
- 2.字典循环
- 3.变量循环(忽略)
1.tag标签(调试)
[[email protected] tasks]# cat task_nfs.yml
- hosts: webservers
tasks:
#对一个任务打多个标签
- name: Install Nfs Server
yum:
name: nfs-utils
state: present
tags:
- install_nfs
- install_nfs-server
#对一个任务打一个标签
- name: Service Nfs Server
service:
name: nfs-server
state: started
enabled: yes
tags: start_nfs-server
ansible-playbook -i ../hosts task_nfs.yml -t start_nfs-server123
ansible-playbook -i ../hosts task_nfs.yml --skip-tags install_nfs-server
2.include包含
3.错误处理 ignore_errors: yes
3.异常处理
? 1.每次状态都是changed,纵使没有修改过被控端
#Check_Redis_Status=$(netstat -lntp | grep redis)
- name: Check Redis Status
shell: netstat -lntp | grep redis
register: Check_Redis_Status
changed_when: false
#echo ${Check_Redis_Status}
- name: Debug Check_Redis Variables
debug:
msg: "Redis Status: {{ Check_Redis_Status.stdout_lines }}"
? 2.nginx推送配置文件后,没有任何的检查功能
###########################################low版
[[email protected] tasks]# cat task_nginx.yml
- hosts: webservers
tasks:
#安装nginx
- name: Installed nginx Server
yum:
name: nginx
state: present
#配置nginx
- name: Configure nginx Server
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#检查nginx (Check_Nginx_Status=$(nginx -t))
- name: Check Nginx Configure File
shell: nginx -t
register: Check_Nginx_Status
ignore_errors: yes
changed_when: false
#启动Nginx
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
when: ( Check_Nginx_Status.rc == 0 ) #只有Check_Nginx_Status.rc=0时,才会执行启动操作
##################################new版
[[email protected] tasks]# cat task_nginx.yml
- hosts: webservers
tasks:
#安装nginx
- name: Installed nginx Server
yum:
name: nginx
state: present
#配置nginx
- name: Configure nginx Server
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#检查nginx (Check_Nginx_Status=$(nginx -t))
- name: Check Nginx Configure File
shell: nginx -t
register: Check_Nginx_Status
changed_when:
- Check_Nginx_Status.stdout.find('successful')
- false
#启动Nginx
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
##################################new版
[[email protected] tasks]# cat task_nginx.yml
- hosts: webservers
tasks:
#安装nginx
- name: Installed nginx Server
yum:
name: nginx
state: present
#配置nginx
- name: Configure nginx Server
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#检查nginx (Check_Nginx_Status=$(nginx -t))
- name: Check Nginx Configure File
shell: nginx -t
#启动Nginx
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
3.强制调用handlers 触发器
[[email protected] tasks]# cat task_nginx.yml
- hosts: webservers
force_handlers: yes #无论tasks失败与否,只要通过过handlers,那me一定会执行
tasks:
#安装nginx
- name: Installed nginx Server
yum:
name: nginx
state: present
#配置nginx
- name: Configure nginx Server
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx Server
#检查nginx (Check_Nginx_Status=$(nginx -t))
- name: Check Nginx Configure File
shell: nginx -t
#启动Nginx
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
总结: Ansible Task 控制
- 1.判断语句 when
- 2.循环语句 with_items
- 3.触发器 handlers
- 4.标签 tag
- 5.忽略错误 ignore_errors
- 6.异常处理
- 1.关闭TASK的changed状态 -->让该TASK一直处理OK状态 changed_when: false
- 2.强制调用handlers: 当正常task调用过handlers,则无论后续的task成功还是失败,都会调用handlers触发器执行任务.
- 3........ Check_Nginx_Status.stdout.find(‘successful‘)
原文地址:https://www.cnblogs.com/baozexu/p/11669932.html
时间: 2024-10-11 21:43:10