目录
2.建立userlist.yml中的用户,并且使用户密码加密,
循环迭代任务
- name: test play
hosts: westos
tasks:
- debug:
msg: "{{item}}"
loop:
- westos
- linux
- lee
- name: test play
hosts: westos
tasks:
- debug:
msg: "{{item.name}} {{item.age}}"
loop:
- { name: westos, age: 18 }
- { name: linux, age: 19 }
- { name: lee, age: 20 }
2 条件判断
= | value == "字符串",value == 数字 |
< | value < 数字 |
> | value > 数字 |
<= | value <= 数字 |
>= | value >= 数字 |
!= | value != 数字 |
is defined | value is defined变量存在 |
is not defined | value is not defined 变量不存在 |
in | value is in value 变量为 |
not in | value is not in value 变量不为 |
bool变量 为true | value的值为true |
bool变量 false | value的值为false |
when:
条件1 and 条件2
- 条件1
- 条件2
when:
条件1 or 条件2
when: >
条件1
or
条件2
notify: 触发器当遇到更改是触发handlers
handlers:触发器触发后执行的动作
实例:安装Apache,更改端口为8080,如果更改,并重启Apache,触发触发器;如果端口号是8080未被修改,则不需要重启,触发器不被触发
- name: apache server
hosts: westos
vars:
port: 8080
tasks:
- name: install httpd
dnf:
name: httpd
state: present
- name: set config
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: ^Listen
line: "Listen {{port}}"
notify: restart apache
- name: enable filewalld
firewalld:
port: "{{port}}/tcp"
permanent: yes
immediate: yes
state: enabled
- name: state apache
service:
name: httpd
state: started
enabled: yes
handlers:
- name: restart apache
service:
name: httpd
state: restarted
作用:
当play遇到任务失败时会终止,ignore_errors: yes将会忽略任务失败使下面的任务继续运行
实例:执行install software play失败后会执行show some message play模块
- name: test play
hosts: westos
tasks:
- name: inatall software
dnf:
name: http(错误)
state: present
ignore_errors: yes
- name: show some message
debug:
msg: hello westos
作用:当任务失败后play被终止,也会调用触发器进程
- name: test play
hosts: westos
force_handlers: yes
tasks:
- name: test
shell: ls /mnt/
- name: install software
dnf:
name: http(错误)
state: present
- name: show some massages
debug:
msg: hello westos
handlers:
- name: notify messages
debug:
msg: hello notify
作用: 控制任务在何时报告它已进行更改,无论有没有更改,都视为更改
- name: test play
hosts: westos
tasks:
- name: install software
dnf:
name: http(错误)
state: present
changed_when: yes
notify: notify message
- name: show some message
debug:
msg: hello westos
handlers:
- name: notify message
debug:
msg: hello notify
当符合条件时强制任务失败 (无论是不是失败的,都强制失败)
- name: test play
hosts: westos
tasks:
- name: install software
dnf:
name: http(错误)
state: present
failed_when: yes
notify: notify message
- name: show some message
debug:
msg: hello westos
handlers:
- name: notify message
debug:
msg: hello notify
block: 定义要运行的任务
rescue: 定义当block句子中出现失败任务后运行的任务
always: 定义最终独立运行的任务
- name: test play
hosts: westos
force_handlers: yes
tasks:
- name: test block
block:
- name: install software
dnf:
name: http(错误)
state: present
rescue:
- name: show error
debug:
msg: http is not exist
always:
- name: show message
debug:
msg: over
1.建立playbook ~/westos.yml
要求如下: 建立大小为1500M名为/dev/vdb1的设备 如果/dev/vdb不存在请输入: /dev/vdb is not exist 如果/dev/vdb大小不足2G请输出: /dev/vdb is less then 2G 并建立800M大小的/dev/vdb1 此设备挂载到/westos上
- name: test
hosts: westos
tasks:
- name: /dev/sdb is not exist
debug:
msg: /dev/sdb is not exist
when:
ansible_facts['devices']['sdb'] is not defined
- name: create_storage
block:
- name: create 1500M
parted:
device: /dev/sdb
number: 1
state: present
part_end: 1500MiB
when:
ansible_facts['devices']['sdb'] is defined
rescue:
- name: create 800M
parted:
device: /dev/sdb
number: 1
state: present
part_end: 800MiB
when:
ansible_facts['devices']['sdb'] is defined
- name: device messages
debug:
msg: /dev/sdb is less 2G
when:
ansible_facts['devices']['sdb'] is defined
always:
- name: create filesystem
filesystem:
fstype: xfs
dev: /dev/sdb1
- name:
file:
path: /westos
state: directory
- name: mount devices
mount:
path: /westos
src: /dev/sdb1
fstype: xfs
state: mounted
when:
ansible_facts['devices']['sdb'] is defined
vim userlist.yml
vim useradd.yml