一、Playbook图解
- 用户通过ansible命令直接调用yml语言写好的playbook,playbook由多条play组成;
- 每条play都有一个任务(task)相对应的操作,然后调用模块modules,应用在主机清单上,通过ssh远程连接 从而控制远程主机或者网络设备。
二、Playbook核心元素
- Hosts:执行的远程主机列表(应用在哪些主机上)。
- Tasks:任务集。
- Variables:内置变量或自定义变量在playbook中调用。
- Templates模板:可替换模板文件中的变量并实现一些简单逻辑的文件。
- Handlers和notify:结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行。
- tags标签:指定某条任务执行,用于选择运行playbook中的部分代码。 ansible具有幂等性,因此会自动跳过没有变化的部分, 即便如此,有些代码为测试其确实没有发生变化的时间依然会非常地长。 此时,如果确信其没有变化,就可以通过tags跳过此些代码片断 ansible-playbook -t tagsname useradd.yml。
三、Playbook基础组件
1️⃣Hosts
playbook中的每一个play的目的都是为了让特定主机以某个指定的用户身份执行任务。
hosts用于指定要执行指定任务的主机,须事先定义在主机清单中。
可以是如下形式:
- one.example.com
- one.example.com:two.example.com 1
- 92.168.1.50
- 192.168.1.*
> Websrvs:dbsrvs 或者,两个组的并集
> Websrvs:&dbsrvs 与,两个组的交集
> webservers:!phoenix 在websrvs组,但不在dbsrvs组
示例: - hosts: websrvs:dbsrvs
2️⃣remote_user
可用于Host和task中。 也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务; 此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户。
- hosts: websrvs
remote_user: root (可省略,默认为root) 以root身份连接
tasks: 指定任务
- name: test connection
ping:
remote_user: magedu
sudo: yes 默认sudo为root
sudo_user:wang sudo为wang
3️⃣task和action
任务列表task:由多个动作,多个任务组合起来的,每个任务都调用的模块,一个模块一个模块执行。
① play的主体部分是task list,task list中的各任务按次序逐个在hosts中指定的所有主机上执行, 即在所有主机上完成第一个任务后,再开始第二个任务。
② task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。 模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致。
③每个task都应该有其name,用于playbook的执行结果输出,建议其内容能清晰地描述任务执行步骤。 如果未提供name,则action的结果将用于输出。
两种格式:
- action: module arguments
- module: arguments 建议使用 模块: 参数
注意:shell和command模块后面跟命令,而非key=value。
某任务的状态在运行后为changed时,可通过"notify"通知给相应的handlers;
任务可以通过"tags"打标签,可在ansible-playbook命令上使用-t指定进行调用。
示例:
tasks:
- name: disable selinux 描述
command: /sbin/setenforce 0 模块名: 模块对应的参数
如果命令或脚本的退出码不为零,可以使用如下方式替代。
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
转错为正 如果命令失败则执行 true
或者使用ignore_errors来忽略错误信息。
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True 忽略错误
四、运行Playbook
运行playbook的方式
ansible-playbook <filename.yml> ... [options]
常见选项
--check -C 只检测可能会发生的改变,但不真正执行操作
(只检查语法,如果执行过程中出现问题,-C无法检测出来)
(执行playbook生成的文件不存在,后面的程序如果依赖这些文件,也会导致检测失败)
--list-hosts 列出运行任务的主机
--list-tags 列出tag (列出标签)
--list-tasks 列出task (列出任务)
--limit 主机列表 只针对主机列表中的主机执行
-v -vv -vvv 显示过程
示例
ansible-playbook hello.yml --check 只检测
ansible-playbook hello.yml --list-hosts 显示运行任务的主机
ansible-playbook hello.yml --limit websrvs 限制主机
五、Playbook VS ShellScripts
安装httpd
SHELL脚本:
#!/bin/bash
# 安装Apache
yum install --quiet -y httpd
# 复制配置文件
cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
cp/tmp/vhosts.conf /etc/httpd/conf.d/
# 启动Apache,并设置开机启动
service httpd start
chkconfig httpd on
Playbook定义:
---
- hosts: all
remote_user: root
tasks:
- name: "安装Apache"
yum: name=httpd yum模块:安装httpd
- name: "复制配置文件"
copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/ copy模块: 拷贝文件
- name: "复制配置文件"
copy: src=/tmp/vhosts.conf dest=/etc/httpd/conf.d/
- name: "启动Apache,并设置开机启动"
service: name=httpd state=started enabled=yes service模块: 启动服务
六、示例
1️⃣创建用户
示例:sysuser.yml
---
- hosts: all
remote_user: root
tasks:
- name: create mysql user
user: name=mysql system=yes uid=36
- name: create a group
group: name=httpd system=yes
2️⃣安装httpd服务
示例:httpd.yml
- hosts: websrvs
remote_user: root
tasks:
- name: Install httpd
yum: name=httpd state=present
- name: Install configure file
copy: src=files/httpd.conf dest=/etc/httpd/conf/
- name: start service
service: name=httpd state=started enabled=yes
3️⃣安装nginx服务
示例 nginx.yml
- hosts: all
remote_user: root
tasks:
- name: add group nginx
user: name=nginx state=present
- name: add user nginx
user: name=nginx state=present group=nginx
- name: Install Nginx
yum: name=nginx state=present
- name: Start Nginx
service: name=nginx state=started enabled=yes