• ansible playbook使用


    - hosts: home  #对应/etc/ansible/hosts的组或者ip
      remote_user: root  #执行用户
      tasks: #任务
      - name: install httpd  #任务名
        yum: name=httpd state=present
      - name: install net-tools
        yum: name=net-tools state=present
      - name: install tree
        yum: name=tree state=present
      - name: restart httpd
        service: name=httpd state=restarted

    notify和handlers的使用

    - hosts: home
      remote_user: root
      tasks:
      - name: install httpd
        yum: name=httpd state=present
        notify: #执行完install httpd后,再触发下一个任务事件
           - restart httpd #任务事件名
      - name: install net-tools
        yum: name=net-tools state=present
      - name: install tree
        yum: name=tree state=present
      handlers: 被触发的事件,对应上面notify
      - name: restart httpd
        service: name=httpd state=restarted
        tags: rehttpd

    tags使用

    - hosts: home
      remote_user: root
      tasks:
      - name: install httpd
        yum: name=httpd state=present
        notify:
           - restart httpd
      - name: install net-tools
        yum: name=net-tools state=present
      - name: install tree
        yum: name=tree state=present
      handlers:
      - name: restart httpd
        service: name=httpd state=restarted
        tags: rehttpd #标记这个任务,名字可以自定义
    ansible-playbook --tags=rehttpd cs.yml #只执行文件里面rehttpd的任务
    ansible-playbook --skip-tags=rehttpd cs.yml #排除rehttpd任务,执行其他任务

     使用sudo执行任务

    tasks: 
      - name: run df -h
        sudo_user: test  #账号
        sudo: yes  #使用sudo执行
        shell: name=df -h

    when使用

    - hosts: gs
      remote_user: root
      tasks:
      - name: install httpd
        yum: name=httpd state=installed
        notify:
           - restart httpd
        when:
          - ansible_distribution == "Centos" #满足条件系统为centos
          - ansible_distribution_major_version == "6" #满足条件系统为centos6,两个条件都满足才能执行这个任务
      - name: install net-tools
        yum: name=net-tools state=installed
      - name: install tree
        yum: name=tree state=installed
      handlers:
      - name: restart httpd
        service: name=httpd state=restarted
        tags: rehttpd
    - hosts: gs
      remote_user: root
      tasks:
      - name: install httpd
        yum: name=httpd state=installed
        notify:
           - restart httpd
        when: (ansible_distribution ==  "CentOS" and ansible_distribution_major_version == "7") or (ansible_distribution ==  "Centos" and ansible_distribution_major_version == "6") #满足系统是centos7或者centos6就可以执行
      - name: install net-tools
        yum: name=net-tools state=installed
      - name: install tree
        yum: name=tree state=installed
      handlers:
      - name: restart httpd
        service: name=httpd state=restarted
        tags: rehttpd

    整个任务里面有出现重复调用同个模板,可以使用with_items让用同个模板的任务在一个任务里面完成

    - name: "Add groups"  #创建用户组
      group: name={{ item.name }} state=present
      with_items:
        - { name: "zhang" }  #创建用户组zhang
        - { name: "zhao" }   #创建用户组zhao
      tags:
      - Add_group
    - name: "Add Users" #创建账号
      user: name={{ item.name }} state=present groups={{ item.groups }}
      with_items:
        - { name: "hu", groups: "zhang" }
        - { name: "zhi", groups: "zhao" }
      tags:
      - Add_users
    - name: "userdel user" #删除账号
      user: name={{ item.name }} state=absent  remove=yes
      with_items:
        - { name: "hu" }
        - { name: "zhi" }
        - { name: "zhangsan" }
        - { name: "lisi" }
        - { name: "zhaosi" }
        - { name: "zhangsi" }
        - { name: "test1" }
      tags:
      - Del_Users
  • 相关阅读:
    html5 canvas 小例子 旋转的时钟
    用深度学习(CNN RNN Attention)解决大规模文本分类问题
    生成式对抗网络GAN 的研究进展与展望
    linux 系统信息查看
    cmd 更改字体
    查看sbt版本
    机器学习算法汇总
    spark 大数据 LR测试
    spark
    hadoop生态圈介绍
  • 原文地址:https://www.cnblogs.com/yunweiweb/p/12990031.html
Copyright © 2020-2023  润新知