目录
基本语法
大小写
缩进
键值对
列表
字典
注释符
变量
全局变量
剧本变量
剧本中定义
通过vars_files引用
资产变量,hosts文件中的内置变量
facts变量
注册变量
变量优先级
任务控制
判断
循环
Tags属性
Handers属性
jinja2模版
基础语法
判断
循环
yaml语言是一种常用于配置文件和数据序列化的数据序列化格式。它简洁、易读,并且易于人类和计算机编写。
YAML是大小写敏感的,例如,Key
和 key
是不同的。
YAML使用缩进表示数据层次结构,通常使用两个空格进行缩进。不能使用制表符(Tab)进行缩进。
YAML中的键值对使用冒号(:)分隔,并且冒号后面需要有一个空格。
列表项使用破折号(-)开始,并且列表项之间用空行或同一个缩进级别的其他项分隔。
键值对可以嵌套,形成嵌套的字典结构。
注释符为#
文件以.yml或者.yaml结尾
文件以---开头文件以...结尾
校验与执行
文件校验语法
ansible-playbook name.yaml --syntax-check
文件使用语法
ansible-playbook name.yaml
在执行playbook时引入变量
使用语法 -e
#执行剧本时引入变量
ansible-playbook name.yaml -e "user=apple"
---
- hosts: all
name: create user
become: yes
tasks:
- name: create user
user:
name: "{{ user }}"
group: "{{ group }}"
shell: "{{ shell }}"
state: "{{ state }}"
vars:
user: apple
group: root
shell: /sbin/nologin
state: present
被引用文件vars.yaml:
#var.yaml
user:
name: apple
group: root
shell: /sbin/nologin
state: absent
使用user.yaml文件引用
---
- hosts: slave1
name: create user
become: yes
vars_files:
- vars.yaml
tasks:
- name: create user
user:
name: "{{ user.name }}"
group: "{{ user.group }}"
shell: "{{ user.shell }}"
state: "{{ user.state }}"
运行
ansible-playbook user.yaml
hosts文件中定义变量:
[db_server]
slave1 ansible_host=192.168.188.11 ansible_ssh_port=22 ansible_ssh_user=root
[mysql_server]
slave2 ansible_host=192.168.188.12 ansible_ssh_port=22 ansible_ssh_user=root
[all_server]
[all_server:children]
db_server
mysql_server
在vars.yaml中使用变量:
- name: print vars
hosts: slave2
become: yes
tasks:
- name: print vars
debug:
msg: "the host is {{ ansible_ssh_host }},the port is {{ ansible_ssh_port | default('22') }},the user is {{ ansible_ssh_user}}"
查看facts变量的语法:
#输出全部facts变量
ansible all -m setup
#过滤所需要的变量
ansible all -m setup -a "filter=*name*"
在剧本中每次默认查询facts变量,浪费剧本执行时间,可以使用以下语句在不需要使用facts变量时关闭此选项
- name: print vars
#添加此选项,关闭facts变量的收集,加快剧本执行速度
gather_facts: no
hosts: slave2
become: yes
tasks:
- name: print vars
debug:
msg: "the host is {{ ansible_ssh_host }},the port is {{ ansible_ssh_port | default('22') }},the user is {{ ansible_ssh_user}}"
使用register实现注册变量的使用,使用debug进行输出注册变量的数值,也可以使用when来进行判断,为剧本增加一定逻辑
判断用户Apple是否存在,不存在进行创建
---
- hosts: slave1
name: user
gather_facts: no
become: yes
tasks:
- name: '判断用户是否存在'
shell: id apple &> /dev/null
#将结果赋值给变量a.rc
register: a
ignore_errors: yes
- name: print result
debug:
msg: "apple用户存在"
when: a.rc == 0
- name: print result
debug:
msg: "apple用户不存在"
when: a.rc != 0
- name: create apple
user:
name: apple
group: root
shell: /sbin/nologin
when: a.rc != 0
全局变量>剧本变量>资产变量
常用条件:
条件 | 含义 |
---|---|
== | 相等 |
!= | 不相等 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
true | 结果为true |
false | 结果为false |
is defined | 已经定义 |
is not defined | 未定义 |
and | 条件都满足 |
or | 满足其中一个条件 |
判断文件file.sh是否存在,存在展示其内容,不存在则进行复制(主机上应有file.sh文件)
- name: set file
hosts: all
gather_facts: no
tasks:
- name: '判断文件是否存在'
stat: path=/root/file.sh
register: file
ignore_errors: yes
- name: '不存在文件,则进行复制'
copy:
src: /root/file.sh
dest: /root/file.sh
owner: root
mode: 755
group: root
backup: no
when: file.stat.exists == false
- name: '存在,则展示文件内容'
shell: cat file.sh
when: file.stat.exists == true
register: a
- name: '展示文件内容'
debug:
msg: "{{ a.stdout_lines }}"
使用loop对变量进行循环,然后进行操作,使得剧本编写更为简介
循环创建vars.yml中的用户
vars.yml文件
user:
- name: apple
shell: /sbin/nologin
create_home: no
group: root
- name: orange
shell: /bin/bash
create_home: yes
group: root
- name: garbage
shell: /bin/bash
create_home: yes
group: adm
user.yml文件
- name: create user
hosts: all
gather_facts: no
become: yes
vars_files: vars.yml
tasks:
- name: create user
user:
name: "{{ item.name }}"
shell: "{{ item.shell }}"
create_home: "{{ item.create_home }}"
group: "{{ item.group }}"
loop: "{{ user }}"
使用tag属性对任务进行标记,可以选择性的执行剧本中的任务
用户创建文件
- name: create user
hosts: all
gather_facts: no
become: yes
vars_files: vars.yml
tasks:
- name: create apple
user:
name: "{{ item.name }}"
shell: "{{ item.shell }}"
create_home: "{{ item.create_home }}"
group: "{{ item.group }}"
loop: "{{ user }}"
when: item.name == "apple"
tags: apple
- name: create orange
user:
name: "{{ item.name }}"
shell: "{{ item.shell }}"
create_home: "{{ item.create_home }}"
group: "{{ item.group }}"
loop: "{{ user }}"
when: item.name == "orange"
tags: orange
- name: create garbage
user:
name: "{{ item.name }}"
shell: "{{ item.shell }}"
create_home: "{{ item.create_home }}"
group: "{{ item.group }}"
loop: "{{ user }}"
when: item.name == "garbage"
tags: garbage
使用:
#创建用户apple
ansible-playbook user.yml -t apple
#创建用户orange
ansible-playbook user.yml -t orange
#创建用户garbage
ansible-playbook user.yml -t garbage
与notify搭配使用,用于执行特殊任务
- name: http site
hosts: all
gather_facts: no
become: yes
tasks:
- name: http install
yum:
name: httpd
state: latest
- name: copy conf
copy:
src: /etc/httpd/conf/httpd.conf
dest: /etc/httpd/conf/httpd.conf
tags: copy conf
notify: reload httpd
- name: http start
systemd:
name: httpd
state: started
enabled: yes
handlers:
- name: reload httpd
systemd:
name: httpd
state: reloaded
文件以.j2结尾
注释:{# #}
变量引用:{{ }}
逻辑表达:{% %}
{%if idc is defined%}
{{idc}}
{%endif%}
{%for host in groups['dbservers']%}
{host}
{%endfor%}