第 5.4 章 Playbook基本使用-任务控制(tags)与Playbook调试
Playbook基本使用-任务控制(tags)与Playbook调试
任务控制(tags)
作用:通过给每个任务添加标签的形式来控制那些任务被执行,哪些则被跳过。
示例如下:
- hosts: web
vars:
hello: Ansible
tasks:
- name: Add repo
yum_repository:
name: nginx
description: nginx repo
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
enabled: 1
tags: addrepo#添加的标签需要与任务同级
- name: Install nginx
yum:
name: nginx
state: latest
tags: #添加多个标签时,需要写成序列形式
- install
- installnginx
- name: Copy nginx configuration file
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf
tags: config
- name: Start nginx
service:
name: nginx
state: started
- name: Create wwwroot directory
file:
dest: /var/www/html
state: directory
- name: Create test page index.html
shell: echo "hello {{hello}}" > /var/www/html/index.html
tags使用:
只运行某一个或者多个模块:
# ansible-playbook nginx.yml --tags "addrepo"
# ansible-playbook nginx.yml --tags "install,config"
跳过某一个或者多个任务任务,执行剩余任务:
# ansible-playbook nginx.yml --skip-tags "addrepo"
# ansible-playbook nginx.yml --skip-tags "addrepo,install,config"
列出文件中定义的所有tags:
[root@CentOS7 ~]# ansible-playbook nginx-demo/nginx.yml --list-tags
playbook: nginx-demo/nginx.yml play #1 (web): webTAGS: [] TASK TAGS: [addrepo, config, install, test]
列出文件中定义的所有tasks:
[root@CentOS7 ~]# ansible-playbook nginx-demo/nginx.yml --list-tasks
playbook: nginx-demo/nginx.yml play #1 (web): webTAGS: [] tasks: Add repoTAGS: [addrepo] Install nginxTAGS: [install] Copy nginx configuration fileTAGS: [config] Start nginxTAGS: [] Create wwwroot directoryTAGS: [] Create test page index.htmlTAGS: [test]
Playbook调试
语法检查:
ansible-playbook main.yml --syntax-check
main.yml为你需要调试的文件
打印语句:
---
- hosts: webservers tasks:
- debug:
msg: {{group_names}}
- debug:
msg: {{inventory_hostname}}
- debug:
msg: {{ansible_hostname}}
文章目录
关闭
