playbook由YMAL语言编写,以下为playbook常用到的YMAL格式:
---
# 一位职工的记录
name: Example Developer
job: Developer
skill: Elite
---
# 一位职工的记录
{name: Example Developer, job: Developer, skill: Elite}
Playbook主要有以下四部分构成:
而Playbook对应的目录层有五个,分别如下:
一般所需的目录层有:(视情况可变化)
yaml文件的格式对空格是很敏感的,一般需要顶格书写,段落划分为两个空格,为了方便我们编写.yml文件,我们可以作如下设置实现在vim一次tab两个空格:
vim .vimrc #编辑文件内容如下:
autocmd FileType yaml setlocal ai ts=2 sw=2 et 文件类型yaml
---
- hosts: server2 #指定主机组,可以是一个或多个组,逗号分隔。
remote_user: root #指定远程主机执行的用户名
---
- hosts: webservers
remote_user: root
become: yes #切换用户运行
become_user: mysql #指定sudo用户为mysql
tasks:
- name: install apache #定义任务名
定义一个task,常见的格式:”module: options” 例如:yum: name=httpd
tasks:
- name: "安装apache软件"
yum: name=httpd state=present #调用yum模块
也可以写成以下格式:
- name: "启动apache服务"
service:
name=httpd
state=started
tasks:
- name: disable selinux
command: /sbin/setenforce 0
使用 command 和 shell 时,我们需要关心返回码信息:
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True #返回值如果不为0,就会报错,tasks停止
ansible-playbook apache.yml
ansible-playbook apache.yml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook apache.yml --list-task #检查tasks任务
ansible-playbook apache.yml --list-hosts #检查生效的主机
ansible-playbook a.yml --start-at-task="启动apache服务" #指定从某个task开始运行
[devops@server1 ansible]$ vim ~/ansible/playbook.yml
---
- hosts: prod
tasks:
- name: install httpd #安装服务
yum:
name: httpd
state: present
- name: start httpd #开启服务
service:
name: httpd
state: started
[devops@server1 ansible]$ ansible-playbook playbook.yml --syntax-check #检测yaml文件的语法是否正确
[devops@server1 ansible]$ ansible-playbook playbook.yml --list-task #检测tasks任务
[devops@server1 ansible]$ ansible-playbook playbook.yml --list-hosts #检测生效的主机
ansible-playbook a.yml --start-at-task="启动apache服务" #指定从某个task开始运行
[devops@server1 ansible]$ ansible-playbook playbook.yml
[root@server3 ~]# rpm -q --scripts httpd #查看安装过程以及安装前后所执行的所有脚本
[root@server3 ~]# rpm -qi httpd #-q查询 -i安装
Handlers: 在发生改变时执行的操作
[devops@server1 ansible]$ ls
ansible.cfg inventory playbook.yml
[devops@server1 ansible]$ mkdir files
[devops@server1 ansible]$ cd files/
[devops@server1 files]$ scp server3:/etc/httpd/conf/httpd.conf .
[devops@server1 ansible]$ vim files/httpd.conf
42 Listen 8080
---
- hosts: prod
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: configure httpd #配置apache,拷贝本机的httpd.conf文件到prod组中主机的指定位置
copy:
src: files/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root #文件所有人,所有组,权限。
group: root
mode: 644
notify: restart httpd #修改完后调用触发器重启服务。
- name: start httpd
service:
name: httpd
state: started
handlers: #定义重启服务的触发器,name和上面notify的内容一致才生效。
- name: restart httpd
service:
name: httpd
state: restarted
[devops@server1 ansible]$ ansible-playbook playbook.yml --syntax-check
[devops@server1 ansible]$ ansible-playbook playbook.yml
[root@server3 ~]# vim /etc/httpd/conf/httpd.conf
[root@server3 ~]# netstat -antlp
注意:做完实验记得将端口改回80。
[root@server2 ~]# cd /home/devops/
[root@server2 devops]# cd .ssh/
[root@server2 .ssh]# ls
authorized_keys
[devops@server1 ~]$ cd .ssh/
[devops@server1 .ssh]$ ls
id_rsa id_rsa.pub known_hosts
[devops@server1 .ssh]$ cp id_rsa.pub authorized_keys
[devops@server1 .ssh]$ ll
[devops@server1 ansible]$ ansible-doc firewalld
[devops@server1 ansible]$ echo liuhaoran > files/index.html #文件内容:刘昊然
[devops@server1 ansible]$ vim playbook.yml
---
- hosts: webserver #针对的组为webserver(包含server2,3)
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy index.html
copy:
src: files/index.html
dest: /var/www/html/index.html #将本机的index.html文件copy到远程主机
- name: configure httpd
copy:
src: files/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}" #写一个循环,开启httpd和firewalld两个服务。
state: started
loop:
- httpd
- firewalld
- name: configure firewalld #配置防火墙
firewalld:
service: http #允许的服务有http
permanent: yes #永久加入列表
immediate: yes #立即生效
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: localhost
become: no #不切换
tasks:
- name: test httpd
uri:
url: http://172.25.24.3
status_code: 200 #返回状态码200ok
[devops@server1 ansible]$ ansible-playbook playbook.yml --syntax-check
[devops@server1 ansible]$ ansible-playbook playbook.yml
[devops@server1 ansible]$ curl server3
liuhaoran
[devops@server1 ansible]$ curl server2
liuhaoran
Template生成目标文件,copy无法对配置文件进行修改。
[webservers]
server2 http_ip=172.25.0.2
vim httpd.conf.j2
Listen {{ http_ip }}:{{ httpd_port }}
[devops@server1 ansible]$ ansible-doc template
[devops@server1 ansible]$ mkdir template
[devops@server1 ansible]$ cd template/
[devops@server1 template]$ cp ../files/httpd.conf .
[devops@server1 template]$ mv httpd.conf httpd.conf.j2
httpd.conf.j2
,修改端口为变量的形式。[devops@server1 template]$ vim httpd.conf.j2
42 Listen {{ http_port }}
---
- hosts: webserver
vars:
http_port: 80
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy index.html
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: configure httpd
template: #template模块
src: template/httpd.conf.j2 #注意路径
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: configure firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: localhost
become: no
tasks:
- name: test httpd
uri:
url: http://172.25.24.3
status_code: 200
[root@server2 .ssh]# vim /etc/httpd/conf/httpd.conf
[root@server2 .ssh]# netstat -antlp
[root@server3 ~]# vim /etc/httpd/conf/httpd.conf
[root@server3 ~]# netstat -antlp
[devops@server1 ansible]$ vim template/httpd.conf.j2
42 Listen {{ http_host}}:{{ http_port }}
[devops@server1 ansible]$ vim inventory
localhost
[test]
server2 http_host=172.25.24.2
[prod]
server3 http_host=172.25.24.3
[webserver:children]
test
prod
[devops@server1 ansible]$ ansible-playbook playbook.yml
。[root@server2 .ssh]# vim /etc/httpd/conf/httpd.conf
[root@server3 ~]# vim /etc/httpd/conf/httpd.conf
[devops@server1 ansible]$ ansible test -m setup #查看test组中主机预留的保留字
[devops@server1 ansible]$ vim template/file.j2
主机名: {{ ansible_facts['hostname'] }}
主机IP: {{ ansible_facts['default_ipv4']['address'] }}
主机DNS: {{ ansible_facts['dns']['nameservers'][-1] }} #取索引
boot分区: {{ ansible_facts['devices']['vda']['partitions']['vda1']['size'] }}
内核: {{ ansible_facts['kernel'] }}
内存空闲: {{ ansible_facts['memfree_mb'] }}
[devops@server1 ansible]$ vim file.yml
---
- hosts: all #所有主机
tasks:
- name: create file
template:
src: template/file.j2
dest: /mnt/file #采集到目标主机的/mnt/file文件中
[devops@server1 ansible]$ vim inventory
[test]
server2 http_host=172.25.24.2
[prod]
server3 http_host=172.25.24.3
[webserver:children]
test
prod
[devops@server1 ansible]$ ansible-playbook file.yml