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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM