前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >04-初识PlayBook

04-初识PlayBook

原创
作者头像
小朋友呢
修改2020-01-14 17:50:14
6600
修改2020-01-14 17:50:14
举报

playbook

什么是playbook

中文名(剧本),它是一个自动化处理脚本,使用yaml语言来表示。

快速入门

当前inventory文件

代码语言:txt
复制
[student@workstation ansible]$ cat inventory 
[dev]
servera

[test]
serverb

[prod]
serverc
serverd

[balancers]
serverb

[webservers:children]
prod

编写yaml剧本

代码语言:txt
复制
[student@workstation ansible]$ vim 1-config_yum.yaml 
  #hosts表示要执行剧本的主机
- hosts: all
  #remote_user表示远程执行的身份
  remote_user: root
  #tasks是任务列表的意思,每一个任务后面的name是当前任务的描述
  tasks:
  - name: 1.remove old yum
    shell: rm -rf /etc/yum.repos.d/
  
  - name: 2.copy new yum config to host
    copy: src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/
 
  - name: 3.clean yum cache
    shell: yum clean all

检测语法

代码语言:txt
复制
[student@workstation ansible]$ ansible-playbook --syntax-check 1-config_yum.yaml 

playbook: 1-config_yum.yaml

模拟执行

代码语言:txt
复制
[student@workstation ansible]$ ansible-playbook -C 1-config_yum.yaml 
PLAY [all] **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [serverc]
ok: [serverd]
ok: [serverb]
ok: [servera]

TASK [1.remove old yum] **************************************************************************************************
skipping: [servera]
skipping: [serverc]
skipping: [serverb]
skipping: [serverd]

TASK [2.copy new yum config to host] **************************************************************************************************
ok: [servera]
ok: [serverd]
ok: [serverb]
ok: [serverc]

TASK [3.clean yum cache] **************************************************************************************************
skipping: [serverc]
skipping: [serverb]
skipping: [servera]
skipping: [serverd]

PLAY RECAP **************************************************************************************************
servera                    : ok=2    changed=0    unreachable=0    failed=0   
serverb                    : ok=2    changed=0    unreachable=0    failed=0   
serverc                    : ok=2    changed=0    unreachable=0    failed=0   
serverd                    : ok=2    changed=0    unreachable=0    failed=0  

真实执行

代码语言:txt
复制
[student@workstation ansible]$ ansible-playbook  1-config_yum.yaml 
PLAY [all] **************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [serverd]
ok: [serverb]
ok: [serverc]
ok: [servera]

TASK [1.remove old yum] **************************************************************************************************
 [WARNING]: Consider using file module with state=absent rather than running rm

changed: [serverc]
changed: [serverb]
changed: [servera]
changed: [serverd]

TASK [2.copy new yum config to host] **************************************************************************************************
changed: [servera]
changed: [serverd]
changed: [serverc]
changed: [serverb]

TASK [3.clean yum cache] **************************************************************************************************
 [WARNING]: Consider using yum module rather than running yum

changed: [serverb]
changed: [servera]
changed: [serverc]
changed: [serverd]

PLAY RECAP **************************************************************************************************
servera                    : ok=4    changed=3    unreachable=0    failed=0   
serverb                    : ok=4    changed=3    unreachable=0    failed=0   
serverc                    : ok=4    changed=3    unreachable=0    failed=0   
serverd                    : ok=4    changed=3    unreachable=0    failed=0 

用法介绍

剧本文件名可以是.yml或者.yaml结尾

里面的内容只能以空格缩进

可以配置vim方便编写剧本

代码语言:txt
复制
[student@workstation ansible]$ head -1 /etc/vimrc 
set nu ai ts=2 sw=2 cursorcolumn cursorline

语法解析

yaml语法

代码语言:txt
复制
--- #代表文件开始
....
…#文件结尾,可省略

列表

代码语言:txt
复制
- play1

- play2

- play3

tasks下面的每一个列表就是一个任务

键值对

key: value

示例

代码语言:txt
复制
id: 1
name: xiaoniu
passwd: 123456

playbook执行

代码语言:txt
复制
检测语法:ansible-playbook --syntax-check config_yum.yaml
模拟执行:ansible-playbook -C config_yum.yaml
真实执行:ansible-playbook config_yum.yaml

playbook提权

剧本中提权

可以写在剧本中,与hosts,tasks同级

代码语言:txt
复制
- hosts: all
  remote_user: devops
  become: true
  become_method: sudo
  become_user: root
  
  tasks:
  - debug:
     msg: "Test"

配置文件提权

也可以提前写在配置文件中

代码语言:txt
复制
[defaults]
inventory  = /home/student/ansible/inventory
remote_user = devops

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

命令行提权

命令参数

--become,-b

使用提权

--ask-become-pass,-K

询问提权用户的密码

--become-method

提权方式

--become-user

提升权限的用户

举例
代码语言:txt
复制
[student@workstation ansible]$ ansible -b -K --become-method=sudo --become-user=root all -m ping
SUDO password:
servera | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
serverc | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
serverb | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
serverd | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

多行字符串

两种方式: “ |”和“ >”,表明多行字符串是一个整体。

“ >”会把换行符替换成空格

“ |”表明多行字符串是一个整体。

代码语言:txt
复制
[student@workstation ansible]$ cat str.yml 
- hosts: all
  tasks:
  - debug:
     msg: >
      This is one line
      This is two line
  
  - debug:
     msg: |
      Example one line
      Example two line

查看结果

代码语言:txt
复制
TASK [debug] **************************************************************************************************
ok: [servera] => {
    "msg": "This is one line This is two line\n"
}
ok: [serverc] => {
    "msg": "This is one line This is two line\n"
}
ok: [serverb] => {
    "msg": "This is one line This is two line\n"
}
ok: [serverd] => {
    "msg": "This is one line This is two line\n"
}

TASK [debug] **************************************************************************************************
ok: [serverb] => {
    "msg": "Example one line\nExample two line\n"
}
ok: [servera] => {
    "msg": "Example one line\nExample two line\n"
}
ok: [serverc] => {
    "msg": "Example one line\nExample two line\n"
}
ok: [serverd] => {
    "msg": "Example one line\nExample two line\n"

playbook书写风格

比如上面那个例子中,就是shorthand格式

代码语言:txt
复制
- name: 2.copy new yum config to host
   copy: src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/

正常yaml格式

代码语言:txt
复制
- name: 2.copy new yum config to host
   copy: 
    src: /etc/yum.repos.d/ 
    dest: /etc/yum.repos.d/

template模块

启动一个http服务

代码语言:yaml
复制
- hosts: all
  vars:
  - web_root: /var/www/html
  - srv_name: www.example.com
  - http_port: 9090

  tasks:
  - name: copy configfile to host
	#src是本机模板文件,dest是目标主机的文件
    template: src=/playbook/myweb.conf dest=/etc/httpd/conf.d/myweb.conf
 
  - name: make web index
	shell: echo "Ansible Genareic" > {{ web_root }}/index.html

  - name: stop selinux firewalld
	shell: setenforce 0 && iptables –F
	
	#忽略错误继续执行
	ignore_errors: yes

  - name: start httpd
    service: name=httpd state=restarted 

查看模板文件

代码语言:txt
复制
[root@ansible playbook]# cat /playbook/myweb.conf
<VirtualHost {{ ansible_default_ipv4.address }}:{{ http_port }}>
	DocumentRoot {{ web_root }}
	ServerName {{ srv_name }}
	
	<Directory {{ web_root }}>
		Require all granted
	</Directory>
</Virtualhost>

yum_repository模块

name

仓库的文件名称

baseurl

yum源地址

description

描述信息

file

仓库文件前缀,默认是以name参数作为前缀,同一个.repo配置文件 中可以存在多个源

enabled

(yes|no) 是否开户yum源

gpgcheck

(yes|no) 是否启用包验证

gpgkey

公钥的位置,当gpgcheck=yes时生效

state

present 添加 absent 删除,默认present

使用playbook方式来添加yum源

代码语言:yaml
复制
- hosts: myhosts
  remote_user: root

  tasks:
  - name: config yum
    yum_repository: name=dvd baseurl=file:///media description=dvd enabled=yes gpgcheck=no

删除远程主机的dvd yum源

代码语言:txt
复制
ansible myhosts -m yum_repository -a 'name=dvd state=absent'

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • playbook
    • 什么是playbook
      • 快速入门
        • 用法介绍
          • 语法解析
            • yaml语法
            • 列表
            • 键值对
          • playbook执行
            • playbook提权
              • 剧本中提权
              • 配置文件提权
              • 命令行提权
            • 多行字符串
              • playbook书写风格
                • template模块
                  • yum_repository模块
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档