使用Ansible 中的PlayBook
PlayBook的功能
PlayBook是由一个或多个play组成的列表.
PlayBook文件使用YAML来书写.
YAML
简介
YAML是一种表达资料序列的格式,类似XML.
Yet Another Markup Language
2001年首次发表
官网为 www.yaml.org
特点
可读性强
与脚本语言交互性好
可扩展性强
易于实现
使用程序执行流树立方式
有严格的缩进
语法简介
在文件中用---
开始
在文件中用...
结束
次行一般书写文件内容
缩进严格且大小写敏感
Key/Value
可以多行书写也可以一行书写
Value
可以是字符串也可以是List
一个Play
需要包括Name
和Tasks
Name
是描述,Tasks
是动作
一个Name
只能包含一个Task
扩展名为yml
或者yaml
YAML列表
[Linux,C++,Java,Python]
- Linux
- C++
- Java
- Python
YAML的字典
字典用于存放键值
name:Test
age:12
jobs:linux
{name: "Test", age: "12", jobs: "linux"}
##两种表示方式均可
PlayBook执行命令
ansible-playbook XXX.yml ...
##常用参数
--check | -C ##检测
--syntax-check ##语法检测
--list-hosts ##列出hosts
--list-tasks ##列出tasks
--list-tags ##列出tag
--limit ##手动指定执行主机
-v | -vv | -vvv ##显示实现过程
##syntax-check只会检测语法不会预执行,check预检测时会执行
PlayBook的核心组件
name ##可选,建议使用
hosts ##受控主机列表
tasks ##任务
如何区分字典和列表
在PlayBook
文件中,列表元素一定以-
开头,而字典元素前一定没有-
.
基本范例
##查询主机名并输出最后一行用户信息
##Test.yml内容
---
- name: Test
hosts: 172.25.254.137
tasks:
- name: Hostname Check
shell:
hostname
- name: UID Check
shell:
id
- name: cat the last user
shell:
tail -n 1 /etc/passwd
##测试执行操作
[root@Node1 .ansible]# ansible-playbook -C Test.yaml
PLAY [Test] ********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.25.254.137]
TASK [Hostname Check] **********************************************************
skipping: [172.25.254.137]
TASK [UID Check] ***************************************************************
skipping: [172.25.254.137]
TASK [cat the last user] *******************************************************
skipping: [172.25.254.137]
PLAY RECAP *********************************************************************
172.25.254.137 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
[root@Node1 .ansible]# ansible-playbook Test.yaml -v
Using /root/.ansible/ansible.cfg as config file
PLAY [Test] ********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.25.254.137]
TASK [Hostname Check] **********************************************************
changed: [172.25.254.137] => {"changed": true, "cmd": "hostname", "delta": "0:00:00.006929", "end": "2021-03-19 09:44:05.444761", "rc": 0, "start": "2021-03-19 09:44:05.437832", "stderr": "", "stderr_lines": [], "stdout": "Node2.westos.org", "stdout_lines": ["Node2.westos.org"]}
TASK [UID Check] ***************************************************************
changed: [172.25.254.137] => {"changed": true, "cmd": "id", "delta": "0:00:00.006508", "end": "2021-03-19 09:44:06.153462", "rc": 0, "start": "2021-03-19 09:44:06.146954", "stderr": "", "stderr_lines": [], "stdout": "uid=0(root) gid=0(root) groups=0(root)", "stdout_lines": ["uid=0(root) gid=0(root) groups=0(root)"]}
TASK [cat the last user] *******************************************************
changed: [172.25.254.137] => {"changed": true, "cmd": "tail -n 1 /etc/passwd", "delta": "0:00:00.010543", "end": "2021-03-19 09:44:06.875592", "rc": 0, "start": "2021-03-19 09:44:06.865049", "stderr": "", "stderr_lines": [], "stdout": "dhcpd:x:177:177:DHCP server:/:/sbin/nologin", "stdout_lines": ["dhcpd:x:177:177:DHCP server:/:/sbin/nologin"]}
PLAY RECAP *********************************************************************
172.25.254.137 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
##安装DNS并配置服务端
##能够分别解析www.westos.org和bbs.westos.org
---
- name: DNS Setup
hosts: 172.25.254.137
tasks:
- name: Install Bind
dnf:
name: bind
state: latest
- name: Firewalld Setup
firewalld:
zone: public
service: dns
permanent: yes
state: enabled
immediate: yes
- name: Copy /etc/named.conf
copy:
src: /root/.ansible/named.conf
dest: /etc/named.conf
owner: root
group: named
mode: 0640
backup: yes
- name: Copy /etc/named.rfc1912.zones
copy:
src: /root/.ansible/named.rfc1912.zones
dest: /etc/named.rfc1912.zones
owner: root
group: named
mode: 0640
backup: yes
- name: Copy /var/named/westos.org.zone
copy:
src: /root/.ansible/westos.org.zone
dest: /var/named/westos.org.zone
owner: root
group: named
mode: 0640
backup: yes
- name: Start DNS Server
service:
name: named
state: started
enabled: yes
...
##安装Vsftpd,并授权匿名用户登录
##在/var/ftp/pub下放置测试文件,方便进行测试
---
- name: Vsftpd Server
hosts: 172.25.254.137
tasks:
- name: Install Vsftpd
dnf:
name: vsftpd.x86_64
state: latest
- name: FirewallD Config
firewalld:
zone: public
service: ftp
permanent: yes
state: enabled
immediate: yes
- name: Inert File
lineinfile:
path: /var/ftp/pub/TestFile
line: Test File
create: yes
- name: chmod & chgrp
file:
path: /var/ftp/pub/
mode: 775
group: ftp
state: directory
recurse: yes
- name: Vsftp Anon Config
lineinfile:
path: /etc/vsftpd/vsftpd.conf
regexp: anonymous_enable=*
line: anonymous_enable=YES
create: no
- name: Start Vsftpd
service:
name: vsftpd
state: started
enabled: yes
...