剧本扩展功能
①在剧本中设置变量信息
②在剧本中设置注册信息 执行剧本时,可以显示输出命令结果信息
③在剧本中设置判断信息
④在剧本中设置循环信息
⑤在剧本中设置错误忽略
⑥在剧本中设置标签信息
⑦在剧本中设置触发信息
⑧在剧本中进行剧本整合
准备yaml原始文件
[root@m01 /etc/ansible/ansible-playbook]$ cat rsync_server.yaml
- hosts: 172.16.1.41
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-push conf file
copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
- name: 03-create user
#user: name=rsync create_home=no shell=/sbin/nologin
shell: useradd rsync -M -s /sbin/nologin
- name: 04-create backup dir
file: path=/backup state=directory owner=rsync group=rsync
- name: 05-create password file
copy: content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600
- name: 06-start rsync server_file/rsync_server/rsyncd
service: name=rsyncd state=started enabled=yes
- hosts: 172.16.1.31,172.16.1.7
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-create password file
copy: content=oldboy123 dest=/etc/rsync.password mode=600
- name: 03-create test file
file: dest=/tmp/test.txt state=touch
- name: 04-check test
shell: rsync -avz /tmp/test.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
①在剧本中设置变量信息
第一种变量:直接在剧本中配置(键值对的形式),在需要引用的地方引用,
配置格式
vars:
backupdir: /backup
passfile: rsync.password
引用格式
path={{ backupdir }}
第二种变量:在命令行中进行配置
第三种变量:在主机清单文件编写
三种方式的优先级: 若同时设置了三种变量,优先级如何?
最优先: 命令行变量设置
次优先: 剧本中变量设置
最后: 主机清单变量设置
②在剧本中设置注册信息
- hosts: oldboy
tasks:
- name: check server port
shell: netstat -lntup -->查询端口信息
register: get_server_port #查询结果会返回,存到注册的参数get_server_port中
借助debug模块,显示注册的参数信息
- name: display port info
debug: msg={{ get_server_port.stdout_lines }}
完整代码:
- hosts: rsync_server
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
#- name: 02-push conf file
# copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
- name: 03-create user
user: name=rsync createhome=no shell=/sbin/nologin
- name: 04-create backup dir
file: path=/backup state=directory owner=rsync group=rsync
- name: 05-create password file
copy: content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600
- name: 06-start rsync server
service: name=rsyncd state=started enabled=yes
- name: 07-check server port info
shell: netstat -lntup | grep 873 -->查询端口信息
register: get_server_port -->注册变量
- name: display port info
debug: msg={{ get_server_port.stdout_lines }} -->输出注册变量的值
③在剧本中设置判断信息
设置判断条件:
(ansible_hostname == "nfs") -->当主机名为nfs执行命令
- hosts: oldboy
remote_user: root
tasks:
- name: Check File
file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")
- name: install httpd
yum: name=httpd state=installed
when: (系统情况 == "CentOS")
- name: install httpd2
yum: name=httpd2 state=installed
when: (系统情况 == "ubuntu")
判断需要借助setup模块
setup模块可用于显示被管理主机系统的详细信息
查看主机清单中的 相关主机以及主机组的详细信息
ansible rsync_server -m setup
setup 只过滤子项信息(子项过滤仅在剧本中使用,才有效果)
比如只过滤关于eth0的ipv4信息
ansible_eth0[ipv4]
获取内置变量的方法
ansible web -m setup -a "filter=ansible_hostname"
[root@m01 /etc/ansible]$ ansible web -m setup -a "filter=ansible_hostname"
172.16.1.7 | SUCCESS => {
"ansible_facts": {
"ansible_hostname": "web01",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
[root@m01 /etc/ansible]$
判断剧本编写
(ansible_hostname == "nfs01") -->当主机名为nfs01执行该模块
(ansible_hostname == "web01") -->当主机名为web01执行该模块
执行判断剧本
④在剧本中设置循环信息(对于有多个步骤,执行同一模块的情况)
循环格式
剧本代码
- hosts: rsync_server
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-push conf file & password file
#copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/ mode=644
copy: src=/etc/ansible/server_file/rsync_server/{{ item.src }} dest={{ item.dest }} mode={{ item.mode }} #循环变量的设置
with_items: #循环变量的声明
- { src: 'rsyncd.conf',dest: '/etc/',mode: '644' } #第一次循环使用的参数
- { src: 'rsync.password',dest: '/etc/',mode: '600' } #第二次循环使用的参数
- name: 03-create user
user: name=rsync createhome=no shell=/sbin/nologin
- name: 04-create backup dir
file: path=/backup state=directory owner=rsync group=rsync
- name: 05-start rsync server
service: name=rsyncd state=started enabled=yes
ansible可识别的两种编写格式
所以有2种循环体的格式
⑤在剧本中设置错误忽略(ignore_errors: yes)
默认playbook会检查命令和模块的返回状态,如遇到错误就中断playbook的执行
加入ignore_errors: yes可以忽略错误,让剩下的步骤继续执行
模拟错误编写,ignore忽略错误,让剩下的步骤继续执行
⑥在剧本中设置标签信息(tags:xxx)
标签功能
在剧本中设置标签,然后只执行这一个标签的步骤,或者跳过指定的标签
指定执行标签: ansible-playbook --tags=t2 test.yaml
指定跳过标签: ansible-playbook --skip-tags=t2 test.yaml
- hosts: oldboy
ignore_errors: yes
remote_user: root
tasks:
- name: Check File
file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup")
tags: t1
- name: bad thing
command: ech 123
#ignore_errors: yes
tags: t2
- name: install httpd
yum: name=httpd state=installed
when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
tags: t3
- name: install httpd2
yum: name=httpd2 state=installed
when: (ansible_distribution == "ubuntu")
tags: t4
⑦在剧本中设置触发信息
触发通知notify
notify: restart rsync server
接收通知 handlers
- hosts: rsync_server
tasks:
- name: 01-Install rsync
yum: name=rsync state=installed
- name: 02-push config file
copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
notify: restart rsync server #设定notify通知
- name: 03-create user
shell: useradd rsync -M -s /sbin/nologin
- name: 04-create backup dir
file: path=/backup state=directory owner=rsync group=rsync
- name: 05-create password file
copy: content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600
- name: 06-start rsync server
service: name=rsyncd state=started enabled=yes
handlers:
- name: restart rsync server
service: name=rsyncd state=restarted
⑧在剧本中进行剧本整合
创立一个新的剧本,用于执行多个剧本
方式一:include_tasks: f1.yml
- hosts: all
remote_user: root
tasks: - include_tasks: f1.yml
- include_tasks: f2.yml
方式二:include: f1.yml
- include:f1.yml
- include:f2.yml
方式三( 常用的):- import_playbook:
[root@m01 ansible-playbook]# cat main.yml
- import_playbook: base.yml
- import_playbook: rsync.yml
- import_playbook: nfs.yml
- import_playbook: oxxx.yml
- import_playbook: rsync.yml
- import_playbook: nfs.yml