zoukankan      html  css  js  c++  java
  • ansible-playbook的应用实例

    mkdir  /root/test/

    vim  /root/test/test1.yaml

    添加:

    ---
    - hosts: 39.107.103.225          //  第一个任务
    gather_facts: False                  //不读取对方主机的信息
    tags:                                          //主机标签一
    - play1
    tasks:
    - name: create user
    user: name={{ item }}
    with_items:
    - lisi
    - zhangsan
    - wangwu
    - hosts: 39.107.103.225                 //第二个任务
    tags:                                                 //第二个主机标签
    - play2
    tasks:
    - name: create directory
    file: path=/root/{{ item.a1 }} owner={{ item.a2 }} group={{ item.a3 }} mode={{ item.a4 }} state=directory
    with_items:
    - {a1: "benet",a2: "zhangsan",a3: "lisi",a4: "0700"}
    - {a1: "accp",a2: "wangwu",a3: "zhangsan",a4: "0755"}
    - {a1: "yun",a2: "lisi",a3: "root",a4: "0777"}
    - hosts: 39.107.103.225                          //第三个任务
    tags:                                                          //第三个主机标签
    - play3 
    tasks:
    - name: web stie
    file: path=/root/benet/{{ item[0] }}/{{ item[1] }} state=directory
    with_nested:
    - ['benetcom','accpcom','yuncom']
    - ['xixi','haha','lala']
    - hosts: 39.107.103.225                              //第四个任务

    tags:                                                              //第四个主机标签
    - play4
    tasks:
    - name: ipv4
    lineinfile:
    dest: /root/accp/lpc.conf
    line: "{{ item }}"
    with_items:
    - "net.ipv4.conf.all.send_redirects = 0"
    - "net.ipv4.conf.default.send_redirects = 0"
    - "net.ipv4.conf.eth0.send_redirects = 0"
    - hosts: 39.107.103.225                                  //第五个任务

    tags:                                                                  //第五个主机标签
    - play5
    tasks:
    - name: copy txt
    copy: src={{ item }} dest=/root/accp/
    with_fileglob:
    - /root/*.txt
    - hosts: 39.107.103.225                                 //第六个任务
    tags:                                                                 //第六个主机标签
    - play6
    tasks:
    - name: redhat vim
    yum: name=vim-enhanced state=installed
    when: ansible_os_family == "RedHat"
    - name: debian
    apt: name=vim state=installed
    when: ansible_os_family == "Debian"
    - hosts: 39.107.103.225                               //第七个任务
    tags:                                                                //第七个主机标签
    - play7
    tasks:
    - name: panduan
    command: echo {{ item }}
    with_items: [ 0,2,4,6,8,10 ]
    when: item < 5

     

     执行这个命令:

    ansible-playbook  /root/test/test1.yaml

     

     

     

  • 相关阅读:
    Constants and Variables
    随想
    C#基础篇之语言和框架介绍
    Python基础19 实例方法 类方法 静态方法 私有变量 私有方法 属性
    Python基础18 实例变量 类变量 构造方法
    Python基础17 嵌套函数 函数类型和Lambda表达式 三大基础函数 filter() map() reduce()
    Python基础16 函数返回值 作用区域 生成器
    Python基础11 List插入,删除,替换和其他常用方法 insert() remove() pop() reverse() copy() clear() index() count()
    Python基础15 函数的定义 使用关键字参数调用 参数默认值 可变参数
    Python基础14 字典的创建修改访问和遍历 popitem() keys() values() items()
  • 原文地址:https://www.cnblogs.com/Leonardo-li/p/8589645.html
Copyright © 2011-2022 走看看