Ansible02-实施playbook

一、编写和运行playbook

1.1、编写playbook

play 是针对清单中选定的主机运行的一组有序任务。playbook 是一个文本文件,其中包含由一个或多个按特定顺序运行的 play 组成的列表。

playbook 是以 YAML 格式编写的文本文件,通常使用扩展名 .yml 保存。playbook 使用空格字符缩进来表示其数据结构。 YAML 对用于缩进的空格数量没有严格的要求,但有两个基本的规则。这点很像python。

  • 处于层次结构中同一级别的必须具有相同的缩进量。
  • 如果项目属于其他项目的子项,其缩进量必须大于父项。

必须使用空格字符进行缩进,而不能使用制表符。使用 vim 编辑器,可以在 $HOME/.vimrc 文件填写下面的内容,检查到编辑 YAML文件时,按 Tab 时会自动缩进两个空格,并且自动缩进后续行。

" ai=autoindent ts=tabspace sw=shiftwidth et=expandtab
autocmd FileType yaml setlocal ai ts=2 sw=2 et

playbook以 --- 开头,以 ... 结尾(一般省略结尾的...)。中间一般包含name、hosts、tasks三个部分。play 本身是一个键值对集合。同一play 中的键应当使用相同的缩进量。

  • name 将一个任意字符串作为标签与该 play 关联,识了该 play 的用途。
  • hosts 属性,它指定对其运行 play 中的任务的主机。
  • tasks 属性,其值指定要为该 play 运行的任务的列表。

比如,编写名为 web.yml 的 playbook,里面添2个play。第一个 play 对 webservers 组部署 httpd,第二个 play 对 dev 组部署 mariadb-server。然后启动服务并设置为开机自动启动。设置防火墙允许访问这两个服务。

[student@workstation ansible]$ vim cat.yml

---
- name: Deploy httpd
  hosts: webservers
  tasks:
    - name: Install httpd
      yum:
        name: httpd
        state: present

    - name: Start httpd
      service:
        name: httpd
        state: started
        enabled: yes

    - name: Permit http
      firewalld:
        service: http
        state: enabled
        permanent: yes
        immediate: yes

    - name: Create /var/www/html/index.html
      copy:
        content: "Added by ansible.\n"
        dest: /var/www/html/index.html


- name: Deploy mariadb-server
  hosts: dev
  tasks:
    - name: Install mariadb-server
      yum:
        name: mariadb-server
        state: present

    - name: Start mariadb
      service:
        name: mariadb
        state: started
        enabled: yes

    - name: Permit mysql
      firewalld:
        service: mysql
        state: enabled
        permanent: yes
        immediate: yes

1.2、运行playbook

运行 playbook 用命令 ansible-play 。用 --syntax-check 对playbook进行语法检查。用 --check 对playbook进行空运行。用 -v、-vv、-vvv、-vvvv 提高输出信息详细度,v越多越详细。确认无误以后再进行执行。执行结束后进行验证。

因为ansible是幂等的,所以已经处于正确状态的内容不会重复执行,因此playbook可以安全的多次执行。

[student@workstation ansible]$ ansible-playbook web.yml --syntax-check

playbook: web.yml

[student@workstation ansible]$ ansible-playbook web.yml --check
......
[student@workstation ansible]$ ansible-playbook web.yml 
......
[student@workstation ansible]$ curl serverc
Added by ansible.
[student@workstation ansible]$ curl serverd
Added by ansible.
[student@workstation ansible]$ telnet servera 3306
Trying 192.168.27.133...
Connected to servera.
Escape character is '^]'.

或者我们再写一个名为 web_test.yml 的 playbook,来检查一下。

[student@workstation ansible]$ vim web_test.yml 
---
- name: Test web page
  hosts: webservers
  tasks:
    - name: Access url get http200
      uri:
        url: http://serverc.lab.example.com/
        status_code: 200
        return_content: yes
      register: web_content

    - name: Show web page
      debug:
        var: web_content.content

- name: Test mariadb
  hosts: dev
  tasks:
    - name: Show databases
      shell: 'mysql -uroot -e "show databases;"'
      register: db_content

    - name: Show databases result
      debug:
        var: db_content.stdout_lines

二、playbook或YAML的一些简单语法

2.1、注释

注释采用 # 开头的一行。

# This a comment.

2.2、字符串

YAML 字符串用 'string' 或 "string" 表示。YAML 格式允许定义多行字符串。使用竖线运算符 (|) 保留换行符(打印出来有换行符),或使用大于运算符 (>) 来将换行符换为空格(打印出来没有换行符,被替换成了空格)。

[student@workstation ansible]$ cat copylines.yml 

---
- name: Copy 1-3 line
  hosts: dev
  tasks:
    - name: One line
      copy:
        content: >
          Ansible is agentless.
          Ansible manage hosts via ssh.
          Ansible is easy to use.
        dest: /tmp/line1.txt

    - name: Three lines
      copy:
        content: |
          Ansible is agentless.
          Ansible manage hosts via ssh.
          Ansible is easy to use.
        dest: /tmp/line3.txt

检查结果

[student@workstation ansible]$ ansible dev -a "cat /tmp/line1.txt"
servera.lab.example.com | CHANGED | rc=0 >>
Ansible is agentless. Ansible manage hosts via ssh. Ansible is easy to use.

[student@workstation ansible]$ ansible dev -a "cat /tmp/line3.txt"
servera.lab.example.com | CHANGED | rc=0 >>
Ansible is agentless.
Ansible manage hosts via ssh.
Ansible is easy to use.

2.3、YAML字典

YAML字典的两种格式,第一种比较常用也好写。注意键值对之间冒号后面有空格,否则是语法错误。

web_pkg: httpd
web_service: httpd
web_rule: http

db_pkg: mariadb-server
db_service: mariadb
db_rule: mysql
{web_pkg: httpd, web_service: httpd, web_rule: http, db_pkg: mariadb-server, db_service: mariadb, db_rule: mysql}

2.4 、YAML列表

YAML列表举例,db_list.yml列表文件里存放了一个名为 databases 的列表,列表有3个元素,每个元素是一个YAML字典,记录了数据库名和数据库用户名。

[student@workstation ansible]$ cat db_list.yml 

---
databases:
  - name: discuss_db
    user: discuss_user
  - name: foo_db
    user: foo_db
  - name: wordpress_db
    user: wordpress_user

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦行僧(csdn)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值