playbook俗称剧本,yml文件,类似于shell脚本,一次编写,永久使用,方便快捷。
编写脚本,帮助文档是最重要的工具,学会使用帮助文档事半功倍。
帮助文档
1.ansible-doc 模块名 查看某个模块的帮助文档,里边有例子,各种参数,参数的使用。
ansible-doc -l 查看所有的模块
例如:ansible-doc service
模块
user模块
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
user:
name: johnd #添加的用户名
comment: John Doe
uid: 1040 #所属ID
group: admin # 所属组
groups: admins,developers #所属组
state: absent #用户状态 present存在(默认值) absent 不存在
shell: /bin/bash #用户默认shell
parted模块
- name: create another primary partition
parted:
device: /dev/sdb #卷的名称
number: 1 #编号
state: present #创建
part_start: 1GiB #块大小开始
part_end: 1GiB #块大小结束
例子:手动添加虚拟磁盘
#创建物理卷pv 卷组vg 逻辑卷lv
---
- hosts: test1
tasks:
- name: create another primary partition
parted:
device: /dev/sdb
number: 1
state: present
part_end: 1GiB #注意:一定要写GiB,不能简写,不能简写!!!parted_end 参数因为版本会不一样。
- name: create another primary partition
parted:
device: /dev/sdb
number: 2
state: present
part_start: 1GiB
part_end: 3GiB
- name: create a vg
lvg:
vg: my_vg
pvs: /dev/sdb1
- name: create a lv
lvol:
vg: my_vg
lv: my_lv
size: 512m
yum模块
例子:安装软件更新安装包
- name: 安装软件
yum:
name:
- httpd #安装软件的名字
- mariadb
- name: 安装系统包
yum:
name: "@Development tools"
- name: 更新安装包
yum:
name:'*'
state:latest #更新为最新的安装包对应的name: '*",此为固定格式。安装状态
set模块----获取主机信息
父子关系的参数用 . 连接
debug模块 ---显示变量的值,辅助排错
例子如下:
- hosts: test1
tasks:
- debug:
#var定义常量
var: ansible_all_ipv4_addresses
- debug:
#msg 定义变量,变量必须用{{}}扩住
msg: "主机名称是:{{ ansible_hostname }}"
- debug:
var: ansible_devices.sda.partitions.sda1.size
- debug:
msg: "总内存大小:{{ ansible_memtotal_mb }}"