playbook yaml语法: 1.文件后缀一般为 .yml 或者 .yaml 2.文件开头一般为 — 3.注释使用 # 4.缩进代表层级结构,一般为俩个空格 5.数据结构: 列表 : - 短横线后跟一个空格 例如: - name 字典: 键值对 例如: name: tom :后跟一个空格
一、创建剧本config_nginx.yml。完成以下功能:
1、安装nginx
2、提供默认主页
3、启动并开机自启服务。
nginx的默认路径为 /usr/share/nginx/html
vim config_nginx.yum
---
- host: dev
tasks:
- name: install nginx
yum:
name: nginx
- name: html
copy:
content: 'This is test page.\n'
dest: /usr/share/nginx/html
- name: config service
service:
name: nginx
state: started
enable: yes
- name: firewalld
firewalld:
service: nginx
permanent: yes
immediate: yes
state: enabled
运行剧本:ansible-playbook 文件名 ansible-playbook 文件名 --syntax-check 检测语法 ansible-playbook 文件名 -C空运行
二、创建一个名为packages.yml的剧本
1、在dev, prod 和 test 主机组中安装 php 和 mariadb 软件包
2、在dev 主机组中安装 Development Tools 包组
3、升级dev主机组中主机的所有软件包
方法一: 编写为3个play
---
- name: play 1
hosts: dev,prod,test
tasks:
- name: install php and mariadb
yum:
name:
- php
- mariadb
- name: play 2
hosts: dev
tasks:
- name: install Development Tools
yum:
name: "@Development Tools"
- name: play 3
hosts: dev
tasks:
- name: updage all
yum:
name: '*'
state: latest
方法2:编写2个play
---
- name: play 1
hosts: dev,prod,test
tasks:
- name: install php and mariadb
yum:
name:
- php
- mariadb
- name: play 2
hosts: dev
tasks:
- name: install Development Tools
yum:
name: "@Development Tools"
- name: updage all
yum:
name: '*'
state: latest
|