ansible基础-playbook剧本的使用
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.YAML概述
1>.YAML的诞生
YAML是一个可读性高,用来表达数据序列的格式。
YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822。
Clark Evans在2001年5月在首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者。
2>.YAML的命名
YAML是"YAML Ain't a Markup Language"(YAML不是一种置标语言)的递归缩写。
在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种置标语言)。但为了强调这种语言以数据作为中心,而不是以标记语言为重点,而反向缩略语重命名。
3>.YAML的功能
YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表,标量等资料形态、。
它使用空白符号缩排和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种设定档、倾印除错内容、文件大纲(例如:许多电子邮件标题格式和YAML非常接近)。
尽管它比较适合用来表达阶层式(hierarchical model)的数据结构,不过也有精致的语法可以表示关联性(relational model)的资料。
由于YAML使用空白字符和分行来分隔资料,使得它他特别适合用grep、Python、Perl、Ruby操作。
其让人最容易上手的特色是巧妙避开各种封闭符号,如:引号、各种括号等,这些符号在嵌套结构中会变得复杂而难以辨认。
4>.JSON格式和YAML格式表现数据结构(基本数据结构)案例对比
JSON格式:
{id:1,name:尹正杰,age:18,spouse:{id:2,name:jason,age:26},fav:[red,yellow,blue]}
YAML格式:
id:1
name:尹正杰
age:18
spouse:
id:2
name:jason
age:26
fav:
-red
-yellow
-blue
5>.YAML语法检查
其实ansible自带的就有YAML语法检查的脚本,当然你也可以参考网页连接的检查:https://www.bejson.com/validators/yaml/。
二.Playbook的核心元素
Hosts:
定义可以操作的主机,
Tasks:
定义我们需要执行的任务列表。
Variable:
支持变量,流程控制语句,如循环语句等等。(YAML支持编程语言的基本语法,如条件表达式,循环语句,流程控制等等,这也是为什么ansible选择它的一个原因吧!)
Templates:
包含了模板语法的文本文件。
Handlers:
处理器是由特定条件触发的任务。
Roles:
定义角色,其实我们可以理解角色是将服务器主机进行分组,然后基于组的方式进行批量管理。这个角色和MySQL8.0之后版本的角色功能很相似!
三.运行playbook的方式案例展示
1>.playbook的基础组件
playbook的基础组件: Hosts: 运行指定任务的目标主机。 remoute_user: 在远程主机上执行任务的用户。 tasks: 任务列表。 模块,模块参数 格式: action:module arguments module:arguments 温馨提示: shell和command模块后面直接跟命令,而非key=value类的参数列表。
某任务的状态在运行后为changed时,可通过“notify”通知给相应的handlers。
任务可以通过“tags”打标签,而后可在ansible-playbook命令上使用-t指定进行调用。
2>.运行playbook的步骤简介
运行playbook的方式: 1>.测试 ansible-playbook --check 只检测可能会发生的改变,但不是真正执行操作。 ansible-playbook --list-hosts 列出运行任务的主机。 ansible-playbook --list-tasks 列出运行的任务列表。 ansible-playbook --syntax-check 语法检查。 2>.运行
3>.编写剧本(注意,每个冒号后面都有一个空格,如果你没写的话,可能会报错!)
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat memcached.yaml - hosts: node102.yinzhengjie.org.cn remote_user: root tasks: - name: 'install memcached package' yum: 'name=memcached state=installed' - name: 'start memcached service' service: 'name=memcached enabled=true state=started' [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
4>.只检测可能会发生的改变,但不是真正执行操作。
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check memcached.yaml PLAY [node102.yinzhengjie.org.cn] *************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node102.yinzhengjie.org.cn] TASK [install memcached package] **************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] TASK [start memcached service] ****************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node102.yinzhengjie.org.cn : ok=3 changed=2 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
5>.列出运行任务的主机。
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --list-hosts memcached.yaml playbook: memcached.yaml play #1 (node102.yinzhengjie.org.cn): node102.yinzhengjie.org.cn TAGS: [] pattern: [u'node102.yinzhengjie.org.cn'] hosts (1): node102.yinzhengjie.org.cn [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
6>.语法检查。如果没有任何的报错就是最好的结果!
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --syntax-check memcached.yaml playbook: memcached.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
7>.执行playbook
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook memcached.yaml PLAY [node102.yinzhengjie.org.cn] *************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node102.yinzhengjie.org.cn] TASK [install memcached package] **************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] TASK [start memcached service] ****************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node102.yinzhengjie.org.cn : ok=3 changed=2 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'ss -tnl | grep 11211' node101.yinzhengjie.org.cn | FAILED | rc=1 >> non-zero return code node103.yinzhengjie.org.cn | FAILED | rc=1 >> non-zero return code node102.yinzhengjie.org.cn | SUCCESS | rc=0 >> LISTEN 0 128 *:11211 *:* LISTEN 0 128 :::11211 :::* [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
四.handlers案例展示
1>.handlers用法简介
handlers:
任务,在特定的条件下触发;
接收到其他的任务的通知时被触发:
notify: HANDLER TASK NAME
2>.编写playbook
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat conf/memcached PORT="11235" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="" [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat memcached-v2.yaml - hosts: all remote_user: root tasks: - name: 'install memcached package' yum: 'name=memcached state=installed' - name: 'install conf file' copy: 'src=/root/ansible_workshop/conf/memcached dest=/etc/sysconfig/memcached' notify: 'restart memcached service' - name: 'start memcached service' service: 'name=memcached enabled=true state=started' handlers: - name: 'restart memcached service' service: 'name=memcached state=restarted' [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
3>.语法检查
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --syntax-check memcached-v2.yaml playbook: memcached-v2.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
4>.检查剧本的执行(注意,若文件没有被改变时不会发送通知的!)
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check memcached-v2.yaml PLAY [all] ************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [install memcached package] **************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] TASK [install conf file] ************************************************************************************************************************************ changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] TASK [start memcached service] ****************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] RUNNING HANDLER [restart memcached service] ***************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=5 changed=3 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=5 changed=3 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=5 changed=3 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
5>.运行剧本
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook memcached-v2.yaml PLAY [all] ************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [install memcached package] **************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [install conf file] ************************************************************************************************************************************ changed: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] TASK [start memcached service] ****************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node101.yinzhengjie.org.cn] RUNNING HANDLER [restart memcached service] ***************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=5 changed=3 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=5 changed=3 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=5 changed=3 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
6>.验证剧本的执行结果
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'ss -tnl | grep 11235' node101.yinzhengjie.org.cn | SUCCESS | rc=0 >> LISTEN 0 128 *:11235 *:* LISTEN 0 128 :::11235 :::* node102.yinzhengjie.org.cn | SUCCESS | rc=0 >> LISTEN 0 128 *:11235 *:* LISTEN 0 128 :::11235 :::* node103.yinzhengjie.org.cn | SUCCESS | rc=0 >> LISTEN 0 128 *:11235 *:* LISTEN 0 128 :::11235 :::* [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
五.variables案例展示
1>.variables简介
variables: 1>.facts: 可直接调用 注意,可使用setup模块直接获取目标主机的facters。
2>.用户自定义变量: 在ansible-playbook命令的命令行中 -e VARS,--extra-vars=VARS 在playbook中定义变量的方法: vars: -var1:values1 -var2:values2 变量引用的方法和python的django框架模板语法类似,如:{{ variable }}
3>.通过roles传递变量
4>.Host Inventory 用户自定义变量 向不同的主机传递不同的变量
IP/HOSTNAME varaiable=value var2=value2
向组中的主机传递相同的变量
[groupname:vars]
variable=value
invertory参数
用于定义ansible远程目标连接主机时使用的参数,而非传递给playbook的变量,一般我们在/etc/ansible/hosts 对应的主机后面定义即可
ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass
ansible_sudo_pass
2>.用户自定义变量案例实操
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ll total 12 drwxr-xr-x. 2 root root 22 Mar 8 18:14 conf -rw-r--r--. 1 root root 189 Mar 8 18:43 install_package.yaml -rw-r--r--. 1 root root 476 Mar 8 18:13 memcached-v2.yaml -rw-r--r--. 1 root root 244 Mar 8 17:20 memcached.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat install_package.yaml - hosts: node102.yinzhengjie.org.cn remote_user: root vars: - package_name: 'tree' tasks: - name: 'install something package' yum: name={{ package_name }} state=installed [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --syntax-check install_package.yaml playbook: install_package.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check install_package.yaml PLAY [node102.yinzhengjie.org.cn] *************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node102.yinzhengjie.org.cn] TASK [install something package] **************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node102.yinzhengjie.org.cn : ok=2 changed=1 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook install_package.yaml PLAY [node102.yinzhengjie.org.cn] *************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node102.yinzhengjie.org.cn] TASK [install something package] **************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node102.yinzhengjie.org.cn : ok=2 changed=1 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ll total 12 drwxr-xr-x. 2 root root 22 Mar 8 18:14 conf -rw-r--r--. 1 root root 189 Mar 8 18:43 install_package.yaml -rw-r--r--. 1 root root 476 Mar 8 18:13 memcached-v2.yaml -rw-r--r--. 1 root root 244 Mar 8 17:20 memcached.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cp install_package.yaml install_package-v2.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# vi install_package-v2.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat install_package-v2.yaml - hosts: node102.yinzhengjie.org.cn remote_user: root vars: - package_name: 'httpd' tasks: - name: install something {{ package_name }} yum: name={{ package_name }} state=installed - name: start {{ package_name }} service: name={{ package_name }} state=started [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check -e package_name='mysql' install_package-v2.yaml PLAY [node102.yinzhengjie.org.cn] *************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node102.yinzhengjie.org.cn] TASK [install something mysql] ****************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] TASK [start mysql] ****************************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node102.yinzhengjie.org.cn : ok=3 changed=2 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
3>.Host Inventory
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat /etc/ansible/hosts [web] node[101:102].yinzhengjie.org.cn [web:vars] package_name='nginx' [db] node[102:103].yinzhengjie.org.cn [tomcat] node[101:103].yinzhengjie.org.cn [redis] node101.yinzhengjie.org.cn [redis:vars] package_name='redis' [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ll total 16 drwxr-xr-x. 2 root root 22 Mar 8 18:14 conf -rw-r--r--. 1 root root 287 Mar 8 18:55 install_package-v2.yaml -rw-r--r--. 1 root root 189 Mar 8 18:43 install_package.yaml -rw-r--r--. 1 root root 476 Mar 8 18:13 memcached-v2.yaml -rw-r--r--. 1 root root 244 Mar 8 17:20 memcached.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cp install_package-v2.yaml install_package-v3.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# vi install_package-v3.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat install_package-v3.yaml - hosts: web remote_user: root vars: - package_name: 'httpd' tasks: - name: install something {{ package_name }} yum: name={{ package_name }} state=installed - name: start {{ package_name }} service: name={{ package_name }} state=started [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check install_package-v3.yaml PLAY [web] ************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] TASK [install something httpd] ****************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] TASK [start httpd] ****************************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=3 changed=1 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=3 changed=0 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
六.模板模块
1>.定义配置文件用模板将其引用
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat jinja2.j2 hostname: {{ ansible_fqdn }} myvar: {{ package_name }} [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat jinja2.yaml - hosts: all remote_user: root tasks: - name: 'install file' template: src=/root/ansible_workshop/jinja2.j2 dest=/tmp/test.conf [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook -e package_name='someackage' --check jinja2.yaml PLAY [all] ************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [install file] ***************************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=2 changed=1 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=2 changed=1 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=2 changed=1 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook -e package_name='someackage' jinja2.yaml PLAY [all] ************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [install file] ***************************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=2 changed=1 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=2 changed=1 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=2 changed=1 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ll /tmp/test.conf -rw-r--r--. 1 root root 55 Mar 8 19:56 /tmp/test.conf [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat /tmp/test.conf hostname: node101.yinzhengjie.org.cn myvar: someackage [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'cat /tmp/test.conf' node101.yinzhengjie.org.cn | SUCCESS | rc=0 >> hostname: node101.yinzhengjie.org.cn myvar: someackage node102.yinzhengjie.org.cn | SUCCESS | rc=0 >> hostname: node102.yinzhengjie.org.cn myvar: someackage node103.yinzhengjie.org.cn | SUCCESS | rc=0 >> hostname: node103.yinzhengjie.org.cn myvar: someackage [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
2>.安装Tomcat服务并配置相应的配置文件
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# yum -y install tomcat tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp java-1.8.0-openjdk-devel ......... libXi.x86_64 0:1.7.9-1.el7 libXinerama.x86_64 0:1.1.3-2.1.el7 libXrandr.x86_64 0:1.5.1-2.el7 libXrender.x86_64 0:0.9.10-1.el7 libXtst.x86_64 0:1.2.3-1.el7 libXxf86vm.x86_64 0:1.1.4-1.el7 libfontenc.x86_64 0:1.1.3-3.el7 libglvnd.x86_64 1:1.0.1-0.8.git5baa1e5.el7 libglvnd-egl.x86_64 1:1.0.1-0.8.git5baa1e5.el7 libglvnd-glx.x86_64 1:1.0.1-0.8.git5baa1e5.el7 libjpeg-turbo.x86_64 0:1.2.90-6.el7 libpng.x86_64 2:1.5.13-7.el7_2 libthai.x86_64 0:0.1.14-9.el7 libtiff.x86_64 0:4.0.3-27.el7_3 libwayland-client.x86_64 0:1.15.0-1.el7 libwayland-server.x86_64 0:1.15.0-1.el7 libxcb.x86_64 0:1.13-1.el7 libxshmfence.x86_64 0:1.2-1.el7 libxslt.x86_64 0:1.1.28-5.el7 lksctp-tools.x86_64 0:1.0.17-2.el7 log4j.noarch 0:1.2.17-16.el7_4 mesa-libEGL.x86_64 0:18.0.5-4.el7_6 mesa-libGL.x86_64 0:18.0.5-4.el7_6 mesa-libgbm.x86_64 0:18.0.5-4.el7_6 mesa-libglapi.x86_64 0:18.0.5-4.el7_6 nss-pem.x86_64 0:1.0.3-5.el7 pango.x86_64 0:1.42.4-1.el7 pcsc-lite-libs.x86_64 0:1.8.8-8.el7 pixman.x86_64 0:0.34.0-1.el7 python-javapackages.noarch 0:3.4.1-11.el7 python-lxml.x86_64 0:3.2.1-4.el7 tomcat-el-2.2-api.noarch 0:7.0.76-8.el7_5 tomcat-jsp-2.2-api.noarch 0:7.0.76-8.el7_5 tomcat-lib.noarch 0:7.0.76-8.el7_5 tomcat-servlet-3.0-api.noarch 0:7.0.76-8.el7_5 ttmkfdir.x86_64 0:3.0.9-42.el7 tzdata-java.noarch 0:2018i-1.el7 xalan-j2.noarch 0:2.7.1-23.el7 xerces-j2.noarch 0:2.11.0-17.el7_0 xml-commons-apis.noarch 0:1.4.01-16.el7 xml-commons-resolver.noarch 0:1.2-15.el7 xorg-x11-font-utils.x86_64 1:7.5-21.el7 xorg-x11-fonts-Type1.noarch 0:7.5-9.el7 Dependency Updated: chkconfig.x86_64 0:1.7.4-1.el7 freetype.x86_64 0:2.8-12.el7_6.1 glib2.x86_64 0:2.56.1-2.el7 libdrm.x86_64 0:2.4.91-3.el7 nspr.x86_64 0:4.19.0-1.el7_5 nss.x86_64 0:3.36.0-7.1.el7_6 nss-softokn.x86_64 0:3.36.0-5.el7_5 nss-softokn-freebl.x86_64 0:3.36.0-5.el7_5 nss-sysinit.x86_64 0:3.36.0-7.1.el7_6 nss-tools.x86_64 0:3.36.0-7.1.el7_6 nss-util.x86_64 0:3.36.0-1.1.el7_6 Complete! [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cp /etc/tomcat/server.xml ./server.xml.j2 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# vi server.xml.j2 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# grep "jvmRoute=" server.xml.j2 <Engine name="Catalina" defaultHost="localhost" jvmRoute="{{ ansible_hostname }}"> [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cp /etc/tomcat/tomcat-users.xml ./ [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# vi tomcat-users.xml ...... <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="yinzhengjie" roles="manager-gui,admin-gui"/> ...... [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat /etc/ansible/hosts [web] node[101:102].yinzhengjie.org.cn [tomcat] node[101:103].yinzhengjie.org.cn [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --syntax-check tomcat.yaml playbook: tomcat.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check tomcat.yaml PLAY [tomcat] *********************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [install tomcat-admin-webapps] ************************************************************************************************************************* ok: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] TASK [install tomcat-users.xml] ***************************************************************************************************************************** changed: [node103.yinzhengjie.org.cn] changed: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] TASK [install server.xml] *********************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] TASK [start tomcat] ***************************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] RUNNING HANDLER [restart tomcat] **************************************************************************************************************************** changed: [node103.yinzhengjie.org.cn] changed: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=6 changed=4 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=6 changed=5 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=6 changed=5 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat tomcat-users.xml <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <tomcat-users> <!-- NOTE: By default, no user is included in the "manager-gui" role required to operate the "/manager/html" web application. If you wish to use this app, you must define such a user - the username and password are arbitrary. It is strongly recommended that you do NOT use one of the users in the commented out section below since they are intended for use with the examples web application. --> <!-- NOTE: The sample user and role entries below are intended for use with the examples web application. They are wrapped in a comment and thus are ignored when reading this file. If you wish to configure these users for use with the examples web application, do not forget to remove the <!.. ..> that surrounds them. You will also need to set the passwords to something appropriate. --> <!-- <role rolename="tomcat"/> <role rolename="role1"/> <user username="tomcat" password="<must-be-changed>" roles="tomcat"/> <user username="both" password="<must-be-changed>" roles="tomcat,role1"/> <user username="role1" password="<must-be-changed>" roles="role1"/> --> <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="yinzhengjie" roles="manager-gui,admin-gui"/> <!-- <role rolename="admin"/> --> <!-- <role rolename="admin-gui"/> --> <!-- <role rolename="admin-script"/> --> <!-- <role rolename="manager"/> --> <!-- <role rolename="manager-gui"/> --> <!-- <role rolename="manager-script"/> --> <!-- <role rolename="manager-jmx"/> --> <!-- <role rolename="manager-status"/> --> <!-- <user name="admin" password="adminadmin" roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status" /> --> </tomcat-users> [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat tomcat.yaml - hosts: tomcat remote_user: root tasks: - name: install tomcat-admin-webapps yum: name=tomcat-admin-webapps state=installed - name: install tomcat-users.xml copy: src=/root/ansible_workshop/tomcat-users.xml dest=/etc/tomcat/tomcat-users.xml owner=root group=tomcat mode=0640 notify: restart tomcat - name: install server.xml template: src=/root/ansible_workshop/server.xml.j2 dest=/etc/tomcat/server.xml owner=root group=tomcat notify: restart tomcat - name: start tomcat service: name=tomcat state=started handlers: - name: restart tomcat service: name=tomcat state=restarted [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat tomcat.yaml - hosts: tomcat remote_user: root tasks: - name: install tomcat-admin-webapps yum: name=tomcat-admin-webapps state=installed - name: install tomcat-users.xml copy: src=/root/ansible_workshop/tomcat-users.xml dest=/etc/tomcat/tomcat-users.xml owner=root group=tomcat mode=0640 notify: restart tomcat - name: install server.xml template: src=/root/ansible_workshop/server.xml.j2 dest=/etc/tomcat/server.xml owner=root group=tomcat notify: restart tomcat - name: start tomcat service: name=tomcat state=started handlers: - name: restart tomcat service: name=tomcat state=restarted [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook tomcat.yaml PLAY [tomcat] *********************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] TASK [install tomcat-admin-webapps] ************************************************************************************************************************* ok: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] TASK [install tomcat-users.xml] ***************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] TASK [install server.xml] *********************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] TASK [start tomcat] ***************************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node101.yinzhengjie.org.cn] RUNNING HANDLER [restart tomcat] **************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=6 changed=4 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=6 changed=5 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=6 changed=5 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible tomcat -m shell -a 'cat /etc/tomcat/server.xml | grep jvmRoute=' node102.yinzhengjie.org.cn | SUCCESS | rc=0 >> <Engine name="Catalina" defaultHost="localhost" jvmRoute="node102"> node101.yinzhengjie.org.cn | SUCCESS | rc=0 >> <Engine name="Catalina" defaultHost="localhost" jvmRoute="node101"> node103.yinzhengjie.org.cn | SUCCESS | rc=0 >> <Engine name="Catalina" defaultHost="localhost" jvmRoute="node103"> [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
3>.tags案例
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat tomcat-users.xml <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <tomcat-users> <!-- NOTE: By default, no user is included in the "manager-gui" role required to operate the "/manager/html" web application. If you wish to use this app, you must define such a user - the username and password are arbitrary. It is strongly recommended that you do NOT use one of the users in the commented out section below since they are intended for use with the examples web application. --> <!-- NOTE: The sample user and role entries below are intended for use with the examples web application. They are wrapped in a comment and thus are ignored when reading this file. If you wish to configure these users for use with the examples web application, do not forget to remove the <!.. ..> that surrounds them. You will also need to set the passwords to something appropriate. --> <!-- <role rolename="tomcat"/> <role rolename="role1"/> <user username="tomcat" password="<must-be-changed>" roles="tomcat"/> <user username="both" password="<must-be-changed>" roles="tomcat,role1"/> <user username="role1" password="<must-be-changed>" roles="role1"/> --> <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="admin" roles="manager-gui,admin-gui"/> <!-- <role rolename="admin"/> --> <!-- <role rolename="admin-gui"/> --> <!-- <role rolename="admin-script"/> --> <!-- <role rolename="manager"/> --> <!-- <role rolename="manager-gui"/> --> <!-- <role rolename="manager-script"/> --> <!-- <role rolename="manager-jmx"/> --> <!-- <role rolename="manager-status"/> --> <!-- <user name="admin" password="adminadmin" roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status" /> --> </tomcat-users> [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cp tomcat.yaml tomcat-v2.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# vi tomcat-v2.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat tomcat-v2.yaml - hosts: tomcat remote_user: root tasks: - name: install tomcat-admin-webapps yum: name=tomcat-admin-webapps state=installed - name: install tomcat-users.xml copy: src=/root/ansible_workshop/tomcat-users.xml dest=/etc/tomcat/tomcat-users.xml owner=root group=tomcat mode=0640 tags: tcusers notify: restart tomcat - name: install server.xml template: src=/root/ansible_workshop/server.xml.j2 dest=/etc/tomcat/server.xml owner=root group=tomcat notify: restart tomcat tags: serverconf - name: start tomcat service: name=tomcat state=started handlers: - name: restart tomcat service: name=tomcat state=restarted [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --syntax-check tomcat-v2.yaml playbook: tomcat-v2.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check tomcat-v2.yaml PLAY [tomcat] *********************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [install tomcat-admin-webapps] ************************************************************************************************************************* ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [install tomcat-users.xml] ***************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] TASK [install server.xml] *********************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] TASK [start tomcat] ***************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] RUNNING HANDLER [restart tomcat] **************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=6 changed=2 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=6 changed=2 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=6 changed=2 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check --list-tags tomcat-v2.yaml playbook: tomcat-v2.yaml play #1 (tomcat): tomcat TAGS: [] TASK TAGS: [serverconf, tcusers] [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check -t tcusers tomcat-v2.yaml PLAY [tomcat] *********************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] TASK [install tomcat-users.xml] ***************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] RUNNING HANDLER [restart tomcat] **************************************************************************************************************************** changed: [node101.yinzhengjie.org.cn] changed: [node103.yinzhengjie.org.cn] changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=3 changed=2 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=3 changed=2 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=3 changed=2 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check -t serverconf tomcat-v2.yaml PLAY [tomcat] *********************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node101.yinzhengjie.org.cn] ok: [node103.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] TASK [install server.xml] *********************************************************************************************************************************** ok: [node103.yinzhengjie.org.cn] ok: [node101.yinzhengjie.org.cn] ok: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************** node101.yinzhengjie.org.cn : ok=2 changed=0 unreachable=0 failed=0 node102.yinzhengjie.org.cn : ok=2 changed=0 unreachable=0 failed=0 node103.yinzhengjie.org.cn : ok=2 changed=0 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
4>.循环语句案例展示
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat lamp.yaml - hosts: node102.yinzhengjie.org.cn remote_user: root tasks: - name: install {{ item }} yum: name={{ item }} state=installed with_items: - httpd - memcached - php-fpm - name: start {{ item }} service: name={{ item }} state=started with_items: - httpd - memcached - php-fpm [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --syntax-check lamp.yaml playbook: lamp.yaml [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check lamp.yaml PLAY [node102.yinzhengjie.org.cn] *************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node102.yinzhengjie.org.cn] TASK [install {{ item }}] *********************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] => (item=[u'httpd', u'memcached', u'php-fpm']) TASK [start {{ item }}] ************************************************************************************************************************************* ok: [node102.yinzhengjie.org.cn] => (item=httpd) ok: [node102.yinzhengjie.org.cn] => (item=memcached) changed: [node102.yinzhengjie.org.cn] => (item=php-fpm) PLAY RECAP ************************************************************************************************************************************************** node102.yinzhengjie.org.cn : ok=3 changed=2 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# [root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook lamp.yaml PLAY [node102.yinzhengjie.org.cn] *************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [node102.yinzhengjie.org.cn] TASK [install {{ item }}] *********************************************************************************************************************************** changed: [node102.yinzhengjie.org.cn] => (item=[u'httpd', u'memcached', u'php-fpm']) TASK [start {{ item }}] ************************************************************************************************************************************* ok: [node102.yinzhengjie.org.cn] => (item=httpd) ok: [node102.yinzhengjie.org.cn] => (item=memcached) changed: [node102.yinzhengjie.org.cn] => (item=php-fpm) PLAY RECAP ************************************************************************************************************************************************** node102.yinzhengjie.org.cn : ok=3 changed=2 unreachable=0 failed=0 [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
5>.when语句案例展示