这份笔记介绍的是Ansible playbook的格式及相关的内容。
Ansible笔记 Ansible Playbook YAML
YAML(/ˈjæməl/,尾音类似 camel ) 是“YAML不是一种标记语言”的外语缩写(见前方参考资料原文内容);但为了强调这种语言以数据做为中心,而不是以置标语言为重点,而用返璞词重新命名。它是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言。数据结构 可以用类似大纲的缩排方式 呈现,结构通过缩进来表示 ,连续的项目通过减号“-”来表示 ,map结构里面的key/value对用冒号“:”来分隔 。 YAML文件一般的文件名为.yaml 或 .yml,文本结构举例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 house: family: name: Doe parents: - John - Jane children: - Paul - Mark - Simone address: number: 34 street: Main Street city: Nowheretown zipcode: 12345
Ansible Playbook的关键字 内容与命令的内容基本一致,有如下的几个关键字:
- hosts 用来指定控制主机的范围,注意短横线后空格接字符
remote_user 用来指定使用的用户
tasks 可在字段下方缩进指定需要执行的任务,注意缩进
- name 用来定义任务的名称或描述,注意短横线后空格接字符
yum : name=httpd state=latest 定义使用的模块功能:后面跟操作参数
tags : 对任务进行标记,可通过命令调用标记执行或排除某些任务
when : 判断,满足when后面的条件才执行任务
notify : 触发handler的标志
handlers : 定义触发任务的内容
- name : 任务的名称,注意短横线后空格接字符
service : name=httpd state=restarted 定义使用的模块:后面跟操作参数
Ansible Playbook示例 先看一个已经写好的playbook,针对写好的来解释playbook如何书写。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 --- - hosts: all remote_user: root tasks: true - name: install httpd CentOS yum: name: httpd state: latest tags: install_httpd when: ansible_os_family == "RedHat" true - name: install httpd Ubuntu apt: name: apache state: latest when: ansible_os_family == "Ubuntu" tags: install_httpd true - name: set the homepage copy: src: ./index.html dest: /var/www/html/index.html notify: - restart_the_service true - name: start httpd service: name: httpd service: started true - name: empty firewalld shell: iptables -F true - name: remove httpd yum: name: httpd state: absent tags: remove_httpd true - name: clean stuff file: name: /var/www/html/ state: absent tags: clean_stuff handlers: true - name: restart_the_service service: name: httpd state: restarted
Ansible playbook执行命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 使用格式: trueansible-playbook [options] playbook.yml [playbook2 ...] 命令作用: trueRuns Ansible playbooks, executing the defined tasks on the targeted hosts. true 命令示例: [root@Hayden ~]$ ansible-playbook --syntax-check install_httpd.yaml [root@Hayden ~]$ ansible-playbook -C install_httpd.yaml [root@Hayden ~]$ ansible-playbook install_httpd.yaml [root@Hayden ~]$ ansible-playbook -t "install_httpd" install-httpd.yaml [root@Hayden ~]$ ansible-playbook --skip-tags "install_httpd" install-httpd.yaml [root@Hayden ~]$ ansible-playbook --skip-tags "install_httpd clean_stuff" install-httpd.yaml
默认文件位置 sudo pacman -Ql ansible | grep hosts 查看hosts文件的范例文件所在目录,其他文件操作类似.
如果有不明白的命令可通过ansible-doc命令直接查看内置的说明文件,针对模块的 ansible-doc -l 为列出所有可用模块