---
- hosts: web1
tasks:
- shell: mkdir /tmp/cache
- name: reload httpd
service:
name: httpd
state: restarted
- name: run some command
shell: /usr/bin/somecommand
ignore_error: True
tasks:
- copy:
src: httpd.conf
dest: /etc/httpd/conf/httpd.conf
- template:
src: index.html
dest: /var/www/html/index.html
owner: apache
group: apache
mode: 0644
- name: modify index.html
copy:
src: index.html
dest: /var/www/html/index.html
owner: apache
group: apache
mode: 0644
tags: update_index
handlers:
- name: reload httpd
service:
name: httpd
state: restarted
tasks:
- name: modify httpd.conf
copy:
src: httpd.conf
dest: /etc/httpd/conf/httpd.conf
tags: update_conf
notify: reload httpd
handlers:
- name: reload httpd
service:
name: httpd
state: restarted
– notify 调用的是handler段的name定义的串,必须一致,否则达不到触发的效果
– 多个task触发同一个notify的时候,同一个服务只会触发一次
– notify可以触发多个条件,在生产环境中往往涉及到某一个配置文件的改变要重启若干服务的场景,handler用到这里非常适合
– 结合vars可以写出非常普通的服务管理脚本
playbook从上往下顺序执行,若报错,后面的命令不会在执行,若想解决可以使用ignoring_errors:True(使用这个,会有报错信息,告诉你错误忽略,继续执行下面的命令)
[root@manger ansible]# vim error.yml
---
- hosts: web
remote_user: root
tasks:
- shell: mkdir /tmp/cache
- name: ReStart service httpd
service:
name: httpd
state: restarted
- name: run some command
shell: /usr/bin/somecommand
ignore_errors: True
[root@ansible ansible]# ansible-playbook error.yml
步骤二:tags给指定的任务定义一个调用标识
[root@manger ansible]# vim adhttp.yml
---
- hosts: cache
remote_user: root
tasks:
- copy:
src: /root/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: apache
group: apache
mode: 0644
tags: syncconf
[root@manger ansible]# ansible-playbook tags.yml -t syncconf
[root@manger ansible]# vim handers.yml
---
- hosts: cache
remote_user: root
tasks:
- copy:
src: /root/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: apache
group: apache
mode: 0644
tags: syncconf
notify:
- restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
[root@manger ansible]# ansible-playbook handers.yml
tashs:
- name: somecommand
command: somecommand
when: expr
---
- name: Install VIM
hosts: all
tasks:
- name: Install VIM via yum
yum: name=vim-enhanced state=installed
when: ansible_os_family == "RedHat"
- name: Install VIM via apt
apt: name=vim state=installed
when: ansible_os_family == "Debian"
- command: test command
register: result
- command: run command
when: result
---
- hosts: web
remote_user: root
tasks:
- shell: uptime | awk '{printf("%.2f",$(NF-2))}'
register: result
- service:
name: httpd
state: stopped
when: result.stdout | float >0.7
---
- hosts: web
remote_user: root
tasks:
- shell: uptime | awk '{printf("%.2f",$(NF-2))}'
register: result
- service:
name: httpd
state: stopped
when: result.stdout| float >0.7
- debug: var=result
---
- hosts: web1
remote_user: root
tasks:
- name: add users
user: group=wheel password={{'123456' |password_hash('sha512')}} name={{item}}
with_items:["a1","a2","a3","a4"]
---
- hosts: web2
remote_user:root
tasks:
- name: add users
user:group={{item.group}} password={{'123456 '| password_hash('sha512')}} name={{item.name}}
with_items:
- {name: 'a1',group: 'root'}
- {name: 'a2',group: 'root'}
- {name: 'a3',group: 'wheel'}
- {name: 'a4',group: 'wheel'}
tasks:
- include: tasks/setup.yaml
- include: tasks/user.yml user=a1 # users.yml 中可以通过{{ user }} 来使用这个变量
handlers:
- include: handler/handlers.yml
roles 像加强版的include,它可以引入一个项目的文件和目录
一般所需的目录层级有
– vars: 变量层
–tasks: 任务层
–handlers: 触发条件
–files: 文件
– template: 模板
– default: 默认,优先级最低
假如有一个play包含了一个叫"x" 的role,则
---
- hosts: host_group
roles:
- x
– x/tasks/main.yml
– x/vars/main.yml
– x/handlers/main.yml
– x/… …/main.yml
– 都会自动添加进这个play
]# ansible-playbook --syntax-check playbook.yaml
]#ansible-playbook -C playbook.yaml
–显示受到影响的主机 --list-hosts
–显示工作的task --list-tasks
–显示将要运行的tag --list-tags
1)当系统负载超过0.7时,则关掉httpd
[root@manager ansible]# vim when.yml
---
- hosts: cache
remote_user: root
tasks:
- shell: uptime | awk '{printf("%.2f\n",$(NF-2))}'
register: result
- service:
name: httpd
state: stopped
when: result.stdout|float > 0.7
[root@manager ansible]# ansible-playbook when.yml
1)with_item创建多用户
[root@manager ansible]# vim adduser.yml
---
- hosts: web2
remote_user: root
tasks:
- name: add users
user: group=wheel password={{'123456' | password_hash('sha512')}} name={{item}}
with_items: ["a1", "a2", "a3", "a4"]
[root@manager ansible]# ansible-playbook adduser.yml
2)为不同用户定义不同组
[root@manager ansible]# vim adduser1.yml
---
- hosts: web2
remote_user: root
tasks:
- name: add users
user: group={{item.group}} password={{'123456' | password_hash('sha512')}} name={{item.name}}
with_items:
- {name: 'a1', group: 'root'}
- {name: 'a2', group: 'root'}
- {name: 'a3', group: 'wheel'}
- {name: 'a4', group: 'wheel'}
[root@manager ansible]# ansible-playbook adduser1.yml
3)include and roles
在编写playbook的时候随着项目越来越大,playbook越来越复杂。可以把一些play、task 或 handler放到其他文件中,通过包含进来是一个不错的选择
roles像是加强版的include,它可以引入一个项目的文件和目录
一般所需的目录层级有
vars:变量层
tasks:任务层
handlers:触发条件
files:文件
template:模板
default:默认,优先级最低
...
tasks:
- include: tasks/setup.yml
- include: tasks/users.yml user=plj
//users.yml 中可以通过{{ user }}来使用这些变量
handlers:
- include: handlers/handlers.yml
步骤三:debug检测
[root@manager ansible]# ansible-playbook --syntax-check http.yml //检测语法
root@manager ansible]# ansible-playbook http.yml --list-tasks
//显示要执行的工作
[root@manager ansible]# vim debug.yml
---
- hosts: cache
remote_user: root
tasks:
- shell: uptime |awk '{printf("%f\n",$(NF-2))}'
register: result
- service:
name: httpd
state: stopped
when: result.stdout|float > 0.7
- name: Show debug info
debug: var=result
[root@manager ansible]# ansible-playbook debug.yml //运行