一. 实施playbook
本章目标:编写基本的ansibleplaybook
1. 编写和运行playbook
查看指定用户的临时命令
[root@workstation~]#ansible-muser-a"name=studentuid=1000
state=present"servera.lab.example.com
'yaml格式通常以yml为扩展名,yaml对于缩进量没有严格要求,但是有两个基本原则'
'1.处于同一层次结构中同一级别的数据元素必须具有相同的缩进量'
'2.如果项目属于其他项目的子项,其缩进量必须大于父项'
2. 改写为playbook
[root@workstation~]#catuser.yml
---#开头三个破折号,文档的开始标记
-name:ConfigureUser#可选,但是建议使用
hosts:servera.lab.example.com
tasks:
-name:StudentUser
user:
name:student
uid:1000
state:present
...#结尾三个省略号,结束标记(通常省略)
'play本身是一个键值对集合,同一play中的键应当使用相同的缩进量'
##安装apache
[root@workstationplaydemo]#cat webserver.yml
---
-name:SetupWebserver
hosts:servera.lab.example.com
tasks:
-name:HttpInstalled
yum:
name:httpd
state:latest
...
[root@workstationplaydemo]#ansible-playbook webserver.yml
PLAY[SetupWebserver]
***********************************************************************************
****************************************
TASK[GatheringFacts]
***********************************************************************************
****************************************
ok:[servera.lab.example.com]
TASK[HttpInstalled]
***********************************************************************************
*****************************************
changed:[servera.lab.example.com]
PLAYRECAP
***********************************************************************************
****************************************************
servera.lab.example.com:ok=2changed=1unreachable=0
failed=0skipped=0rescued=0ignored=0
##例子:确保服务开机启动
[root@workstationplaydemo]#catservice.yml
---
- name: SetupWebserver
hosts: servera.lab.example.com
tasks:
- name: Apacheisenabled
service:
name: httpd
enabled: true
- name: Postfixisenabled
service:
name: postfix
enabled: true
...
##提高输出详细程度
ansible-playbook
默认输出不提供详细任务执行信息。
-v
参数提供,共四个级别:
-v#显示任务结果
-vv#显示任务结果和任务配置
-vvv#包含关于与受管主机的连接信息
-vvvv#增加连接插件相关的额外详细程度选项(包括受管主机上用于执行脚本的用户及所
执行的脚本)
#执行playbool前最好进行语法验证
[root@workstationplaydemo]#ansible-playbook--syntax-check webserver.yml
#无语法错误
playbook:webserver.yml
[root@workstationplaydemo]#ansible-playbook--syntax-check webserver.yml
#有语法错误,会提示错误位置
ERROR!SyntaxErrorwhileloadingYAML.
mappingvaluesarenotallowedinthiscontext
Theerrorappearstobein'/root/playdemo/webserver.yml':line8,column14,but
may
beelsewhereinthefiledependingontheexactsyntaxproblem.
Theoffendinglineappearstobe:
name:httpd
state:latest
^here
#执行空运行
[root@workstationplaydemo]#ansible-playbook -C webserver.yml
PLAY[SetupWebserver]
***********************************************************************************
****************************************
TASK[GatheringFacts]
***********************************************************************************
****************************************
ok:[servera.lab.example.com]
TASK[HttpInstalled]
***********************************************************************************
*****************************************
changed:[servera.lab.example.com]
PLAYRECAP
***********************************************************************************
****************************************************
servera.lab.example.com:ok=2changed=1unreachable=0
failed=0skipped=0rescued=0ignored=0
'空运行会报告执行这个playbook将会发生什么,但不会改变目标主机'
#安装、配置默认发布页并启动apache
[root@workstationplaybook-basic]#cat site.yml
---
- name: Install and Start Apache
host: web
tasks:
- name: Apache is present
yum:
name: httpd
state: present
- name: Change index.html
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: Start Apache
service:
name:httpd
state:started
enabled:true
...
#检测语法
[root@workstationplaybook-basic]#ansible-playbook--syntax-check site.yml
playbook:site.yml
#创建配置文件和清单
[root@workstationplaybook-basic]#cat ansible.cfg
[defaults]
inventory=./inventory
[root@workstationplaybook-basic]#cat inventory
[web]
serverb.lab.example.com
serverc.lab.example.com
#默认发布页面
[root@workstationplaybook-basic]#cat files/index.html
www.westos.org
#执行playbook
[root@workstationplaybook-basic]#ansible-playbooksite.yml
PLAY[InstallandStartApache]
***********************************************************************************
*******************************
TASK[GatheringFacts]
***********************************************************************************
****************************************
ok:[serverb.lab.example.com]
ok:[serverc.lab.example.com]
TASK[Apacheispresent]
***********************************************************************************
**************************************
changed:[serverc.lab.example.com]
changed:[serverb.lab.example.com]
TASK[Changeindex.html]
***********************************************************************************
**************************************
changed:[serverc.lab.example.com]
changed:[serverb.lab.example.com]
TASK[StartApache]
***********************************************************************************
*******************************************
changed:[serverb.lab.example.com]
changed:[serverc.lab.example.com]
PLAYRECAP
***********************************************************************************
****************************************************
serverb.lab.example.com:ok=4changed=3unreachable=0
failed=0skipped=0rescued=0ignored=0
serverc.lab.example.com:ok=4changed=3unreachable=0
failed=0skipped=0rescued=0ignored=0
#测试
[root@workstationplaybook-basic]#curl serverb.lab.example.com
www.westos.org
[root@workstationplaybook-basic]#curl serverc.lab.example.com
www.westos.org
3. 实施多个play
##模板
---
- name: first play
hosts: servera.example.com
tasks:
- name: first task
yum:
name: httpd
status: present
- name: second task
service:
name: httpd
enabled:true
- name: second play
hosts: serverb.example.com
tasks:
- name: first task
service:
name: mariadb
enabled: true
...
#练习1
ansible-doc -l #列出所有模块
[root@workstation~]#ansible-doc yum #列出yum的用法和示例
[root@workstation~]#ansible-doc -s yum #终端中输出yum模块中各参数的用法
PLAYBOOK语法变化
###yaml注释
#This is a YAML comment
somedata#This is also a YAML comment
###yaml字符串
this is a string
'this is a string'
"this is a string"
#练习
[root@workstationplaybook-multi]#ls
ansible.cfg intranet.yml inventory
[root@workstationplaybook-multi]#cat ansible.cfg
[defaults]
inventory=./inventory
[root@workstationplaybook-multi]#cat inventory
[web]
servera.lab.example.com
[root@workstationplaybook-multi]#vim intranet.yml
---
- name: Enable intranet services
hosts: web
become: yes
tasks:
- name: latest version of httpd and firewalld installd #检测httpd是否安装和是否最新版本
yum:
name:
- httpd
- firewalld
state: latest
- name: test html page is configured #检测是否配置默认发布页面
copy:
content: "Welcome to westos!\n"
dest: /var/www/html/index.html
- name: firewalld enabled and running #检测防火墙是否开启并处于enable状 态
service:
name: firewalld
enabled: true
state: started
- name: firewalld permit saccess to httpd service #检测防火墙是否允许httpd服务访问
firewalld:
service: http
permanent: true
state: enabled
immediate: yes
- name: httpd enabled and running #检测httpd是否开启和设置开机启动
service:
name: httpd
enabled: true
state: started
- name: Test intranet webserver #在本机测试
hosts: localhost
become: no
tasks:
- name: connect to intranet webserver #测试访问servera
uri:
url: http://servera.lab.example.com
return_content: yes
status_code: 200
#检测语法
[root@workstationplaybook-multi]#ansible-playbook --syntax-check intranet.yml
[root@workstationplaybook-multi]#ansible-playbook -vintranet.yml #运行
...
TASK[connecttointranetwebserver]
***********************************************************************************
*************************
ok:[localhost]=>{"accept_ranges":"bytes","changed":false,"connection":
"close","content":"Welcometowestos!\n","content_length":"19","content_type":
"text/html;charset=UTF-8","cookies":{},"cookies_string":"","date":"Sun,08Mar
202013:43:13GMT","elapsed":0,"etag":"\"13-5a05811249f16\"","last_modified":
"Sun,08Mar202013:43:08GMT","msg":"OK(19bytes)","redirected":false,
"server":"Apache/2.4.37(RedHatEnterpriseLinux)","status":200,"url":
"http://servera.lab.example.com"}
PLAYRECAP
***********************************************************************************
****************************************************
localhost:ok=2changed=0unreachable=0failed=0
skipped=0rescued=0ignored=0
servera.lab.example.com:ok=6changed=4unreachable=0
failed=0skipped=0rescued=0ignored=0
可以看到返回了内容welcome to westos和状态码200
4. 管理变量和事实
将playbook中的某些值使用变量代替,从而
简化playbook的编写
######管理变量######
#ansible变量简介
变量可能包含下面这些值:
要创建的用户、要安装的软件包、
要重启的服务、
要删除的文件、
要从互联网检索的文档
#命名变量
变量名称必须以字母开头,并且只能含有字母、数字和下划线
错误 | 正确 |
webserver | web_server |
westos.file | remote_file |
1stfilefile1
定义变量
三个范围级别
全局范围:从命令行或ansible配置设置的变量
play范围:在play和相关结构中设置的变量
主机范围:由清单、事实收集或注册的任务,在主机组和个别主机上设置的变量
注意:如果多个级别上定义了相同名称的变量,优先采用级别最高的变量,窄范围优先于广范围
playbook中的变量
在playbook中定义变量
1.常见方式:在playbook开头的vars块中:
- host: all
vars:
user: student
home: /home/student
2.在外部文件定义playbook变量
- hosts: all
vars_files:
- vars/users.yml
在users.yml文件中写入
user: student
home: /home/student
在playbook中使用变量
将变量名称放在花括号内即可
vars:
user: westos
tasks:
- name: Createuser{{ user }}
user:
name: "{{ user }}"
注意:当变量用作开始一个值的第一元素时,必须使用引号