1.编写loop和条件任务
2.实施handler
3.处理Errors
4.扩展-实施Tags
1.编写Loop和条件任务
1.1 loop
条件查看:ansible.cfg
a)变量定义 var.yml
user1:jack
user2:tom
b)变量调用 config_user.yml
---
- name:
hosts: servera
tasks:
- name:
user:
name: "{{ user1 }}"
state: present
- name:
user:
name: "{{ user2 }}"
state: present
ansible-playbook config_user.yml
---
- name:
hosts: servera
# vars:
# users:
# - jack
# - tom
tasks:
- name: create user {{ item }} #此处不识别
user:
name: "{{ item }}"
state: present
loop:
- jack
- tom
1.11循环-直接列表
1.12.循环-变量 vars
---
- name:
hosts: servera
vars:
users:
- jack
- tom
tasks:
- name: create user {{ item }} #此处不识别
user:
name: "{{ item }}"
state: present #添加 present 删除:absent
loop:"{{ users }}"
1.13.循环:列表+字典
---
- name:
hosts: servera
vars:
users:
- name:jack #3.列表+字典
uid:1011
- name:tom
uid:1012
tasks:
- name: create user {{ item }} #此处不识别
user:
name: "{{ item.name }}"
uid: "{{ item.uid }}"
state: present #添加 present 删除:absent
loop:"{{ users }}"
1.2条件判断
字符串==,数字< 、>、...
变量是否定义 min_memory is defined,min_memory is not defined
memory avaliable,not memory avaliable
1.准备条件:
ansiblg.cfg
[defaults]
inventory=inventory
remote_user=root
inventory
[servers]
servera
serverb
vim ~/.vimrc
set ts=2 paste
1.21when+bool、数字
---
- name:test
hosts: servera
gather_facts: no
vars:
run_my_task: true #0:false、no #1:TRUE,yes 不区分大小写
tasks:
- name: test when
debug:
msg:
when: run_my_task #此处为bool值
# when: run_my_task <15 #此处为数字
1.22when+文件的路径:vars=/etc/hosts
---
- name:test
hosts: servera
gather_facts: no
vars:
file_name: /etc/hosts
tasks:
- name: test when
debug:
msg:"{{ file_name}} is regular file"
when: file_name is file
#file:文件
#directory:目录
#link: 软链接
#mount:挂载点
#exist:路径
1.23when+结果(succeeded+failed+changed+skiped)
---
- name:test
hosts: servera
gather_facts: no
vars:
doshell:"yes"
tasks:
- shell: cat /etc/hosts
register:result
ignore_errors:true
when:doshell=="yes"
- name: success
debug:
msg: success
when: result is succeeded
when: result is failed
when: result is changed
when: result is skipped
#succeeded:成功
#failed: 失败
#changed: 改变
#skiped: 跳过
1.24 字符串大小写+奇数偶数(string +lower+upper+number+even+odd)
1.25 变量值是否在另一个变量当中
in ,not in
---
- name:test
hosts: servera
gather_facts:no
vars:
my_special_user:devops
superusers:
- root
- wheel
tasks:
- name:
user:
name:
groups:
append:yes
when: my_special_user not in superusers
1.26 多个条件
and
when: ansible kernel=="" and inventory_hostname in groups['']
when:
- ansible kernel==""
- inventory_hostname in groups['']
2.实施Handlers
2.1由其他任务通知执行的任务 inactive 任务,通过notify调用。
每次yum安装都重启。
---
- name: deploy server
hosts: servera
tasks:
- name: install packages
yum:
name: httpd
state: present
notify:
- restart apache
- name: enable httpd service
service:
name: httpd
enabled: yes
state: started
- name: install httpd-manual
yum:
name: httpd-manual
state: present
notify:
- restart apache
handlers:
- name: restart apache
service:
name: httpd
state: restarted
3.处理Errors
3.1执行失败
3.11- 忽略任务 error
yum:
name:
state:
ignore_errors:yes
---
- name: deploy server
hosts: servera
gather_facts: no
vars:
doshell:"yes"
tasks:
- shell: cat /etc/hostsads
register: result
ignore_errors: true #忽略失败,执行debug failed
when: doshell == "yes"
- name: success
debug:
msg: success
when: result is succeeded
- name: failed
debug:
msg: failed
when: result is failed
- name: changed
debug:
msg: changed
enabled: yes
when: result is changed
handlers:
- name: restart apache
service:
name: httpd
state: restarted
3.12失败后handlers是不调用的,如果要执行
force_handlers:yes
---
- name:test
hosts:servera
force_handlers: yes #即使失败也要执行
tasks:
- name: a task which always notifies its hander
command: /bin/true
notify: restart the sshd
- name:
yum:
name: notapkg
state: latest
handlers:
- name:
service:
name: sshd
state: restarted
3.2fail模块
3.21fail模块后是不执行的。
playbook 要么执行完成退出,要么执行完成退出。如果想中间退出就用fail.
---
- name:test
hosts:servera
force_handlers: yes #即使失败也要执行
gather_facts: no #不收集
tasks:
- name: a task which always notifies its hander
command: /bin/true
notify: restart the sshd
- fail: #fail后是不执行的
- debug:
msg: hello
handlers:
- name:
service:
name: sshd
state: restarted
3.22执行任务失败条件
fail与when配合使用
-fail:
when:
或者 failed_when:
--- name: test fail module
hosts: servera
tasks:
- shell: /root/adduser
register: command_result
- fail:
msg:" add user "
when: "'failed' in command_result.stdout"
--- name: test fail module
hosts: servera
tasks:
- shell: /root/adduser
register: command_result
- failed_when:"'failed' in command_result.stdout"
--- name: test fail module
hosts: servera
gather_facts: no
tasks:
- shell: /root/adduser
register: command_result
changed_when:"'success' in command_result.stdout"
- debug:
var: command_result
- fail:
msg: "add user"
when:"'failed' in command_result.stdout"
3.23指定任务变更条件
changed_when:
---
- name: test when #任务结果状态为改变
hosts:servera
gather_facts:no
tasks:
- name: test changed when
shell: cat /etc/redhat.rel
changed_when:false
3.3 block:
3.31多个任务都在when时触发。
---
- name: block
hosts: servera
tasks:
- block: #when与block对齐
-debug:
msg: task1
-debug:
msg: task2
when: ansible_distrbution=="RedHat"
3.32block -rescue(block中出错时执行)-always(block是否成功都会运行always)
4.扩展-实施Tags
4.1执行 tags标签 : ansible-playbook tags.yml --list-tags
4.2执行 tags webserver :ansible-playbook tags.yml --tags webserver
4.3跳过 tags webserver : ansible-playbook tags.yml --skip-tags webserver
---
- name: test tags
hosts: servera
gather_facts: no
tasks:
- name: httpd is installed
yum:
name: httpd
state: latest
tags: webserver
- name: postfix is installed
yum:
name: postfix
state: latest
tags: mailserver
- name: always debug
debug:
msg: debug
tags:
- always