一、YMAL语言:一般用来写配置文件的语言
1.语法规则:
大小写敏感
使用缩进表示层级关系
缩进时不允许使用Tab键,只允许使用空格。(两个空格作为缩进)
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
2.支持的数据结构:
对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary) #键: 值 (注意冒号后面有个空格)
数组:一组按次序排列的值,又称为序列(sequence) / 列表(list) #depend: \- gcc #注意-后面有个空格 \- openssl-devel
纯量(scalars)标量:单个的、不可再分的值
#注意:
1.缩进建议用两个空格,不能用tab
2.注释用#
二、playbook剧本:剧本是Ansible的配置、部署语言,由它对描述你想要远程机器执行的策略或步骤,使用YAML编写。
是由一个或多个“play”(task)组成的列表。从根本上讲task就是调用ansible的一个module。将多个play组织在一个playbook中,运行时就会根据自上而下的顺序依次执行。
#注意
1.需要以---开始,且需顶行首写
2.文件后缀.yml 或 .yaml
3.包含多个任务
4.自上而下执行
#执行剧本:ansible-palybook 文件名
#语法检查命令:ansible-palybook 文件名 --syntax-check
#列出所有任务:ansible-playbook 文件名 --list-task
#列出主机:ansible-playbook 文件名 --list-hosts
#第一个剧本:实现ping功能
#编写playbook
[root@manager ~]# vim ping.yml
---
- hosts: node1
tasks:
- name: test connction
ping:
#用ansible-playbook 文件名 执行
[root@manager ~]# ansible-playbook ping.yml
PLAY [node1] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node1]
TASK [test connction] **********************************************************
ok: [node1]
PLAY RECAP *********************************************************************
node1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#检查语法
[root@manager ~]# ansible-playbook ping.yml --syntax-check
playbook: ping.yml
#列出所有任务
[root@manager ~]# ansible-playbook ping.yml --list-task
playbook: ping.yml
play #1 (node1): node1 TAGS: []
tasks:
test connction TAGS: []
#列出主机
[root@manager ~]# ansible-playbook ping.yml --list-hosts
playbook: ping.yml
play #1 (node1): node1 TAGS: []
pattern: [u'node1']
hosts (1):
node1
练习:一键化部署apache
[root@manager ~]# vim apache.yml
---
- hosts: node1
tasks:
- name: 1.install httpd
yum:
name: httpd
state: latest
- name: 2.configure httpd
copy:
src: /files/httpd.conf
dest: /etc/httpd/conf/httpd.conf
- name: 3.index.html
copy:
content: "this is test page\n"
dest: /var/www/html/index.html
- name: 4.start service
service:
name: httpd
state: started
enabled: yes
[root@manager ~]# ansible-playbook apache.yml
PLAY [node1] ***********************************************************************
TASK [Gathering Facts] *************************************************************
ok: [node1]
TASK [1.install httpd] *************************************************************
ok: [node1]
TASK [2.configure httpd] ***********************************************************
ok: [node1]
TASK [3.index.html] ****************************************************************
changed: [node1]
TASK [4.start service] *************************************************************
ok: [node1]
PLAY RECAP *************************************************************************
node1 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@manager ~]# curl node1
this is test page
[root@manager ~]#
对上述列题进行修改:增加notify(handlers结合notify实现触发条件): 当配置文件发生改变(监听端口改为88),将重启服务
[root@manager ~]# cat apache2.yml
---
- hosts: node1
tasks:
- name: 1.install httpd
yum:
name: httpd
state: latest
- name: 2.configure httpd
copy:
src: /files/httpd.conf
dest: /etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: 3.index.html
copy:
content: "this is test page\n"
dest: /var/www/html/index.html
- name: 4.start service
service:
name: httpd
state: started
enabled: yes
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
#修改配置文件的监听端口为88
[root@manager ~]# cat /files/httpd.conf
Listen 88
[root@manager ~]# curl node1
curl: (7) Failed connect to node1:80; 拒绝连接
[root@manager ~]# curl node1:88
this is test page