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

 

 

 

posted @ 2018-03-17 14:34  Leonardo-li  阅读(1377)  评论(0编辑  收藏  举报