playbook参考文档html
Playbook
与ad-hoc
相比,是一种彻底不一样的运用ansible的方式,相似与saltstack
的state
状态文件。ad-hoc
没法持久使用,playbook
能够持久使用。playbook
是由一个或多个play
组成的列表,play
的主要功能在于将事先归并为一组的主机装扮成事先经过ansible
中的task
定义好的角色。从根本上来说,所谓的task
无非是调用ansible
的一个module
。将多个play
组织在一个playbook
中,便可以让它们联合起来按事先编排的机制完成某一任务linux
playbook
使用yaml
语法格式,后缀能够是yaml
,也能够是yml
。nginx
playbook
文件中,能够连续三个连子号(---
)区分多个play
。还有选择性的连续三个点好(...
)用来表示play
的结尾,也可省略。playbook
的内容,通常都会写上描述该playbook
的功能。tab
混用。YAML
文件内容和Linux
系统大小写判断方式保持一致,是区分大小写的,k/v
的值均需大小写敏感k/v
的值可同行写也能够换行写。同行使用:分隔。v
能够是个字符串,也能够是一个列表name: task
# 建立playbook文件 [root@ansible ~]# cat playbook01.yml --- #固定格式 - hosts: 192.168.1.31 #定义须要执行主机 remote_user: root #远程用户 vars: #定义变量 http_port: 8088 #变量 tasks: #定义一个任务的开始 - name: create new file #定义任务的名称 file: name=/tmp/playtest.txt state=touch #调用模块,具体要作的事情 - name: create new user user: name=test02 system=yes shell=/sbin/nologin - name: install package yum: name=httpd - name: config httpd template: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf notify: #定义执行一个动做(action)让handlers来引用执行,与handlers配合使用 - restart apache #notify要执行的动做,这里必须与handlers中的name定义内容一致 - name: copy index.html copy: src=/var/www/html/index.html dest=/var/www/html/index.html - name: start httpd service: name=httpd state=started handlers: #处理器:更加tasks中notify定义的action触发执行相应的处理动做 - name: restart apache #要与notify定义的内容相同 service: name=httpd state=restarted #触发要执行的动做 #测试页面准备 [root@ansible ~]# echo "<h1>playbook test file</h1>" >>/var/www/html/index.html #配置文件准备 [root@ansible ~]# cat httpd.conf |grep ^Listen Listen {{ http_port }} #执行playbook, 第一次执行能够加-C选项,检查写的playbook是否ok [root@ansible ~]# ansible-playbook playbook01.yml PLAY [192.168.1.31] ********************************************************************************************* TASK [Gathering Facts] ****************************************************************************************** ok: [192.168.1.31] TASK [create new file] ****************************************************************************************** changed: [192.168.1.31] TASK [create new user] ****************************************************************************************** changed: [192.168.1.31] TASK [install package] ****************************************************************************************** changed: [192.168.1.31] TASK [config httpd] ********************************************************************************************* changed: [192.168.1.31] TASK [copy index.html] ****************************************************************************************** changed: [192.168.1.31] TASK [start httpd] ********************************************************************************************** changed: [192.168.1.31] PLAY RECAP ****************************************************************************************************** 192.168.1.31 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 # 验证上面playbook执行的结果 [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'ls /tmp/playtest.txt && id test02' 192.168.1.31 | CHANGED | rc=0 >> /tmp/playtest.txt uid=990(test02) gid=985(test02) 组=985(test02) [root@ansible ~]# curl 192.168.1.31:8088 <h1>playbook test file</h1>
经过ansible-playbook
命令运行
格式:ansible-playbook <filename.yml> ... [options]
git
[root@ansible PlayBook]# ansible-playbook -h #ansible-playbook经常使用选项: --check or -C #只检测可能会发生的改变,但不真正执行操做 --list-hosts #列出运行任务的主机 --list-tags #列出playbook文件中定义全部的tags --list-tasks #列出playbook文件中定义的因此任务集 --limit #主机列表 只针对主机列表中的某个主机或者某个组执行 -f #指定并发数,默认为5个 -t #指定tags运行,运行某一个或者多个tags。(前提playbook中有定义tags) -v #显示过程 -vv -vvv更详细
在一个playbook
开始时,最早定义的是要操做的主机和用户github
--- - hosts: 192.168.1.31 remote_user: root
除了上面的定义外,还能够在某一个tasks
中定义要执行该任务的远程用户web
tasks: - name: run df -h remote_user: test shell: name=df -h
还能够定义使用sudo
受权用户执行该任务shell
tasks: - name: run df -h sudo_user: test sudo: yes shell: name=df -h
每个task
必须有一个名称name
,这样在运行playbook
时,从其输出的任务执行信息中能够很清楚的辨别是属于哪个task
的,若是没有定义 name
,action
的值将会用做输出信息中标记特定的task
。
每个playbook
中能够包含一个或者多个tasks
任务列表,每个tasks
完成具体的一件事,(任务模块)好比建立一个用户或者安装一个软件等,在hosts
中定义的主机或者主机组都将会执行这个被定义的tasks
。apache
tasks: - name: create new file file: path=/tmp/test01.txt state=touch - name: create new user user: name=test001 state=present
不少时候当咱们某一个配置发生改变,咱们须要重启服务,(好比httpd配置文件文件发生改变了)这时候就能够用到handlers
和notify
了;
(当发生改动时)notify actions
会在playbook
的每个task结束时被触发,并且即便有多个不一样task通知改动的发生,notify actions
知会被触发一次;好比多个resources
指出由于一个配置文件被改动,因此apache
须要重启,可是从新启动的操做知会被执行一次。vim
[root@ansible ~]# cat httpd.yml #用于安装httpd并配置启动 --- - hosts: 192.168.1.31 remote_user: root tasks: - name: install httpd yum: name=httpd state=installed - name: config httpd template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: - restart httpd - name: start httpd service: name=httpd state=started handlers: - name: restart httpd service: name=httpd state=restarted #这里只要对httpd.conf配置文件做出了修改,修改后须要重启生效,在tasks中定义了restart httpd这个action,而后在handlers中引用上面tasks中定义的notify。
环境说明:这里配置了两个组,一个apache组和一个nginx组centos
[root@ansible PlayBook]# cat /etc/ansible/hosts [apache] 192.168.1.36 192.168.1.33 [nginx] 192.168.1.3[1:2]
执行playbook
时候经过参数-e
传入变量,这样传入的变量在整个playbook
中均可以被调用,属于全局变量
[root@ansible PlayBook]# cat variables.yml --- - hosts: all remote_user: root tasks: - name: install pkg yum: name={{ pkg }} #执行playbook 指定pkg [root@ansible PlayBook]# ansible-playbook -e "pkg=httpd" variables.yml
在/etc/ansible/hosts
文件中定义变量,能够针对每一个主机定义不一样的变量,也能够定义一个组的变量,而后直接在playbook
中直接调用。注意,组中定义的变量没有单个主机中的优先级高。
# 编辑hosts文件定义变量 [root@ansible PlayBook]# vim /etc/ansible/hosts [apache] 192.168.1.36 webdir=/opt/test #定义单个主机的变量 192.168.1.33 [apache:vars] #定义整个组的统一变量 webdir=/web/test [nginx] 192.168.1.3[1:2] [nginx:vars] webdir=/opt/web # 编辑playbook文件 [root@ansible PlayBook]# cat variables.yml --- - hosts: all remote_user: root tasks: - name: create webdir file: name={{ webdir }} state=directory #引用变量 # 执行playbook [root@ansible PlayBook]# ansible-playbook variables.yml
编写playbook
时,直接在里面定义变量,而后直接引用,能够定义多个变量;注意:若是在执行playbook
时,又经过-e
参数指定变量的值,那么会以-e
参数指定的为准。
# 编辑playbook [root@ansible PlayBook]# cat variables.yml --- - hosts: all remote_user: root vars: #定义变量 pkg: nginx #变量1 dir: /tmp/test1 #变量2 tasks: - name: install pkg yum: name={{ pkg }} state=installed #引用变量 - name: create new dir file: name={{ dir }} state=directory #引用变量 # 执行playbook [root@ansible PlayBook]# ansible-playbook variables.yml # 若是执行时候又从新指定了变量的值,那么会已从新指定的为准 [root@ansible PlayBook]# ansible-playbook -e "dir=/tmp/test2" variables.yml
setup
模块默认是获取主机信息的,有时候在playbook
中须要用到,因此能够直接调用。经常使用的参数参考
# 编辑playbook文件 [root@ansible PlayBook]# cat variables.yml --- - hosts: all remote_user: root tasks: - name: create file file: name={{ ansible_fqdn }}.log state=touch #引用setup中的ansible_fqdn # 执行playbook [root@ansible PlayBook]# ansible-playbook variables.yml
为了方便管理将全部的变量统一放在一个独立的变量YAML
文件中,laybook
文件直接引用文件调用变量便可。
# 定义存放变量的文件 [root@ansible PlayBook]# cat var.yml var1: vsftpd var2: httpd # 编写playbook [root@ansible PlayBook]# cat variables.yml --- - hosts: all remote_user: root vars_files: #引用变量文件 - ./var.yml #指定变量文件的path(这里能够是绝对路径,也能够是相对路径) tasks: - name: install package yum: name={{ var1 }} #引用变量 - name: create file file: name=/tmp/{{ var2 }}.log state=touch #引用变量 # 执行playbook [root@ansible PlayBook]# ansible-playbook variables.yml
一个playbook
文件中,执行时若是想执行某一个任务,那么能够给每一个任务集进行打标签,这样在执行的时候能够经过-t
选择指定标签执行,还能够经过--skip-tags
选择除了某个标签外所有执行等。
# 编辑playbook [root@ansible PlayBook]# cat httpd.yml --- - hosts: 192.168.1.31 remote_user: root tasks: - name: install httpd yum: name=httpd state=installed tags: inhttpd - name: start httpd service: name=httpd state=started tags: sthttpd - name: restart httpd service: name=httpd state=restarted tags: - rshttpd - rs_httpd # 正常执行的结果 [root@ansible PlayBook]# ansible-playbook httpd.yml PLAY [192.168.1.31] ************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.1.31] TASK [install httpd] ************************************************************************************************************************* ok: [192.168.1.31] TASK [start httpd] *************************************************************************************************************************** ok: [192.168.1.31] TASK [restart httpd] ************************************************************************************************************************* changed: [192.168.1.31] PLAY RECAP *********************************************************************************************************************************** 192.168.1.31 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1)经过-t
选项指定tags
进行执行
# 经过-t指定tags名称,多个tags用逗号隔开 [root@ansible PlayBook]# ansible-playbook -t rshttpd httpd.yml PLAY [192.168.1.31] ************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.1.31] TASK [restart httpd] ************************************************************************************************************************* changed: [192.168.1.31] PLAY RECAP *********************************************************************************************************************************** 192.168.1.31 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2)经过--skip-tags
选项排除不执行的tags
[root@ansible PlayBook]# ansible-playbook --skip-tags inhttpd httpd.yml PLAY [192.168.1.31] ************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.1.31] TASK [start httpd] *************************************************************************************************************************** ok: [192.168.1.31] TASK [restart httpd] ************************************************************************************************************************* changed: [192.168.1.31] PLAY RECAP *********************************************************************************************************************************** 192.168.1.31 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
template
模板为咱们提供了动态配置服务,使用jinja2
语言,里面支持多种条件判断、循环、逻辑运算、比较操做等。其实说白了也就是一个文件,和以前配置文件使用copy
同样,只是使用copy
,不能根据服务器配置不同进行不一样动态的配置。这样就不利于管理。
说明:
一、多数状况下都将template
文件放在和playbook
文件同级的templates
目录下(手动建立),这样playbook
文件中能够直接引用,会自动去找这个文件。若是放在别的地方,也能够经过绝对路径去指定。
二、模板文件后缀名为.j2
。
示例:经过template安装httpd
1)playbook
文件编写
[root@ansible PlayBook]# cat testtmp.yml #模板示例 --- - hosts: all remote_user: root vars: - listen_port: 88 #定义变量 tasks: - name: Install Httpd yum: name=httpd state=installed - name: Config Httpd template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #使用模板 notify: Restart Httpd - name: Start Httpd service: name=httpd state=started handlers: - name: Restart Httpd service: name=httpd state=restarted
2)模板文件准备,httpd
配置文件准备,这里配置文件端口使用了变量
[root@ansible PlayBook]# cat templates/httpd.conf.j2 |grep ^Listen Listen {{ listen_port }}
3)查看目录结构
# 目录结构 [root@ansible PlayBook]# tree . . ├── templates │ └── httpd.conf.j2 └── testtmp.yml 1 directory, 2 files
4)执行playbook
,因为192.168.1.36
那台机器是6
的系统,模板文件里面的配置文件是7
上面默认的httpd
配置文件,httpd
版本不同(6默认版本为2.2.15,7默认版本为2.4.6
),因此拷贝过去后启动报错。下面使用playbook
中的判断语句进行处理;此处先略过
[root@ansible PlayBook]# ansible-playbook testtmp.yml PLAY [all] ****************************************************************************************** TASK [Gathering Facts] ****************************************************************************** ok: [192.168.1.36] ok: [192.168.1.32] ok: [192.168.1.33] ok: [192.168.1.31] TASK [Install Httpd] ******************************************************************************** ok: [192.168.1.36] ok: [192.168.1.33] ok: [192.168.1.32] ok: [192.168.1.31] TASK [Config Httpd] ********************************************************************************* changed: [192.168.1.31] changed: [192.168.1.33] changed: [192.168.1.32] changed: [192.168.1.36] TASK [Start Httpd] ********************************************************************************** fatal: [192.168.1.36]: FAILED! => {"changed": false, "msg": "httpd: Syntax error on line 56 of /etc/httpd/conf/httpd.conf: Include directory '/etc/httpd/conf.modules.d' not found\n"} changed: [192.168.1.32] changed: [192.168.1.33] changed: [192.168.1.31] RUNNING HANDLER [Restart Httpd] ********************************************************************* changed: [192.168.1.31] changed: [192.168.1.32] changed: [192.168.1.33] PLAY RECAP ****************************************************************************************** 192.168.1.31 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.1.32 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.1.33 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.1.36 : ok=3 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
条件测试:若是须要根据变量、facts
或此前任务的执行结果来作为某task
执行与否的前提时要用到条件测试,经过when
语句执行,在task
中使用jinja2
的语法格式、
when语句:
在task
后添加when
子句便可使用条件测试;when
语句支持jinja2
表达式语法。
相似这样:
tasks: - command: /bin/false register: result ignore_errors: True - command: /bin/something when: result|failed - command: /bin/something_else when: result|success - command: /bin/still/something_else when: result|skipped
示例:经过when语句完善上面的httpd配置
1)准备两个配置文件,一个centos6
系统httpd
配置文件,一个centos7
系统httpd配置文件。
[root@ansible PlayBook]# tree templates/ templates/ ├── httpd6.conf.j2 #6系统2.2.15版本httpd配置文件 └── httpd7.conf.j2 #7系统2.4.6版本httpd配置文件 0 directories, 2 files
2)修改playbook
文件,经过setup模块获取系统版本去判断。setup经常使用模块
[root@ansible PlayBook]# cat testtmp.yml #when示例 --- - hosts: all remote_user: root vars: - listen_port: 88 tasks: - name: Install Httpd yum: name=httpd state=installed - name: Config System6 Httpd template: src=httpd6.conf.j2 dest=/etc/httpd/conf/httpd.conf when: ansible_distribution_major_version == "6" #判断系统版本,为6便执行上面的template配置6的配置文件 notify: Restart Httpd - name: Config System7 Httpd template: src=httpd7.conf.j2 dest=/etc/httpd/conf/httpd.conf when: ansible_distribution_major_version == "7" #判断系统版本,为7便执行上面的template配置7的配置文件 notify: Restart Httpd - name: Start Httpd service: name=httpd state=started handlers: - name: Restart Httpd service: name=httpd state=restarted
3)执行playbook
[root@ansible PlayBook]# ansible-playbook testtmp.yml PLAY [all] ****************************************************************************************** TASK [Gathering Facts] ****************************************************************************** ok: [192.168.1.31] ok: [192.168.1.32] ok: [192.168.1.33] ok: [192.168.1.36] TASK [Install Httpd] ******************************************************************************** ok: [192.168.1.32] ok: [192.168.1.33] ok: [192.168.1.31] ok: [192.168.1.36] TASK [Config System6 Httpd] ************************************************************************* skipping: [192.168.1.33] skipping: [192.168.1.31] skipping: [192.168.1.32] changed: [192.168.1.36] TASK [Config System7 Httpd] ************************************************************************* skipping: [192.168.1.36] changed: [192.168.1.33] changed: [192.168.1.31] changed: [192.168.1.32] TASK [Start Httpd] ********************************************************************************** ok: [192.168.1.36] ok: [192.168.1.31] ok: [192.168.1.32] ok: [192.168.1.33] RUNNING HANDLER [Restart Httpd] ********************************************************************* changed: [192.168.1.33] changed: [192.168.1.31] changed: [192.168.1.32] changed: [192.168.1.36] PLAY RECAP ****************************************************************************************** 192.168.1.31 : ok=5 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 192.168.1.32 : ok=5 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 192.168.1.33 : ok=5 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 192.168.1.36 : ok=5 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
with_items
迭代,当有须要重复性执行的任务时,可使用迭代机制。
对迭代项的引用,固定变量名为“item”
,要在task中使用with_items给定要迭代的元素列表。
列表格式:
字符串
字典
示例1:经过with_items安装多个不一样软件
编写playbook
[root@ansible PlayBook]# cat testwith.yml # 示例with_items --- - hosts: all remote_user: root tasks: - name: Install Package yum: name={{ item }} state=installed #引用item获取值 with_items: #定义with_items - httpd - vsftpd - nginx
上面tasks
写法等同于:
--- - hosts: all remote_user: root tasks: - name: Install Httpd yum: name=httpd state=installed - name: Install Vsftpd yum: name=vsftpd state=installed - name: Install Nginx yum: name=nginx state=installed
示例2:经过嵌套子变量建立用户并加入不一样的组
1)编写playbook
[root@ansible PlayBook]# cat testwith01.yml # 示例with_items嵌套子变量 --- - hosts: all remote_user: root tasks: - name: Create New Group group: name={{ item }} state=present with_items: - group1 - group2 - group3 - name: Create New User user: name={{ item.name }} group={{ item.group }} state=present with_items: - { name: 'user1', group: 'group1' } - { name: 'user2', group: 'group2' } - { name: 'user3', group: 'group3' }
2)执行playbook
并验证
# 执行playbook [root@ansible PlayBook]# ansible-playbook testwith01.yml # 验证是否成功建立用户及组 [root@ansible PlayBook]# ansible all -m shell -a 'tail -3 /etc/passwd' 192.168.1.36 | CHANGED | rc=0 >> user1:x:500:500::/home/user1:/bin/bash user2:x:501:501::/home/user2:/bin/bash user3:x:502:502::/home/user3:/bin/bash 192.168.1.32 | CHANGED | rc=0 >> user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1003:1003::/home/user3:/bin/bash 192.168.1.31 | CHANGED | rc=0 >> user1:x:1002:1003::/home/user1:/bin/bash user2:x:1003:1004::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash 192.168.1.33 | CHANGED | rc=0 >> user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1003:1003::/home/user3:/bin/bash
经过使用for
,if
能够更加灵活的生成配置文件等需求,还能够在里面根据各类条件进行判断,而后生成不一样的配置文件、或者服务器配置相关等。
示例1
1)编写playbook
[root@ansible PlayBook]# cat testfor01.yml # template for 示例 --- - hosts: all remote_user: root vars: nginx_vhost_port: - 81 - 82 - 83 tasks: - name: Templage Nginx Config template: src=nginx.conf.j2 dest=/tmp/nginx_test.conf
2)模板文件编写
# 循环playbook文件中定义的变量,依次赋值给port [root@ansible PlayBook]# cat templates/nginx.conf.j2 {% for port in nginx_vhost_port %} server{ listen: {{ port }}; server_name: localhost; } {% endfor %}
3)执行playbook
并查看生成结果
[root@ansible PlayBook]# ansible-playbook testfor01.yml # 去到一个节点看下生成的结果发现自动生成了三个虚拟主机 [root@linux ~]# cat /tmp/nginx_test.conf server{ listen: 81; server_name: localhost; } server{ listen: 82; server_name: localhost; } server{ listen: 83; server_name: localhost; }
示例2
1)编写playbook
[root@ansible PlayBook]# cat testfor02.yml # template for 示例 --- - hosts: all remote_user: root vars: nginx_vhosts: - web1: listen: 8081 server_name: "web1.example.com" root: "/var/www/nginx/web1" - web2: listen: 8082 server_name: "web2.example.com" root: "/var/www/nginx/web2" - web3: listen: 8083 server_name: "web3.example.com" root: "/var/www/nginx/web3" tasks: - name: Templage Nginx Config template: src=nginx.conf.j2 dest=/tmp/nginx_vhost.conf
2)模板文件编写
[root@ansible PlayBook]# cat templates/nginx.conf.j2 {% for vhost in nginx_vhosts %} server{ listen: {{ vhost.listen }}; server_name: {{ vhost.server_name }}; root: {{ vhost.root }}; } {% endfor %}
3)执行playbook
并查看生成结果
[root@ansible PlayBook]# ansible-playbook testfor02.yml # 去到一个节点看下生成的结果发现自动生成了三个虚拟主机 [root@linux ~]# cat /tmp/nginx_vhost.conf server{ listen: 8081; server_name: web1.example.com; root: /var/www/nginx/web1; } server{ listen: 8082; server_name: web2.example.com; root: /var/www/nginx/web2; } server{ listen: 8083; server_name: web3.example.com; root: /var/www/nginx/web3; }
示例3
在for循环中再嵌套if判断,让生成的配置文件更加灵活
1)编写playbook
[root@ansible PlayBook]# cat testfor03.yml # template for 示例 --- - hosts: all remote_user: root vars: nginx_vhosts: - web1: listen: 8081 root: "/var/www/nginx/web1" - web2: server_name: "web2.example.com" root: "/var/www/nginx/web2" - web3: listen: 8083 server_name: "web3.example.com" root: "/var/www/nginx/web3" tasks: - name: Templage Nginx Config template: src=nginx.conf.j2 dest=/tmp/nginx_vhost.conf
2)模板文件编写
# 说明:这里添加了判断,若是listen没有定义的话,默认端口使用8888,若是server_name有定义,那么生成的配置文件中才有这一项。 [root@ansible PlayBook]# cat templates/nginx.conf.j2 {% for vhost in nginx_vhosts %} server{ {% if vhost.listen is defined %} listen: {{ vhost.listen }}; {% else %} listen: 8888; {% endif %} {% if vhost.server_name is defined %} server_name: {{ vhost.server_name }}; {% endif %} root: {{ vhost.root }}; } {% endfor %}
3)执行playbook
并查看生成结果
[root@ansible PlayBook]# ansible-playbook testfor03.yml # 去到一个节点看下生成的结果发现自动生成了三个虚拟主机 [root@linux ~]# cat /tmp/nginx_vhost.conf server{ listen: 8081; root: /var/www/nginx/web1; } server{ listen: 8888; server_name: web2.example.com; root: /var/www/nginx/web2; } server{ listen: 8083; server_name: web3.example.com; root: /var/www/nginx/web3; }
上面三个示例的图片展现效果
例一
例二
例三