概述
Ansible playbook的使用
1.playbook的介绍
- playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式。值得注意的是playbook是通过YAML格式来进行描述定义的。
2.playbook基础组件
- Hosts:运行执行任务(task)的目标主机
- remote_user:在远程主机上执行任务的用户
- tasks:任务列表
- handlers:任务,与tasks不同的是只有在接受到通知时才会被触发
- templates:使用模板语言的文本文件
- variables:变量,变量替换
3.YAML语法
- 使用缩进来表示层级;
- 只能使用空格缩进,禁止使用Tab缩进;
- 对字母大小写敏感;
- 可以使用—来标记开头,使用…来标记结尾;
- 使用#表示注释内容;
- 字符串可以不用引号标注;
- 针对不同数据类型有特定标记模型(后面会用实例演示)
4.playbook实例
//使用playbook创建一个用户 指定他的uid是2002
[root@localhost ~]#mkdir playbook
[root@localhost opt]# ls
ansible.cfg
inventory
playbook
[root@localhost opt]# cd playbook/
[root@localhost playbook]# touch user.yml
[root@localhost playbook]# ls
user.yml
[root@localhost playbook]# vim user.yml
---
- hosts: httpd
tasks:
- name: create user xym
user:
name: xym
uid: 2002
state: present
//运行playbook
[root@localhost opt]# ansible-playbook playbook/user.yml
PLAY [httpd] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.147]
TASK [create user xym] *********************************************************
changed: [192.168.200.147]
PLAY RECAP *********************************************************************
192.168.200.147
: ok=2
changed=1
unreachable=0
failed=0
skipped=0
rescued=0
ignored=0
//受控主机查看
[root@www ~]# id xym
uid=2002(xym) gid=2002(xym) 组=2002(xym)
//部署多个tasks实例 安装一个autofs
[root@localhost playbook]# vim
auto.yml
---
- hosts: httpd
tasks:
- name: install autofs
yum:
name: autofs
state: present
- name: enabled autofs
service:
name: autofs
state: started
enabled: yes
//运行
[root@localhost opt]# ansible-playbook playbook/auto.yml
PLAY [httpd] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.147]
TASK [install autofs] **********************************************************
ok: [192.168.200.147]
TASK [enabled autofs] **********************************************************
changed: [192.168.200.147]
PLAY RECAP *********************************************************************
192.168.200.147
: ok=3
changed=1
unreachable=0
failed=0
skipped=0
rescued=0
ignored=0
5.语法验证
//语法验证成功
[root@localhost opt]# ansible-playbook --syntax-check playbook/auto.yml
playbook: playbook/auto.yml
//语法验证失败
[root@localhost opt]# ansible-playbook --syntax-check playbook/auto.yml
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
did not find expected key
The error appears to be in '/opt/playbook/auto.yml': line 9, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: enabled autofs
^ here
6.执行空运行
//可以使用-C选项对playbook执行空运行。这会使Ansible报告在执行该playbook时将会发生什么更改,但不会对受管主机进行任何实际的更改。
[root@localhost opt]#ansible-playbook -C playbook/httpd.yml
7.实施多个play
[root@localhost playbook]# vim test.yml
---
- hosts: httpd
tasks:
- name: create user qwer uid 4567
user:
name: qwer
uid: 4567
state: present
- hosts: mysql
tasks:
- name: create user lol uid 6789
user:
name: lol
uid: 6789
state: present
//
[root@localhost opt]# ansible-playbook playbook/test.yml
PLAY [httpd] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.147]
TASK [create user qwer uid 4567] ***********************************************
changed: [192.168.200.147]
PLAY [mysql] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.145]
TASK [create user lol uid 6789] ************************************************
changed: [192.168.200.145]
PLAY RECAP *********************************************************************
192.168.200.145
: ok=2
changed=1
unreachable=0
failed=0
skipped=0
rescued=0
ignored=0
192.168.200.147
: ok=2
changed=1
unreachable=0
failed=0
skipped=0
rescued=0
ignored=0
8.play中的远程用户和特权升级
//指定用户
remote_user: remoteuser
//升级特殊权限
become: true
//实例
[root@localhost playbook]# vim test.yml
---
- hosts: php
remote_user: xym
become: yes
tasks:
- name:
lineinfile:
path: /etc/hosts
line: '192.168.200.146 www.php.com'
state: present
//运行
[root@localhost opt]# ansible-playbook playbook/test.yml
PLAY [php] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.146]
TASK [lineinfile] **************************************************************
changed: [192.168.200.146]
PLAY RECAP *********************************************************************
192.168.200.146
: ok=2
changed=1
unreachable=0
failed=0
skipped=0
rescued=0
ignored=0
//查看受控主机
[root@php ~]# visudo
[root@php ~]# cat /etc/hosts
127.0.0.1
localhost localhost.localdomain localhost4 localhost4.localdomain4
::1
localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.146 www.php.com
9.playbook语法变化
YAML注释
注释也可以用于提高可读性。在YAML中,编号或井号字符(#)右侧的所有内容都是注释。如果注释的左侧有内容,请在该编号符号的前面加一个空格。
# This is a YAML comment
some data # This is also a YAML comment
YAML字符串
YAML中的字符串通常不需要放在引号里,即使字符串中包含空格。字符串可以用双引号或单引号括起。
this is a string
'this is another string'
"this is yet another a string"
编写多行字符串有两种方式。可以使用管道符表示要保留字符串中的换行字符。
include_newlines: |
Example Company
123 Main Street
Atlanta, GA 30303
要编写多行字符串,还可以使用大于号字符来表示换行字符转换成空格并且行内的引导空白将被删除。这种方法通常用于将很长的字符串在空格字符处断行,使它们跨占多行来提高可读性。
fold_newlines: >
This is an example
of a long string,
that will become
a single sentence once folded.
最后
以上就是玩命楼房为你收集整理的Ansible playbook的使用的全部内容,希望文章能够帮你解决Ansible playbook的使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复