Fork me on GitHub

[转] 第三章 Ansible Playbook说明介绍

3.1 Playbook语法简介

Ansible使用YAML语法描述配置文件,YAML语法以简洁明了、结构清晰著称。Ansible的任务配置文件被称为Playbook,我们可以称之为"剧本”。

每一个剧本(Playbook)中都包含一系列的任务,这每个任务在Ansible中又被称为"戏剧”(play)。一个剧本(Playbook)中包含多出戏剧(play),这很容易理解。

3.1.1 多行缩进

数据结构可以用类似大纲的缩排方式呈现, 结构通过缩进来表示, 连续的项目通过减号 ”- ” 来表示, map结构里面的key/value对用冒号 ” : ” 来分隔。 格式如以下代码所示:

house:
family:
name: Doe
parents:
- John
- Jane
children:
- Paul
- Mark
- Simone
address:
number: 34
street: Main Street
city: Nowheretown
zipcode: 12345

1)字串不一定要用双引号标识;

2)在缩排中空白字符的数目并不重要, 只要相同阶层的元素左侧对齐就可以了(不能使用Tab字符);

3)允许在文件中加入选择性的空行, 以增加可读性;

4)选择性的符号"..." 可以用来表示档案结尾(在利用串流的通信中, 这非常有用,可以在不关闭串流的情况下, 发送结束信号)。

3.1.2 单行缩写

YAML也有用来描述好几行相同结构的数据的缩写语法, 数组用” [] “ 包括起来, hash用“ {} ” 来包括。因此, 上面的这个YAML能够缩写成这样:

house:
family : { name: Doe, par ents: [John, Janel, children: [Paul, Mark, Simone]
address: { number: 34, street: Main Street, city: Nowheretown, zipcode: 12345 }

3.1.3 Playbook样例文件解析

---
- name: This is demo
hosts: webservers
# 这个是变量
vars:
  http_port: 80
  max_clients: 200
# 远端的执行权限
  remote_user: root
tasks:
# 利用YUM模块来操作
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
# 触发重启服务器
notify:
  - name: ensure apache is running
    service:
      name: httpd
      state: started
# 这里的restart apache和上面的触发是配对的。这就是handlers的作用。相当于tag
handlers:
  - name: restart apache
    service:
      name:  httpd
      state:  restarted
  1. 总的来说, Playbook 语法具有如下一些特性。

  2. 需要以“ --- " (3 个减号)开始, 且需顶行首写。

  3. 次行开始正常写Playbook 的内容, 但笔者建议写明该Playbook 的功能。

  4. 使用 # 号注释代码。

  5. 缩进必须是统一的, 不能将空格和Tab 混用。

  6. 缩进的级别必须是一致的, 同样的缩进代表同样的级别, 程序判别配置的级别是通过缩进结合换行来实现的。

  7. YAML 文件内容和Linux 系统大小写判断方式保持一致, 是区别大小写的, k/v 的值均需大小写敏感。

  8. k/v 的值可同行写也可换行写。同行使用“ : “ 分隔, 换行写需要以“ - "分隔。

  9. 一个完整的代码块功能需最少元素, 需包括name: task。

  10. 一个name 只能包括一个task。

3.2 Playbook案例分析

之前介绍的Ad-Hoc 点对点、一次执行一个模块的操作方式已经使得Andsible 成为非常强大的管理工具, 但Playbook 将会使Ansible 的功能更为完善、Playbook 可以固化所有操作, 减少人为失误, 满足工程量较大的工具所具备的复杂度、可扩展度等要求, 使得Ansible 成为超一流的管理工具。

Shell脚本与Playbook的转换

现在, 越来越多的DevOps也开始将目光移向了Ansible, 因为Ansible可以轻松地将Shell脚本或简单的Shell命令转换为Ansible play。

# !/bin/bash
#安装Apache
yum -y install --quiet httpd httpd-devel

# 复制配置文件
cp /path/to/config/httpd.conf /etc/httpd/conf/httpd.conf
cp /path/to/httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf

# 启动Apache, 并设置开机启动
systemctl start httpd
systemctl enabled httpd

将上述Shell脚本转换为一个完整的Playbook后, 内容如以下代码所示:

---
- name: This is demo
hosts: all
remote_user: devops
become: yes
tasks:
  - name: "安装Apache"
    command: yum install --quiet -y httpd httpd-devel
  - name: "复制配置文件"
    command: cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
    command: cp /tmp/httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf
  - name: "启动Apache, 并设置开机启动"
    command: systemctl start httpd
    command: systemctl enabled httpd

将以上内容放在一个名为playbook.yml的文件中, 直接调用 ansible-playbook命令, 即可运行, 运行结果和脚本运行结果一致。

ansible-playbook ./playbook.yml --check

ansible-playbook ./playbook.yml

也就是说, 只要你有编写Shell脚本的基本能力, 你就可以快速学会利用Playbook来发挥Ansible的强大威力。在上述Playbook中, 我们使用了command模块来运行了标准的Shell命令 。我们还给 了每一出 play 一个 “ name", 因此当我们运行Playbook时, 每一个play都会有非常易读的信息输出。

上面的Playbook已经很好地实现了与Shell脚本相同的功能, 但是Ansible还有很多其他内置模块, 可以大幅度提升处理复杂配置的能力,

---
- name: Install web server
hosts: all
remote_user: devops
become: yes
tasks:
  - name: 安装Apache
    yum:
      name: httpd
      state: latest
       
  - name: 复制httpd配置文件
    copy:
      src: httpd.conf
      dest: /etc/httpd/conf
      owner: root
      group: root
      mode: 0644
       
  - name: 检查Apache运行状态, 并设置开机启动
    service:
      name: httpd
      state: started
      enabled: true
  - name: 检查firewalld运行状态, 并设置开机启动
    service:
      name: firewalld
      state: started
      enabled: true
  - name: 开启防火墙相应端口
    firewalld:
      port: 80/tcp
      permanent: yes
      state: disabled

3.3 Ansible-playbook实战技巧

3.3.1 限定执行范围

当 Playbook 指定的一批主机中有个别主机需进行变更时, 不需要去修改 Playbook 文件本身, 而是通过一些选项就可以直接限定和查看 Ansible 命令的执行范围。

--limit

如果运行上面的例子,会发现所有被Ansible管理的主机都会被操作,我们可以通过修改 "- hosts:" 字段来指定哪些主机将会应用Playbook的操作。

ansible-playbookplaybook.yml --limit webservers

这样一来(假设你的inventory文件中包含webserver组),即便Playbook中设定但也仅对webserver组生效。

--list-hosts

如果想知道在执行Playbook时, 哪些主机将会受影响,则使用 --list-hosts选项。

ansible-playbookplaybook.yrnl --list-hosts

运行结果:

playbook: playbook.yml

play # 1 (all): host count=1
webserver

如上两种方式针对需对批量操作的主机列表中的某一台主机做特定修改时非常有用。该技巧极大地增加了Ansible的使用灵活度。

3.3.2 用户与权限设置

--remote-user

在Playbook中,如果在hosts字段下没有定义users关键字,那么Ansible将会使用你在Inventory文件中定义的用户,如里Inventory文件中也没定义用户,Ansible将默认使用当前系统用户身份来通过SSH连接远程主机,在运程程主机中运行play内容。

我们也可以直接在ansible-playbook中使用--remote-user选项来指定用户。

ansible-playbookplaybook.yml --remote-user=tom

--ask-sudo-pass

在某些情况下,我们需要传递sudo密码到远程主机,来保证sudo命令的正常运行。这时,可以使用 --ask-sudo-pass (-K)选项来交互式的输入密码。

--sudo 使用--sudo选项,可以强制所有play都使用sudo用户,同时使用 --sudo-user选项指定sudo可以执行哪个用户的权限,如果不指定,则默认以root身份运行。比如, 当前用户Tom想以Jerry的身份运行Playbook,命令如下:

ansible-playbook playbook.yml --sudo --sudo-user=jerry --ask-sudo-pass

执行过程中,会要求用户输入Jerry的密码。

3.3.3 其他选项技巧

Ansible-playbook命令还有一些其他选项。

--inventory=PATH (-i PATH):指定inventory文件,默认文件是/etc/ansible/hosts。

--verbose (-v):显示详细输出,也可以使用 -vvvv 显示精确到每分钟的输出。

--extra-vars=VARS (-e VARS):定义在Playbook 使用的变量,格式为:“key=value,key=value”。

--forks=NUM ( -f NUM):指定并发执行的任务数,默认为5,根据服务器性能,调大这个值可提高Ansible执行效率。

--connection= TYPE (-c TYPE):指定连接远程主机的方式,默认为SSH,设为local时,则只在本地执行Playbook,建议不做修改。

--check(-C):检测模式,Playbook中定义的所有任务将在每台远程主机上进行检测,但并不直正执行。

3.4 实战一:Ansible 批量添加yum源

---
- name: in node1 install web server
hosts: servera
become: true
tasks:
name: aliyun repo address
description: EPEL YUM repo
file: aliyun
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
gpgcheck: no

3.5 实战二:Ansible 安装apache服务

在servera服务器上部署web 服务,并设置防火墙规则, 最后在serverb上测试是否可以正常访问

---
- name: in node1 install web server
hosts: servera
become: true
tasks:
- name: install apache
yum:
name: httpd
state: latest

- name: copy apache file
copy:
src: httpd.conf
dest: /etc/httpd/conf
owner: root
group: root
mode: 0644

- name: start and auto start httpd server
service:
name: httpd
state: started
enabled: true

- name: start and auto start firewalld server
service:
name: firewalld
state: started
enabled: true

- name: open firewalld port
firewalld:
port: 80/tcp
permanent: yes
state: disabled

- name: Test servera web server
hosts: serverb
become: no
tasks:
- name: Test web server
url:
url: http://servera
return_content: yes
status_code: 200

3.6 实战三:Ansible 复制远程服务器文件到本地

---
- name: copy file
hosts: webserver
become: true
tasks:
- name:
fetch:
src: /etc/fstab
dest: /testdir/ansible/

 

本文摘抄或总结其他笔记,笔记不涉及任何商业用途,如果侵权请及时联系处理
posted @ 2021-08-04 15:58  低调的神  阅读(183)  评论(0编辑  收藏  举报