ansible的playbook模板
本文适用于对ansible有一定了解的人享用,列举的模板虽然没几个,但是所有模块,用这个模板套一样一样的
模板一
[root@localhost ansible]# pwd
/etc/ansible
[root@localhost ansible]# cat nginx.yml
- hosts: web
#如要使用系统变量需要打开,默认打开
gather_facts: yes
#变量定义在开头,不要放在name下
#定义的变量使用需要用{{}}调用
vars:
- hello: 123
tasks:
- name: Add repo file
yum_repository:
name: nginx
description: nginx repo
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
enabled: 1
- name: Install
yum:
name: nginx
state: latest
#添加tags,可以在执行命令时,加上--tags 选项指定执行或--skip-tags跳过执行指定的tags
tags: install
- name: Started
service:
name: nginx
state: started
enabled: 1
- name: Copy config file
copy:
src: test.conf
dest: /etc/nginx/conf.d/test.conf
tags: files
#定义notify表明当执行copy config时,检测配置文件有改动,则去执行handlers,否则不执行handlers,名字与handlers名字一致
notify: reload nginx
- name: creater user
user:
name: "{{ item.name }}"
uid: "{{ item.uid }}"
groups: "{{ item.groups }}"
state: present
with_items:
- { name: testuser1, uid: 1002, groups: "root" }
- { name: testuser2, uid: 1003, groups: "root" }
tags: create_user
#shell模块。加"|”可以执行多条命令,或者循环脚本
- name: tt
shell: |
ls
pwd
#注册变量register:使用register可以将该任务的结果定义到变量中,如此例中的shell_test
register: shell_test
tags: shell
#也可用debug中输出指定的内容(通过整体打印内容确定),如shell_test.stdout_lines
- debug:
msg: "{{ shell_test }}"
tags: shell
- debug:
msg: "{{ shell_test.stdout_lines }}"
tags: shell
#debug用于调试,将变量赋值给msg,会自动打印出来
- debug:
msg: "this: {{ item }}"
with_items:
- "{{ hello }}"
tags: test
#注册变量的引用,+前面有空格,后面没有
- name:
command: date +"%F_%T"
register: datetime
tags: touch
- name: touch file
file:
dest: /tmp/{{datetime.stdout}}
state: touch
tags: touch
#系统变量查看可通过“ansible web -m setup”模块查看
- name: 系统变量调用
file:
dest: /tmp/{{ansible_all_ipv4_addresses}}
state: touch
tags: touch
#handlers放在yml文件最后,否则在handlers下面的内容不会执行
handlers:
- name: reload nginx
service: name=nginx state=reload
模板二
Playbook的条件判断when
[root@localhost ansible]# cat choose.yml
- hosts: web
tasks:
- name: get status
debug:
msg: "{{ item }}"
with_items:
- "{{ansible_default_ipv4.address}}"
- "{{ansible_user_id}}"
#when选项,属于选择选项,当满足when定义的条件时,则执行该模块
when: ansible_default_ipv4.address == '192.168.112.23' or ansible_user_id == "root"
#写成下面格式属于and,与选项
# - ansible_user_id == "root"
# - ansible_default_ipv4.address == '192.168.112.23'
#或者以下格式也支持
#when: (条件1 and 条件2) or (条件3 and 条件4)
模板三
playbook的循环配置with_list、loop、with_items
[root@localhost ansible]# cat xunhuan.yml
- hosts: web
tasks:
- name: 循环测试(老版本)
debug:
msg: "{{ item }}"
with_list:
- 123
- qwe
- asd
tags: old
- name: 循环测试(新版本哦)
debug:
msg: "{{ item }}"
loop:
- one
- two
- three
tags: new
- name: 常用版本(可以遍历字典)
user:
name: "{{ item.name }}"
groups: "{{ item.group}}"
state: present
with_items:
- {name: 'zhangsan', group: 'testuser1'}
- {name: 'wangwu', group: 'testuser1'}
- {name: 'zhaoliu', group: 'testuser2'}
tags: use
模板四
import和include的区别
[root@localhost ansible]# cat php.yml
- hosts: web
tasks:
- name: php
debug:
msg: "php"
[root@localhost ansible]# cat mysql.yml
- hosts: web
tasks:
- name: mysql
debug:
msg: "msyql"
[root@localhost ansible]# cat study.yml
#include_tasks(动态):在运行时导入
##•--list-tags,--list-tasks不会显示到输出
##•不能使用notify触发来自include调用的文件内处理程序名称(handlers)
##
##import_playbook(静态):在Playbook解析时预先导入
##•不能与循环一起使用
##•将变量用于目标文件或角色名称时,不能使用inventory(主机/主机组等)中的变量
#
##main.yml
##需要导入几个写几个,可以给每个模块yml的每一个任务都定义一个相同的tags
##主yml开始时倒入
---
#通过import倒入的Yml文件,在源yml文件必须写hosts和tasks字段
- import_playbook: php.yml
- import_playbook: mysql.yml
#
- hosts: web
gather_facts: no
tasks:
#通过include倒入的yml文件,不用写hosts和tasks字段
- include_tasks: mysql.yml
tags: include
- include_tasks: php.yml
tags: include
- name: dd
debug: msg="xyh"
模板五
jinj2的变量调用
#这是主机清单文件
[root@localhost ansible]# cat hosts
[web]
192.168.112.22
192.168.112.23
192.168.112.24
[web:vars]
#这是给web区域主机的定义的变量
ansible_ssh_pass="xyh,.123"
http_port="9090"
server_name="test.com"}
#也可以在下面区域定义全局变量
[vars]
xxxx=xxx
#以在ansible的目录下创建group_vars目录
[root@localhost group_vars]# pwd
/etc/ansible/group_vars
#创建与主机清单中定义的区域同名文件,即为对应的变量,all文件即为全局变量文件,格式用key=value
[root@localhost group_vars]# ll
总用量 0
-rw-r--r-- 1 root root 0 3月 13 20:53 all
-rw-r--r-- 1 root root 0 3月 13 20:52 web
[root@localhost ansible]# cat nginx.j2
server {
listen {{http_port}};
server_name {{server_name}};
location / {
root /var/www/html;
index index.html;
}
}
[root@localhost ansible]# cat test.j2
#遍历一个列表
#set自定义变量,变量需要用{{}}括选才有用,jinj2语句要用{% %}括选
{% set list=['one', 'two', 'three'] %}
{% for i in list %}
{% if i == 'two' %}
-> two
{% elif i == 'three' %}
{{i}}
{% endif %}
{% endfor %}
#这是一个yml文件中定义的变量(下方yml文件中有定义)
{{hello}}
{{test}}
#遍历一个字典(老板字典遍历的属性可能时temtitems)
{% set dict={'zhangsan': 26, 'list': 25} %}
{% for key, value in dict.items() %}
这是{{key}} ,这是{{value}}
{% endfor %}
[root@localhost ansible]# cat jinjia2.yml
- hosts: web
gather_facts: yes
vars:
hello: ansible
test: xyh
tasks:
- template: src=test.j2 dest=/tmp/j2
tags: test1
- template: src=nginx.j2 dest=/etc/nginx/conf.d/jinjia.conf
tags: nginx1