playbook 任务标签
1. 执行wp22.yml
[root@m01 project]# cat wp22.yml
- hosts: nginx
tasks:
- name: Tar Nginx package
copy:
src: /project/package/nginx-1.16.1-1.el7.ngx.x86_64.rpm
dest: /tmp
- name: Install Nginx Server
yum:
name: /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm
state: present
- name: Config Nginx Server
copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/
notify: restart_nginx
- name: Start Nginx Server
systemd:
name: nginx
state: started
- name: Create code
file:
path: /code
state: directory
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Push Wordpress
unarchive:
src: /project/package/wordpress-5.0.3-zh_CN.tar.gz
dest: /code/
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Config Wordpress DB
copy:
src: /project/conf/wp-config.php
dest: /code/wordpress/
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Chown Code Dir
file:
path: /code
state: directory
owner: www
group: www
recurse: yes
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Config wordpress Nginx
copy:
src: /project/conf/linux.wp.com.conf
dest: /etc/nginx/conf.d/
- name: restart_nginx
systemd:
name: nginx
state: restarted
notify: reload_nginx
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Restart Nginx
systemd:
name: nginx
state: restarted
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
handlers:
- name: restart_nginx
systemd:
name: nginx
state: restarted
- name: reload_nginx
systemd:
name: nginx
state: restarted
2.测试跑起来
[root@m01 project]# ansible-playbook wp22.yml
PLAY [nginx] *************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************
ok: [lb01]
ok: [lb02]
ok: [web01]
ok: [web02]
TASK [Tar Nginx package] *************************************************************************************************************************
ok: [web01]
ok: [lb02]
ok: [web02]
ok: [lb01]
TASK [Install Nginx Server] **********************************************************************************************************************
ok: [web02]
ok: [lb02]
ok: [lb01]
ok: [web01]
TASK [Config Nginx Server] ***********************************************************************************************************************
ok: [web02]
ok: [lb01]
ok: [lb02]
ok: [web01]
TASK [Start Nginx Server] ************************************************************************************************************************
ok: [lb02]
ok: [web02]
ok: [web01]
ok: [lb01]
TASK [Create code] *******************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
ok: [web01]
ok: [web02]
TASK [Push Wordpress] ****************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
changed: [web02]
changed: [web01]
TASK [Config Wordpress DB] ***********************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
ok: [web02]
ok: [web01]
TASK [Chown Code Dir] ****************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
changed: [web01]
changed: [web02]
TASK [Config wordpress Nginx] ********************************************************************************************************************
ok: [web01]
ok: [web02]
changed: [lb02]
changed: [lb01]
TASK [restart_nginx] *****************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
changed: [web01]
changed: [web02]
TASK [Restart Nginx] *****************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
changed: [web01]
changed: [web02]
RUNNING HANDLER [reload_nginx] *******************************************************************************************************************
changed: [web01]
changed: [web02]
PLAY RECAP ***************************************************************************************************************************************
lb01 : ok=6 changed=1 unreachable=0 failed=0 skipped=6 rescued=0 ignored=0
lb02 : ok=6 changed=1 unreachable=0 failed=0 skipped=6 rescued=0 ignored=0
web01 : ok=13 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=13 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3.修改配置文件测试
[root@m01 project]# vim conf/linux.wp.com.conf
server {
listen 8080;
server_name linux.wp.com;
root /code/wordpress;
index index.php;
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4 继续测试跑起来
[root@m01 project]# ansible-playbook wp22.yml
PLAY [nginx] *************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************
ok: [web02]
ok: [web01]
ok: [lb01]
ok: [lb02]
TASK [Tar Nginx package] *************************************************************************************************************************
ok: [lb02]
ok: [web01]
ok: [lb01]
ok: [web02]
TASK [Install Nginx Server] **********************************************************************************************************************
ok: [lb01]
ok: [lb02]
ok: [web01]
ok: [web02]
TASK [Config Nginx Server] ***********************************************************************************************************************
ok: [web02]
ok: [lb01]
ok: [lb02]
ok: [web01]
TASK [Start Nginx Server] ************************************************************************************************************************
ok: [lb01]
ok: [lb02]
ok: [web01]
ok: [web02]
TASK [Create code] *******************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
ok: [web01]
ok: [web02]
TASK [Push Wordpress] ****************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
changed: [web02]
changed: [web01]
TASK [Config Wordpress DB] ***********************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
ok: [web01]
ok: [web02]
TASK [Chown Code Dir] ****************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
changed: [web01]
changed: [web02]
TASK [Config wordpress Nginx] ********************************************************************************************************************
changed: [lb01]
changed: [lb02]
changed: [web01]
changed: [web02]
TASK [restart_nginx] *****************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
changed: [web02]
changed: [web01]
TASK [Restart Nginx] *****************************************************************************************************************************
skipping: [lb01]
skipping: [lb02]
changed: [web01]
changed: [web02]
RUNNING HANDLER [reload_nginx] *******************************************************************************************************************
changed: [web02]
changed: [web01]
PLAY RECAP ***************************************************************************************************************************************
lb01 : ok=6 changed=1 unreachable=0 failed=0 skipped=6 rescued=0 ignored=0
lb02 : ok=6 changed=1 unreachable=0 failed=0 skipped=6 rescued=0 ignored=0
web01 : ok=13 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=13 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
因为我只修改了linux.wp.com.conf的配置文件,重新执行。发现有reload_nginx重启履历。但是实际上我们只需要执行修改的部分即可。但是因为我们不会,所以只能从头到尾执行。这样显得效率不高。
这时候引入打标签的名词
引入 标签
[root@m01 project]# cat wp22.yml
- hosts: nginx
tasks:
- name: Tar Nginx package
copy:
src: /project/package/nginx-1.16.1-1.el7.ngx.x86_64.rpm
dest: /tmp
- name: Install Nginx Server
yum:
name: /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm
state: present
- name: Config Nginx Server
copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/
notify: restart_nginx
tags:
- reconf_nginx
- reconfig_nginx
- name: Start Nginx Server
systemd:
name: nginx
state: started
- name: Create code
file:
path: /code
state: directory
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Push Wordpress
unarchive:
src: /project/package/wordpress-5.0.3-zh_CN.tar.gz
dest: /code/
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Config Wordpress DB
copy:
src: /project/conf/wp-config.php
dest: /code/wordpress/
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Chown Code Dir
file:
path: /code
state: directory
owner: www
group: www
recurse: yes
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
- name: Config wordpress Nginx
copy:
src: /project/conf/linux.wp.com.conf
dest: /etc/nginx/conf.d/
notify: reload_nginx
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
tags: reconf_nginx
- name: Restart Nginx
systemd:
name: nginx
state: started
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
handlers:
- name: restart_nginx
systemd:
name: nginx
state: restarted
- name: reload_nginx
systemd:
name: nginx
state: restarted
#### 修改nginx配置文件,进行测试
[root@m01 project]# vim /etc/nginx/nginx.conf
[root@m01 project]# ansible-playbook wp22.yml -t reconfig_nginx
PLAY [nginx] *************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************
ok: [lb01]
ok: [web02]
ok: [web01]
ok: [lb02]
TASK [Config Nginx Server] ***********************************************************************************************************************
changed: [lb01]
changed: [lb02]
changed: [web02]
changed: [web01]
RUNNING HANDLER [restart_nginx] ******************************************************************************************************************
changed: [lb02]
changed: [lb01]
changed: [web02]
changed: [web01]
PLAY RECAP ***************************************************************************************************************************************
lb01 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
lb02 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web01 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
明显速度快,简洁明了。只需要执行自己想要的把部分。
七、playbook 任务标签
1.标签的作用
默认情况下,Ansible在执行一个playbook时,会执行playbook中定义的所有任务,Ansible的标签(tag)功能可以给单独任务甚至整个playbook打上标签,然后利用这些标签来指定要运行playbook中的个别任务,或不执行指定的任务。
2.打标签的方式
对一个task打一个标签
对一个task打多个标签
对多个task打一个标签
3.对一个task打一个标签
- hosts: nginx
- name: Config Nginx Server
copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/
notify: restart_nginx
tags: reconf_nginx
4.对多个task打一个标签
- hosts: nginx
- name: Config Nginx Server
copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/
notify: restart_nginx
tags: reconf_nginx
- name: Config Nginx wordpress
copy:
src: /project/conf/linux.wp.com.conf
dest: /etc/nginx/conf.d/
notify: reload_nginx
when: (ansible_fqdn == "web01") or (ansible_fqdn == "web02")
tags: reconf_nginx
5.对一个task打多个标签
- hosts: nginx
- name: Config Nginx Server
copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/
notify: restart_nginx
tags:
- reconf_nginx
- reconfig_nginx
6.标签使用方式
1)查看标签
[root@m01 project]# ansible-playbook wp.yml --list-tags
playbook: wp.yml
play #1 (nginx): nginx TAGS: []
TASK TAGS: [reconf_nginx, reconfig_nginx]
2)执行指定标签的内容
[root@m01 project]# ansible-playbook wp.yml -t reconfig_nginx
3)执行多个标签代表的内容
[root@m01 project]# ansible-playbook wp.yml -t reconf_nginx,reconfig_nginx
4)不执行指定标签的内容
[root@m01 project]# ansible-playbook wp.yml --skip-tags reconfig_nginx
八、playbook 复用
在之前写playbook的过程中,我们发现,写多个playbook没有办法,一键执行,这样我们还要单个playbook挨个去执行,很鸡肋。所以在playbook中有一个功能,叫做include用来动态调用task任务列表。
1.playbook复用的配置
1)编辑两个剧本
[root@m01 project]# cat play1.yml
- name: Install Nginx Server
yum:
name: nginx
state: present
[root@m01 project]# cat play2.yml
- name: Config Nginx Server
copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/
2)编写复用剧本的文件
[root@m01 project]# vim main.yml
- hosts: web_group
tasks:
- include_tasks: /project/play1.yml
- include_tasks: /project/play2.yml
2.直接复用palybook文件
[root@m01 project]# cat main.yml
- import_playbook: ./base.yml
- import_playbook: ./nginx.yml
- import_playbook: ./php.yml
- import_playbook: ./wordpress.yml
- import_playbook: ./mariadb.yml