• ansible-play


    #1
    - hosts: test
      user: root
      tasks:
      - name: create nginx group
        group: name=nginx system=yes gid=208
      - name: create nginx user
        user: name=nginx system=yes uid=208 group=nginx
        
    - hosts: test
      user: root
      tasks:
      - name: copy file to test
        copy: src=/etc/inittab dest=/tmp/inittab.ans
        
        
        
    handlers 处理器  由某事件触发执行的操作
    
    #2
    - hosts: test
      user: root
      tasks:
      - name: install httpd package
        yum: name=httpd state=present
      - name: install configuation file for httpd
        copy: src=/root/conf/httpd.conf  dest=/etc/httpd/conf/httpd.conf
      - name: start httpd service
        service: enabled=true  name=httpd state=started
        
     
    #3 修改文件,才会触发重启
    - hosts: test
      user: root
      tasks:
      - name: install httpd
        yum: name=httpd state=present
      - name: install configuation file for httpd
        copy: src=/root/conf/httpd.conf  dest=/etc/httpd/conf/httpd.conf
        notify:
        - restart httpd
      - name: start httpd service
        service: enabled=true name=httpd state=started
      handlers:
      - name: restart httpd
        service: name=httpd state=restarted
        
        
    #4 playbook 使用变量
    - hosts: test
      user: root
      vars:
      - package: httpd
      - service: httpd
      
      tasks:
      - name: install httpd
        yum: name={{ package }} state=present
      - name: install configuation file for httpd
        copy: src=/root/conf/httpd.conf  dest=/etc/httpd/conf/httpd.conf
        notify:
        - restart httpd
      - name: start httpd service
        service: enabled=true name={{ service }} state=started
      handlers:
      - name: restart httpd
        service: name=httpd state=restarted
    
    
    #5  when 条件测试
    - hosts: test
      user: root
      vars:
      - user: user100
      tasks:
      - name: create {{ user }} user
        user: name={{ user }}
        when: ansible_hostname == "mongo_slave1"  
        
    
    
    - hosts: test
      user: root
      tasks:
      - name: copy file
        copy: content={{ ansible_os_family }} dest=/tmp/vars.test
        
    
    - hosts: test
      user: root
      tasks:
      - name: copy file
        copy: content='{{ ansible_os_family }},{{ httpd_port  }}' dest=/tmp/vars.test
    
    
    #迭代循环
    - hosts: test
      tasks:
      - name: "install httpd"
        yum : name={{ item }} state=present
        with_items:
        - httpd
        - httpd-devel
      - name: "chkconfig"
        service: name=httpd state=started enabled=yes
    
        
    
    - hosts: test
      tasks:
      - name: create user
        user: name={{ item }}
        with_items:
        - ysl01
        - ysl02
        - ysl03
        - ysl04
        - ysl05
        - ysl06
        
        
    #template 模版
    #[test]
    #192.168.2.100 httpd_port=100 maxClient=1000
    #192.168.2.106 httpd_port=106 maxClient=2000
     
    - hosts: test
      user: root
      tasks:
      - name: install http
        yum: name=httpd state=present
      - name: copy file
        template: src=/root/templates/httpd.conf.j2  dest=/etc/httpd/conf/httpd.conf
        notify:
        - restart httpd
      - name: restart httpd
        service: name=httpd state=started
      handlers:
      - name: restart httpd
        service: name=httpd state=restarted
        
        
    # tags 在playbook可以为某个任务定义一个标签, 在执行此playbook时,通过为ansible-playbook命令使用--tags选项能实现仅运行指定的tasks     而非所有 
        
    - hosts: test
      remote_user: root
      vars:
      - package: httpd
      - service: httpd
      tasks:
      - name: install httpd package
        yum: name={{ package }} state=latest
      - name: install configuation file for httpd
        template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
        tags:
        - conf
        notify:
        - restart httpd
      - name: start httpd service
        service: enabled=true name={{ service }} state=started
      handless:
      - name: restart httpd
        service: name=httpd start=restarted
        
        
      - name: install configuation file for httpd
        template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
        tags:
        - conf
    

      

  • 相关阅读:
    powershell网络钓鱼获取用户密码
    js 倒计时(转)
    TFS如何设置在客户端独占签出
    TFS 2010 配置的时候,提示TF255466错误
    浅谈Dynamic 关键字系列之一:dynamic 就是Object(转)
    js替换字符串中全部“-”
    苹果safari浏览器登陆时Cookie无法保存的问题
    IIS发布网站出现“未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。”的解决方法
    Aspose.Cells单元格转换为数字格式
    SQL Server中GO的使用方法(转)
  • 原文地址:https://www.cnblogs.com/augustyang/p/9238963.html
Copyright © 2020-2023  润新知