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

 

 

 

转载于:https://www.cnblogs.com/Leonardo-li/p/8589645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值