YAML语法和playbook写法
ansible的playbook采用了yaml语法,它简单地实现了json格式的事件描述。在学习ansible playbook之前,很有必要把yaml的语法格式、引用方式做个梳理。
一、初步说明
以一个简单的playbook为例,说明yaml的基本语法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P4hLJEuE-1666695756966)(file:///C:\Users\左冕\AppData\Local\Temp\ksohtml5084\wps1.jpg)]
\1. yaml⽂件以 — 开头,以表明这是⼀个yaml⽂件,就像xml⽂件在开头使⽤ <?xml version="1.0" encoding="utf-8"?> 宣称它是xml⽂件⼀样。但即使没有使⽤ — 开头,也不会有什么影响。
\2. yaml中使⽤"#“作为注释符,可以注释整⾏,也可以注释⾏内从”#"开始的内容。
\3. yaml中的字符串通常不⽤加任何引号,即使它包含了某些特殊字符。但有些情况下,必须加引号,最常见的是在引⽤变量的时候。
\4. 关于布尔值的书写格式,即true/false的表达⽅式。其实playbook中的布尔值类型⾮常灵活,可分为两种情况:
模块的参数: 这时布尔值作为字符串被ansible解析。接受yes/on/1/true/no/of f /0/false,这时被ansible解析。例如上⾯⽰例中的 update_cache=yes 。
⾮模块的参数: 这时布尔值被yaml解释器解析,完全遵循yaml语法。接受不区分⼤⼩写的 true/yes/on/y/f alse/no/off /n。例如上⾯的 gpgcheck=no 和 enabled=True 。建议遵循ansible的官⽅规范,模块的布尔参数采⽤yes/no,⾮模块的布尔参数采⽤True/False
*playbook的内容*
每个play都包含⼀个hosts和⼀个tasks,hosts定义的是inventory中待控制的主机,tasks下定义的是⼀系列task任务列表,⽐如调⽤各个模块。这些task按顺序⼀次执⾏⼀个,直到所有被筛选出来的主机都执⾏了这个task之后才会移动到下⼀个task上进⾏同样的操作。
需要注意的是,虽然只有被筛选出来的主机会执⾏对应的task,但是所有主机(此处的所有主机表⽰的是,hosts选项所指定的那些主机)都会收到相同的task指令,所有主机收到指令后,ansible主控端会筛选某些主机,并通过ssh在远程执⾏任务。
*YAML字典*
YAML中使用的key/value对也称为字典、散列或关联数组在key/value对中,键与值通过由冒号和空格组成的分隔符隔开name: svcrolesvcservice: httpsvcport: 80 字典也可以使用内嵌块格式表示,其中多个key/value对用花括号括起,并由逗号和空格隔开- {name: svcrole, svcservice: http, svcport: 80}*YAML列表*
在YAML中,列表类似于其他编程语言中的数组为表示一组列表项,使用一个短划线加一个空格作为每个列表项的前缀hosts: - server1 - server2列表也可使用内嵌块表示,其中多个列表项用方括号括起来并由逗号和空格隔开hosts: [server1, server2]
playbook的使用
ansible-playbook用于运行剧本,-C 测试运行结果,并不是真的执行任务。
一、部署web服务器
1、部署yum仓库
2、安装httpd
3、新建/www目录
4、在/www中新建index.html,内容为my name is chenyu(chenyu为你们自己名字的全拼)
5、该web服务器的DocumentRoot为/www
5、实现在ansible中能够使用http://node1访问到该网页内容
[student@ansible ansible]$ cat httpd.yml
---
- name: create web service
hosts: dev
tasks:
- name: mount mnt
mount:
src: /dev/sr0
fstype: iso9660
path: /mnt
state: mounted
- name: cteate yum repo-1
yum_repository:
file: hehe
name: ww
description: dkajflf
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: cteate yum repo-2
yum_repository:
file: hehe
name: kk
description: skdjfka
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: install httpd
yum:
name: httpd
state: present
- name: create www link
file:
src: /var/www/html/
dest: /www
state: link
mode: 0755
- name: create /www/index.html
copy:
content: "my name is zuomian\n"
dest: /www/index.html
- name: firewalld set
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
- name: set context
file:
path: /www/index.html
setype: httpd_sys_content_t
- name: set -R
shell: restorecon -Rv /www/index.html
- name: set httpd.conf
replace:
path: /etc/httpd/conf/httpd.conf
regexp: 'DocumentRoot "/var/www/html"'
replace: 'DocumentRoot "/www"'
- name: set httpd.conf2
replace:
path: /etc/httpd/conf/httpd.conf
regexp: <Directory "/var/www">
replace: <Directory "/www">
- name: started httpd service
service:
name: httpd
state: restarted
enabled: yes
[student@ansible ansible]$ ansible-playbook httpd .yml
PLAY [create web service] *********************************************** *******
TASK [Gathering Facts]
*********************************************************
ok : [node1]
TASK [mount mnt] ***************************************************************
ok : [node1]
TASK [cteate yum repo-1] *******************************************************
ok : [node1]
TASK [cteate yum repo-2] *******************************************************
ok : [node1]
TASK [install httpd] ***********************************************************
ok : [node1]
TASK [create www link]
*********************************************************
ok : [node1]
TASK [create /www/index .html] **************************************************
ok : [node1]
TASK [firewalld set] ***********************************************************
ok : [node1]
TASK [set context] *************************************************************
ok : [node1]
TASK [set -R] ******************************************************************
changed : [node1]
TASK [set httpd .conf] **********************************************************
changed : [node1]
TASK [set httpd .conf2]
*********************************************************
changed : [node1]
TASK [started httpd service] ***************************************************
changed : [node1]
PLAY RECAP ************************************************************** *******
node1: ok=13 changed=4 unreachable=0 failed =0 skipped=0 rescued=0 ignored=0
[student@ansible ansible]$ curl http://node1 my name is zuomian
二、使用notify…handlers
1、写一个剧本runtime.yml,只对node1操作
2、创建用户aa,该用户不能用于登录,家目录/www
3、在/www创建一个文件html
4、每次执行该剧本时,将系统的当前时间输入到html文件中。
5、如果html中的时间发生变化,那么创建/tmp/kk的文件
[student@ansible ansible]$ cat runtime.yml
---
- name: create tasks
hosts: node1
tasks:
- name: usseradd aa -d /www -s /sbin/nologin
user:
name: aa
shell: /sbin/nologin
home: /www
state: present
- name: create file html
file:
path: /www/html
state: touch
-name: date > html
shell: date > /www/html
notify:
- haha
handlers:
-name: haha
file:
handlers:
- name: haha
file:
path: /tmp/kk
state: touch
[student@ansible ansible]$ ansible-playbook runtime .yml
PLAY [create tasks] ***************************************************** *******
TASK [Gathering Facts]
*********************************************************
ok : [node1]
TASK [useradd aa -d /www -s /sbin/nologin] *****************************
*******
changed : [node1]
TASK [create file html] ************************************************* *******
changed : [node1]
TASK [date > html] ****************************************************** *******
changed : [node1]
RUNNING HANDLER [haha]
*********************************************************
changed : [node1]
PLAY RECAP ************************************************************** *******
node1 : ok=5 changed=4 unreachable=0 failed
=0 skipped=0 rescued=0 ignored=0
[root@node1 ~]# cd /www/
[root@node1 www]# ls
html index .html
[root@node1 www]# cat html
Tue Oct 25 16 :15 :54 CST 2022
[root@node1 tmp]# ls
kk
failed
=0 skipped=0 rescued=0 ignored=0
[root@node1 ~]# cd /www/
[root@node1 www]# ls
html index .html
[root@node1 www]# cat html
Tue Oct 25 16 :15 :54 CST 2022
[root@node1 tmp]# ls
kk
本文介绍了Ansible Playbook的基础知识,包括YAML语法的使用,如字符串、布尔值的表示,以及playbook的结构。文章通过示例详细讲解了hosts、tasks、字典和列表的定义。此外,还提到了playbook的执行命令`ansible-playbook`,以及如何在剧本中使用handlers来响应状态变化。

1226

被折叠的 条评论
为什么被折叠?



