1.playbook简介

  • playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式。值得注意的是playbook是通过YAML格式来进行描述定义的。
  • 核心元素:

Tasks:任务,由模板定义的操作列表
Variables:变量
Templates:模板,即使用模板语法的文件
Handlers:处理器 ,当某条件满足时,触发执行的操作
Roles:角色

  • 在playbook中的每一个play都可以选择在哪些服务器和以什么用户完成,hosts一行可以是一个主机组、主机、多个主机,中间以冒号分隔,可使用通配模式。其中remote_user表示执行的用户账号。

  • hosts: abc #指定主机组,可以是一个或多个组
    remote_user: root #指定远程主机执行的用户名
  • 指定远程主机sudo切换用

#vim ping.yml

- hosts: abc
remote_user: root
become: yes #2.6版本以后的参数,之前是sudo,意思为切换用户运行
become_user: mysql #指定sudo用户为mysql
执行playbook
# ansible-playbook ping.yml -K

  • Tasks list 和action介绍

1:Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始第二个任务。
在运行playbook时(从上到下执行),如果一个host执行task失败,整个tasks都会回滚,请修正playbook 中的错误,然后重新执行即可。
Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量,模块执行时幂等的,这意味着多次执行是安全的,因为其结果一致。
2:每一个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中可以很好的辨别出是属于哪一个task的。如果没有定义name,‘action’的值将会用作输出信息中标记特定的task。
3:定义一个task,常见的格式:”module: options” 例如:yum: name=httpd
4:ansible的自带模块中,command模块和shell模块无需使用key=value格式

  • 常用命令
  • ansible-playbook [yaml文件名、也可以yml结尾]
  • 例如:ansible-playbook a.yml
  • 参数:

-k(–ask-pass) 用来交互输入ssh密码
-K(-ask-become-pass) 用来交互输入sudo密码
-u 指定用户

#ansible-playbook a.yml --syntax-check #检查yaml文件的语法是否正确
#ansible-playbook a.yml --list-task #检查tasks任务
#ansible-playbook a.yml --list-hosts #检查生效的主机
#ansible-playbook a.yml --start-at-task=‘Copy Nginx.conf’ #指定从某个task开始运行

2、编写playbook实现自动安装服务

1、yml文件语法的要求是python语法,非常的严格,为了方便我们的使用,我们先编写一个特定的vim,一个tab等于两个空格

[devopes@server1 ansible]$ vim .vimrc
[devopes@server1 ansible]$ cat .vimrc
autocmd FileType yaml setlocal ai ts=2 sw=2 et


2、开始编写yml文件

[devopes@server4 ansible]$ pwd
/home/devopes
[devopes@server4 ansible]$ ls
ansible
[devopes@server4 ansible]$ cd ansible/
[devopes@server4 ansible]$ ls
ansible.cfg inventory
[devopes@server4 ansible]$ vim playbook.yml
[devopes@server4 ansible]$ cat playbook.yml
---
- hosts: prodtasks:- name: install httpdyum:name: httpdstate: present- name: start httpdservice:name: httpdstate: started


3、检测语法

[devopes@server4 ansible]$ ansible-playbook playbook.yml --syntax-check     #检查yaml文件的语法是否正确
playbook: playbook.yml
[devopes@server4 ansible]$ ansible-playbook playbook.yml --list-task   #检查tasks任务
playbook: playbook.ymlplay #1 (prod): prod  TAGS: []tasks:install httpd TAGS: []start httpd TAGS: []
[devopes@server4 ansible]$ ansible-playbook playbook.yml --list-hosts   #检查生效的主机
playbook: playbook.ymlplay #4 (prod): prod  TAGS: []pattern: [u'prod']hosts (1):172.25.35.6
[devopes@server1 ansible]$ ansible-playbook playbook.yml



4、在server6上查看
方法一:

方法二:

3、编写playbook实现修改配置文件,并且加入触发器

1、编辑playbook.yml文件

[devopes@server1 ansible]$ vim playbook.yml
[devopes@server1 ansible]$ cat playbook.yml
---
- hosts: prodtasks:- name: install httpdyum:name: httpdstate: present- name: configure httpdcopy:src: files/httpd.confdest: /etc/httpd/conf/httpd.confowner: rootgroup: rootmode: 644notify: restart httpd- name: start  httpdservice:name: httpdstate: startedhandlers:- name: restart httpdservice:name: httpdstate: restarted


2、创建目录并拷贝.conf文件

[devopes@server1 ansible]$ ls
ansibe.cfg   inventory   playbook.yml
[devopes@server1 ansible]$ mkdir files
[devopes@server1 ansible]$ ls
[devopes@server1 ansible]$ cd files/
[devopes@server1 files]$  ls
[devopes@server1 files]$ scp server6:/etc/httpd/conf/httpd.conf .
[devopes@server1 files]$ ls
[devopes@server1 files]$ cd ..
[devopes@server1 ansible]$ ls


3、执行.yml文件

[devopes@server1 ansible]$ ansible-playbook playbook.yml


4、查看端口,发现80端口生效

[root@server6 ~]# netstat -antlp


5、为了再次验证,我们将httpd中的80端口更改为8080

[devopes@server4 ansible]$ ls
ansible.cfg  files  inventory  playbook.retry  playbook.yml
[devopes@server4 ansible]$ cd files
[devopes@server4 files]$ ls
httpd.conf
[devopes@server4 files]$ vim httpd.conf   ###改变端口为8080
[devopes@server4 files]$ cd ..
[devopes@server4 ansible]$ ansible-playbook playbook.yml




6、执行完毕再次去查看端口发现端口改变成功(已经为8080)

[root@server6 ~]# netstat -antlp   ##此时端口为8080


为了后面实验的方便,更改完之后我们在将端口改回去。

4 、通过ansible给server3加入发布页面并实现给本地访问测试server6上服务

1、编辑.yml文件,在其中加入发布页面的模块

[devopes@server4 ansible]$ vim playbook.yml
[devopes@server4 ansible]$ cat playbook.yml
---
- hosts: webservertasks:- name: install httpdyum:name: httpdstate: present- name: copy index.htmlcopy:src: files/index.htmldest: /var/www/html/index.html- name: configure httpdcopy:src: files/httpd.confdest: /etc/httpd/conf/httpd.confowner: rootgroup: rootmode: 644notify: restart httpd- name: start  httpd and firewalldservice:name: "{{item}}"state: startedloop:- httpd- firewalld- name: configure firewalldfirewalld:service: httppermanent: yes immediate: yesstate: enabledhandlers:- name: restart httpdservice:name: httpdstate: restarted- hosts: localhostbecome: notasks:- name: test httpduri:url: http://172.25.35.6status_code: 200

2、此时我们去执行.yml文件,发现会报错,这是因为没有发布页面,下面我们需要给她添加

[devopes@server4 ansible]$ ansible-playbook playbook.yml   ##报错
[devopes@server4 ansible]$ ls
ansible.cfg  files  inventory  playbook.retry  playbook.yml
[devopes@server4 ansible]$ cd files
[devopes@server4 files]$ echo www.westos.org > index.html
[devopes@server4 files]$ cd -
/home/devopes/ansible

3、添加之后再次去执行发现,还是报错(此处是因为我们没有给它私钥)

[devopes@server4 ansible]$ ansible-playbook playbook.yml   ###报错
[devopes@server4 ansible]$ cd
[devopes@server4 ~]$ cd .ssh
[devopes@server4 .ssh]$ ls
id_rsa  id_rsa.pub  known_hosts
[devopes@server4 .ssh]$ ls
id_rsa  id_rsa.pub  known_hosts
[devopes@server4 .ssh]$ cp id_rsa.pub authorized_keys
[devopes@server4 .ssh]$ ll
total 16
-rw-r--r-- 1 devopes devopes  397 Aug 10 15:21 authorized_keys
-rw------- 1 devopes devopes 1675 Aug 10 09:56 id_rsa
-rw-r--r-- 1 devopes devopes  397 Aug 10 09:56 id_rsa.pub
-rw-r--r-- 1 devopes devopes  686 Aug 10 14:06 known_hosts

4、再次执行就可以发现成功

[devopes@server4 .ssh]$ cd
[devopes@server4 ~]$ cd ansible/
[devopes@server4 ansible]$ ansible-playbook playbook.yml   ##好了

5、 编写playbook实现引用变量

1、建立模板目录,创建模板文件

[devopes@server4 ansible]$ mkdir templates
[devopes@server4 ansible]$ cd templates/
[devopes@server4 templates]$ ls
[devopes@server4 templates]$ cp ../files/httpd.conf .
[devopes@server4 templates]$ ls
httpd.conf
[devopes@server4 templates]$ mv httpd.conf httpd.conf.j2
[devopes@server4 templates]$ ls
httpd.conf.j2

2、进入.conf文件修改端口

[devopes@server4 templates]$ vim httpd.conf.j2  ##修改端口为{{ http_port }}

3、编辑playbook.yml文件

[devopes@server4 ansible]$ vim playbook.yml
[devopes@server4 ansible]$ cat playbook.yml
---
- hosts: webservervars:http_port: 80tasks:- name: install httpdyum:name: httpdstate: present- name: copy index.htmlcopy:src: files/index.htmldest: /var/www/html/index.html- name: configure httpdtemplate:src: templates/httpd.conf.j2dest: /etc/httpd/conf/httpd.confowner: rootgroup: rootmode: 644notify: restart httpd- name: start  httpd and firewalldservice:name: "{{item}}"state: startedloop:- httpd- firewalld- name: configure firewalldfirewalld:service: httppermanent: yes immediate: yesstate: enabledhandlers:- name: restart httpdservice:name: httpdstate: restarted- hosts: localhostbecome: notasks:- name: test httpduri:url: http://172.25.35.6status_code: 200

3、测试(在server5和server6上能发现httpd进程)

[devopes@server1 ansible]$ ansible-playbook playbook.yml     ###正确,没有报错
[root@server6 ~]# ps ax
[root@server5 ~]# ps ax

接下来我们再次更改端口进行验证,如下所示:

1、修改inventory文件

[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
localhost
[test]
server5 http_host=172.25.35.5  [prod]
server6 http_host=172.25.35.6[webserver:children]
test
prod

2、更改端口

[devopes@server1 ansible]$ vim templates/httpd.conf.j2   ###更改端口为{{ http_host }}:{{ http_port }]

3、检查语法并执行.yml文件

[devopes@server1 ansible]$ ansible-playbook playbook.yml --syntax-checkplaybook: playbook.yml
[devopes@server1 ansible]$ ansible-playbook playbook.yml    ###成功

4、分别在server5和server6上查看变量是否生效

[root@server5 tmp]$ cat /etc/httpd/conf/httpd.conf | grep Listen
# Listen: Allows you to bind Apache to specific IP addresses and/or
# Change this to Listen on specific IP addresses as shown below to
#Listen 12.34.56.78:80
Listen 172.25.35.5:80[root@server6 tmp]$ cat /etc/httpd/conf/httpd.conf | grep Listen
# Listen: Allows you to bind Apache to specific IP addresses and/or
# Change this to Listen on specific IP addresses as shown below to
#Listen 12.34.56.78:80
Listen 172.25.35.6:80

6、编写playbook实现采集远程主机的信息

1、编写file.yml文件

[devopes@server4 ansible]$ vim file.yml
[devopes@server4 ansible]$ cat file.yml
---
- hosts: alltasks:- name: create filetemplate:src: templates/file.j2dest: /tmp/file

2、创建模板

[devopes@server4 ansible]$ vim templates/file.j2
[devopes@server4 ansible]$ cat templates/file.j2
主机名: {{ ansible_facts['hostname'] }}
主机IP: {{ ansible_facts['default_ipv4']['address'] }}
主机DNS: {{ ansible_facts['dns']['nameservers'] }}
boot分区: {{ ansible_facts['devices']['sda']['partitions']['sda1']['size'] }}
内核: {{ ansible_facts['kernel'] }}
内存空闲: {{ ansible_facts['memfree_mb'] }}

3、编写inventory文件(将上一步的localhost注释或者删除)

[devopes@server6 ansible]$ vim inventory
[devopes@server6 ansible]$ cat inventory
[test]
server5 http_host=172.25.35.5[prod]
server6 http_host=172.25.35.6[webserver:children]
test
prod

4、执行yml文件,发现正确

[devopes@server4 ansible]$ ansible-playbook file.yml   ###正确

5、分别在server5和server6上进行查看,发现可以正确查看

[root@server5 ~]# cat /tmp/file
主机名: server5
主机IP: 172.25.35.5
主机DNS: [u'114.114.114.114']
boot分区: 1.00 GB
内核: 3.10.0-514.el7.x86_64
内存空闲: 408[root@server6 ~]# cat /tmp/file
主机名: server6
主机IP: 172.25.35.6
主机DNS: [u'114.114.114.114']
boot分区: 1.00 GB
内核: 3.10.0-514.el7.x86_64
内存空闲: 629

上面的DNS部分看起来不那么美观,要想美观我们可以在templates/file.j2文件中修改以下:

主机DNS: {{ ansible_facts['dns']['nameservers'] }}

修改完之后再次去执行.yml文件,再查看就OK了

7、编写playbook实现haproxy负载均衡

1、在server4上面进行授权

[root@server4 ansible]# vim /etc/sudoers
devopes   ALL=(ALL)       NOPASSWD: ALL

2、编辑playbook.yml文件

[devopes@server1 ansible]$ vim playbook.yml
[devopes@server1 ansible]$ cat playbook.yml
---
- hosts: webservervars:http_port: 80tasks:- name: install httpdyum:name: httpdstate: present- name: copy index.htmlcopy:content: "{{ ansible_facts['hostname'] }}"dest: /var/www/html/index.html- name: configure httpdtemplate:src: templates/httpd.conf.j2dest: /etc/httpd/conf/httpd.confowner: rootgroup: rootmode: 644notify: restart httpd- name: start  httpd and firewalldservice:name: "{{item}}"state: startedloop:- httpd- firewalld- name: configure firewalldfirewalld:service: httppermanent: yes immediate: yesstate: enabledhandlers:- name: restart httpdservice:name: httpdstate: restarted- hosts: localhosttasks:- name: install haproxyyum:name: haproxystate: present- name: configure haproxytemplate:src: templates/haproxy.cfg.j2dest: /etc/haproxy/haproxy.cfgnotify: restart haproxy- name: start haproxyservice:name: haproxystate: startedhandlers:- name: restart haproxyservice:name: haproxystate: restarted

3、在server4上安装haproxy服务,复制模板

[devope@server4 ansible]# yum list haproxy
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Available Packages
haproxy.x86_64                        1.5.18-3.el7                        westos[devopes@server4 ansible]# sudo yum install haproxy -y
[devopes@server4 ansible]# cp /etc/haproxy/haproxy.cfg  templates/haproxy.cfg.j2

4、编辑模板,实验sever5和server6上的负载均衡

[devopes@server4 ansible]$ vim templates/haproxy.cfg.j2 55     timeout server          1m56     timeout http-keep-alive 10s57     timeout check           10s58     maxconn                 300059     stats     uri    /status60 #---------------------------------------------------------------------61 # main frontend which proxys to the backends62 #---------------------------------------------------------------------63 frontend  main *:8064     acl url_static       path_beg       -i /static /images /javascript /styl    esheets65     acl url_static       path_end       -i .jpg .gif .png .css .js66 67 #    use_backend static          if url_static68     default_backend             app69 70 #---------------------------------------------------------------------71 # static backend for serving up images, stylesheets and such72 #---------------------------------------------------------------------73 backend app74          balance     roundrobin75          server  app1 172.25.35.5:80 check76          server  app2 172.25.35.6:80 check

5、执行.yml文件

[devopes@server1 ansible]$ ansible-playbook playbook.yml

6、浏览器监控测试(输入172.25.35.4/status)

7、浏览器轮循测试(输入172.25.35.4)



8、编写playbook实现将server7自动加入到负载均衡的集群中

1、创建用户并授权

[root@server7 ~]# useradd devopes
[root@server7 ~]# passwd devopes
[root@server7 ~]# vim /etc/sudoers
devopes ALL=(ALL)       NOPASSWD: ALL

2、在server4上ssh给sever7做免密

[devopes@server4 ansible]$ ssh-copy-id server7

3、在server4上编辑inventory文件

[devopes@server4 ansible]$ vim inventory
[devopes@server4 ansible]$ cat inventory
[test]
server5 http_host=172.25.35.5[prod]
server6 http_host=172.25.35.6
server7 http_host=172.25.35.7[webserver:children]
test
prod

4、编辑haproxy.cfg.j2的文件

[devopes@server4 ansible]$ vim templates/haproxy.cfg.j2 55     timeout server          1m56     timeout http-keep-alive 10s57     timeout check           10s58     maxconn                 300059     stats     uri    /status60 #---------------------------------------------------------------------61 # main frontend which proxys to the backends62 #---------------------------------------------------------------------63 frontend  main *:8064     acl url_static       path_beg       -i /static /images /javascript /styl    esheets65     acl url_static       path_end       -i .jpg .gif .png .css .js66 67 #    use_backend static          if url_static68     default_backend             app69 70 #---------------------------------------------------------------------71 # static backend for serving up images, stylesheets and such72 #---------------------------------------------------------------------73  backend app74      balance     roundrobin75  {% for host in groups['webserver'] %}server {{ hostvars[host]['ansible_facts']['hostname'] }} {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}:80 check }}76 {% endfor %}

5、执行.yml文件,发现正确

[devopes@server4 ansible]$ ansible-playbook playbook.yml    ###正确

6、浏览器监控测试(输入172.25.35.4/status)

7、浏览器轮循测试(输入172.25.35.4)


Ansible(三)编写ansible的playbook文件(实现端口更改、远程主机信息采集、负载均衡)相关推荐

  1. 论文阅读三:基于改进人工蜂群算法的SDN负载均衡策略研究

    名词解释: Artificial Bee Colony Algorithm, ABC:人工蜂群算法 Load balancing algorithm based on improved artific ...

  2. Ansible概述与部署及playbook剧本编写

    Ansible自动化运维管理工具的概述与部署 文章目录 Ansible自动化运维管理工具的概述与部署 一.Ansible简介 1.Ansible概述 2.ansible环境安装部署 3.ansible ...

  3. Ansible playbook文件中指定SSH密钥文件

    Ansible playbook可以在命令行上使用--key-file指定用于ssh连接的密钥. ansible-playbook -i hosts init_system.yml --key-fil ...

  4. 05 ansible剧本编写

    1.ansible基础知识部分补充 数据移动模块 1.1 ansible软件特点 01.可以实现批量管理 02.可以实现批量部署 03.ad-hoc(批量执行命令)---针对临时性的操作 ansibl ...

  5. ansible自动化运维详解(三)ansible常用模块续

    文章目录 ansible自动化运维详解(三)ansible常用模块续 四.ansible常用模块(2) 4.10.yum_repository 4.11.dnf 4.12.service 及 fire ...

  6. Ansible剧本编写

    目录 ansible剧本组成部分 ansible剧本编写规范 ansible剧本主机规划 ansible剧本主机清单 ansible剧本编写实践 ad-hoc部署rsync服务 playbook部署r ...

  7. 37: sudo提权 、 Ansible配置 、 Ansible Playbook 、 Ansible进阶 、 总结和答疑

    Top NSD AUTOMATION DAY02 案例1:配置sudo权限 案例2:修改Ansible配置 案例3:Playbook应用案例 案例4:Playbook应用案例 1 案例1:配置sudo ...

  8. ansible剧本编写_4个开放源代码工具,用于编写下一个剧本

    ansible剧本编写 当我在Great Wide Open (发生于3月16日至17日)上整理幻灯片的闪电演讲时, <那不是很奇怪:创意的开源工具> ,我记得在2015年下半年,我们的工 ...

  9. Ansible(四)ansible roles实现(apache+haproxy+keepalived)负载均衡+高可用

    1.ansible roles简介 <1> roles 用于层次性.结构化地组织playbook. <2> roles 能够根据层次型结构自动装载变量文件.tasks以及han ...

最新文章

  1. 敏捷开发本质 与 敏捷个人本质
  2. 转载 .net面试题大全(有答案)
  3. vue 导出 excel表格
  4. js特效 在服务器显示变形,使一行文字变形产生弯曲弧度特效的jQuery插件 - Arctext.js...
  5. AI顶会直播丨深度学习顶级会议ICLR 2021中国预讲会明天召开,为期三天五大论坛...
  6. c++ python opencv_ubuntu下C++与Python混编,opencv中mat类转换
  7. 【事件流】浅谈事件冒泡事件捕获------【巷子】
  8. [pytorch、学习] - 3.6 softmax回归的从零开始实现
  9. 【转】什么是staging server
  10. RAC 之 RMAN 备份
  11. Dubbo 新编程模型之外部化配置 1
  12. 插入排序 php,常用的排序算法(二)--插入排序(PHP实现)
  13. Python模块——HashLib(摘要算法)与base64
  14. 算法:回文数字9. Palindrome Number
  15. Franka Emika机械臂快速入门教程
  16. C语言 输出螺旋数组
  17. Proteus器件查找
  18. wps表格错开半行_WPS文字制作左右错行表格(运用插入分节符、分栏等功能)
  19. H3C路由器-内/外网用户通过公网IP访问内部服务器
  20. IA-32:Privilege level

热门文章

  1. EJB3.0学习笔记---Bean实现多个接口的情况下定义,访问方式:
  2. opencv 编译静态库
  3. Linux创建用户、用户组 及 删除
  4. android 怎么获取app 字体颜色,Android APP使用自定义字体实现方法
  5. 机器学习与计算机视觉(keras和mnist)
  6. verilog学习记(快速入门)
  7. java成绩前五名的代码_一个 JAVA 程序,实现输出考试成绩的前三名
  8. wps启用编辑按钮在哪里_WPS 新功能上线,官宣首发!人人都会用的图片设计
  9. 埃里克贝里奇_9大热门技术的安全隐患
  10. python document_python-docx 常用方法