ansible-任务控制tags

摘要:
2-yum:present5loop:templates/src.j213dest:/etc/foo.conf14tags:nginxepo13baseurl:no15enabled:/ansible/nginx/conf/site.conf29dest:nginx33状态:Createwwwrootdirectory35file:

1. ansible-任务控制tags介绍
        如果你有一个大型的剧本,那么只能运行它的特定部分而不是在剧本中运行所有内容可能会很有用。因此,Ansible支持“tags:”属性。
        执行playbook时,可以通过两种方式根据 “tags” 过滤任务

    • 在命令行上,使用或选项“--tags或 --skip-tags ”
    • 在ansible配置设置中,使用和选项“TAGS_RUN 或 TAGS_SKIP”

  标签可以应用于Ansible中的许多结构(参见下面的“标签继承”),但最简单的用途是单独的任务。以下是使用不同标记标记两个任务的示例:

 1 tasks:
 2 - yum:
 3     name: "{{ item }}"
 4     state: present
 5   loop:
 6   - httpd
 7   - memcached
 8   tags:
 9   - packages
10 
11 - template:
12     src: templates/src.j2
13     dest: /etc/foo.conf
14   tags:
15   - configuration

   如果您只想运行一个非常长的剧本的“配置”和“包”部分,您可以在命令行上使用该选项:--tags

1   ansible-playbook example.yml --tags "configuration,packages"

   另一方面,如果要在没有某些标记任务的情况下运行playbook ,可以使用命令行选项:--skip-tags

1   ansible-playbook example.yml --skip-tags "packages"

2. ansible-任务控制实例
  1)编写一个nginx的tags控制版本

 1 [root@test-1 bin]# vim  /ansible/nginx/bin/nginx_tags.yaml 
 2 [root@test-1 bin]# cat  /ansible/nginx/bin/nginx_tags.yaml 
 3 - hosts: web1
 4   remote_user: root
 5   vars:
 6     hello: Ansible
 7  
 8   tasks:
 9   - name: Add repo 
10     yum_repository:
11       name: nginx
12       description: nginx repo
13       baseurl: http://nginx.org/packages/centos/7/$basearch/
14       gpgcheck: no
15       enabled: 1
16     tags: 
17     - yum install
18 
19   - name: Install nginx
20     yum:
21       name: nginx
22       state: latest
23     tags:
24     - yum nginx install
25 
26   - name: Copy nginx configuration file
27     copy:
28       src: /ansible/nginx/conf/site.conf
29       dest: /etc/nginx/conf.d/site.conf
30   - name: Start nginx
31     service:
32       name: nginx
33       state: started
34   - name: Create wwwroot directory
35     file:
36       dest: /var/www/html
37       state: directory
38   - name: Create test page index.html
39     shell: echo "hello {{hello}}" > /var/www/html/index.html

  2) 检查配置文件是否正确

1 [root@test-1 bin]# ansible-playbook --syntax-check nginx_tags.yaml 
2 
3 
4 playbook: nginx_tags.yaml

  3) 执行文件
      包括tags 标签的执行的

 1 [root@test-1 bin]# ansible-playbook nginx_tags.yaml  --tags "yum nginx install"
 2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
 3 
 4 
 5 PLAY [web1] ************************************************************************************************************************************
 6 
 7 TASK [Gathering Facts] *************************************************************************************************************************
 8 ok: [192.168.200.133]
 9 ok: [192.168.200.132]
10 
11 TASK [Install nginx] ***************************************************************************************************************************
12 ok: [192.168.200.133]
13 ok: [192.168.200.132]
14 
15 PLAY RECAP *************************************************************************************************************************************
16 192.168.200.132            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
17 192.168.200.133            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

      不包括tags内的以外的执行

 1 [root@test-1 bin]# ansible-playbook nginx_tags.yaml  --tags "yum install,yum nginx install"
 2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
 3 
 4 
 5 PLAY [web1] ************************************************************************************************************************************
 6 
 7 TASK [Gathering Facts] *************************************************************************************************************************
 8 ok: [192.168.200.133]
 9 ok: [192.168.200.132]
10 
11 TASK [Add repo] ********************************************************************************************************************************
12 ok: [192.168.200.133]
13 ok: [192.168.200.132]
14 
15 TASK [Install nginx] ***************************************************************************************************************************
16 ok: [192.168.200.133]
17 ok: [192.168.200.132]
18 
19 PLAY RECAP *************************************************************************************************************************************
20 192.168.200.132            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
21 192.168.200.133            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

免责声明:文章转载自《ansible-任务控制tags》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇几种常见的排序算法分析Winform中对自定义xml配置文件进行Xml节点的添加与删除下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

几种定时任务(Timer、TimerTask、ScheduledFuture)的退出—结合真实案例【JAVA并发】

工作中常常会有定时任务的开发需求,特别是移动端。最近笔者正好有所涉及,鉴于此,结合开发中的案例说明一下几种定时任务的退出。 需求说明:定时更新正在生成的文件大小和状态【进行中、失败、完成】,如果文件生成完成,则退出【CoderBaby】 调度可以用Timer 【调用schedule()或者scheduleAtFixedRate()方法实现】或者Sched...

在ie和chrome浏览器中滚动条样式的设置

  1、IE下设置滚动条样式的属性 scrollbar-arrow-color: color; /*三角箭头的颜色*/scrollbar-face-color: color; /*立体滚动条的颜色(包括箭头部分的背景色)*/scrollbar-3dlight-color: color; /*立体滚动条亮边的颜色*/scrollbar-highlight-...

vis.js 关系图 插件 配置项

vis.js 关系图 插件 配置项 关系图的配置 var options = { nodes: { borderWidth: 1, borderWidthSelected: 1, brokenImage: undefined, chose...

使用pageHelper遇到的问题

在做SSM整合的时候,遇到一个小问题,在我使用pageHelper的时候,分页的效果总是无法正确显示,卡了我几个小时,现在来说一下我的问题。  1.首先导入pageHelper的包: <!--引入pageHelper分页插件 --> <dependency> <groupId>com.github.pagehelper&...

HTML5--》点击显示隐藏内容

<details>浏览器支持比较差,可以用JavaScript实现这种功能。 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>js点击显示隐藏内容</title> 6 &...

MySQL学习随笔--通过实例理解merge ,temptable算法的差异

实例1 使用视图的两种算法merge和temptable分别统计 表tb_phone中market_price大于4000的手机,然后查询视图查找出小于6000的手机 简单总结最终获取的结果:查询出market_price大于4000且小于6000的手机 表数据: merge合并算法 合并的执行方式,每当执行的时候,先将视图的sql语句与外...