ansible-playbook应用
Target #定义playbook的远程主机组
Variable #定义piaybook使用的变量
Task #定义远程主机上执行的任务列表
Handler #定义task执行完成以后需要调用的任务,例如配置文件被改动,则启动handler任务重启相关联的服务。
Target常见参数如下:
hosts #定义远程主机组
user #执行该任务的用户
sudo #设置为yes的时候,执行任务的时候用root权限
sudo user #指定普通用户
connection #默认基于SSH连接客户端
gather_facts #获取远程主机facts基础信息
Variable常用参数如下:
vars #定义格式
vars files #指定变量文件
vars prompt #用户交互模式自定义变量
setup #模块去远程主机的值
Task常用参数如下:
name #任务显示名称也即屏幕显示信息
action #定义执行的动作
copy #复制本地文件到远程主机
template #复制本地文件到远程主机,乐意引用表里
service #定义服务的状态
ansible-playbook应用案例:
1)远程主机安装nginx web服务,playbook代码如下(注意空格/格式):
固定格式:- hosts:all表示去所有机器执行, remote_user表示使用远程主机那个用户来执行,connection表示客户端连接方式(默认就是SSH 22端口,可以不写).
tasks:任务集, -name:任务名称, yum:调用YUM模块, shell:调用shell模块。
- hosts all
remote_user: root
connection: ssh
gather_facts: no
tasks:
- name: precious pcre-devel openssl-devel make gcc-c++ install.
yum: name=prce-devel,openssl-devel,make,gcc-c++ state=installed
- name: precious nginx WEB server install process.
shell: cd /usr/src/;wget -c http://nginx.org/download/nginx-1.16.0.tar.gz;tar xf nginx-1.16.0.tar.gz;cd nginx-1.16.0;make;make install
- hosts nginx
remote_user: root
tasks:
- name:nginx server install 2020-04-03
file: path=/usr/local/nginx/ state=directory
notify:
- nginx install
- nginx start
handlers:
- name: nginx install
shell: cd /data/sh/;/bin/bash auto_install_nginx.sh
- name: nginx start
shell: /usr/local/nginx/sbin/nginx
3)检测远程主机内核配置文件是否更新,如果更新则执行命令sysctl -p使内核参数生效,playbook代码如下:
-hosts: 192.168.1.100,192.168.1.101
remote_user: root
tasks:
- name: Linux Kernet config 2020
copy: src=/data/sh/sysctl.conf dest=/etc/
notify:
- source sysctl
handlers:
- name: source sysctl
shell: sysctl -p
4)检测远程主机nginx服务配置文件被修改,则重启nginx服务:
- hosts: all
remote_user: root
tasks:
- name: nginx web server .conf
shell: sed -i 's/80/8888/g' /usr/local/nginx/conf/nginx.conf;grep "8888" /usr/local/nginx/conf/nginx.conf
notify:
- nginx reload
handlers:
- name: nginx reload
shell: /usr/local/nginx/sbin/nginx -s reload
5) 基于列表items多个值创建用户,通过{{}}双大括号 来定义列表变量, with_items选项传入变量的值:
使用user模块的方法:
状态state=present创建的意思:
- hosts: all
remote_user: root
tasks:
- name: Linux ststem Add User list.
user: name={{item}} state=present
with_itmes:
- docker1
- docekr2
- docker3
- docker4
使用shell模块的方法:创建100个用户
- hosts: all
remote_user: root
tasks:
- name: Linux system Add User list.
shell: for i in `seq 1 100`;do useradd docker$i;echo 123|passwd --stdin docker$i;done


6) ansible playbook可以自定义template模板文件,模板文件主要用于服务器需求不一致的情况,需要独立定义的,例如两台服务器安装了nginx,安装完毕之后讲服务器A的HTTP的端口改成81,服务器B的HTTP端口改成82,基于tempalte模块轻松实现,方法步骤如下:
1,ansbile hosts文件指定不同服务器不同的httpd port端口,代码如下:
vim /etc/ansible/hosts
[web]
192.168.1.200 httpd_port=81
192.168.1.201 httpd_port=82
2, ansible端创建nginx.conf.j1(自定义命名)模板文件, cp nginx.conf.j1 nginx.conf.j2,并修改listen 80为listen {{httpd_port}},nginx其它配置想不变, 代码如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen {{httpd_port}};
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3, ansible playbook剧本yaml文件创建,(template调用hosts文件中得变量)代码如下:
- hosts: all
remote_user: root
tasks:
- name: nginx server install 2020
file: path=/usr/local/nginx/ state=directory
notify:
- nginx install
- nginx config
handlers:
- name: nginx install
shell: cd /usr/src/;wget -c http:/nginx.org/download/nginx-1.16.0.tar.gz;tar xf nginx-1.16.0.tar.gz;cd nginx-1.16.0;make;make install
- name: nginx config
template: src=/data/sh/nginx.j1 dest=/usr/local/nginx/conf/nginx.conf
- hosts: all
remote_user: root
tasks:
- name: Nginx server Install 2020
shell: if [ -d /usr/local/nginx/ ];then exit 127;fi
notify:
- nginx install
- nginx config
- nginx start
handlers:
- name: nginx install
shell: cd /usr/src;wget -c http://nginx.org/download/nginx-1.16.0.tar.gz;tar xf nginx-1.16.0.tar.gz;cd nginx-1.16.0;./configure --prefix=/usr/local/nginx;make;make install
- name: nginx config
template: src=/data/sh/nginx.conf.j1 dest=/usr/local/nginx/conf/nginx.conf
- name: nginx start
shell: /usr/local/nginx/sbin/nginx
执行:
ansible-playbook web.yaml
ansible all -m shell -a "cat /usr/local/nginx/conf/nginx.conf;netstat -nutlp"




6) ansible playbook剧本中调用shell脚本,有两种方法如下:
使用copy模块:
使用copy 模块拷贝到对方远程机器:(src源地址 dest目标地址 mode授予权限)
- hosts: all
remote_user: root
ganther_facts: no
tasks:
- name: nginx server install 2020
copy: src=/data/sh/auto_install_nginx.sh dest=/tmp/ mode=645
- name: Exec auto install nginx SHELL.
shell: cd /tmp/;/bin/bash auto_install_nginx.sh
使用script模块: ansible-doc script查看帮助:(需先再ansible本机创建脚本:/data/sh/df.sh)
ansible 192.168.1.200 -m script -a "/data/sh/df.sh"

7) 在剧本中定义变量:
vars:
- nginx_ver:1.16.0
- nginx_url:http://nginx.org/download
调用变量时 使用{{}}双大括号:
- hosts: all
remote_user: root
gather_facts: no
vars:
- nginx_ver: 1.16.0
- nginx_url: http://nginx.org/download
tasks:
- name: nginx web server install.
shell: wget -c {{nginx_url}}/nginx-{{nginx_ver}}.tar.gz

ansible-playbook应用的更多相关文章
- Ansible playbook API 开发 调用测试
Ansible是Agentless的轻量级批量配置管理工具,由于出现的比较晚(13年)基于Ansible进行开发的相关文档较少,因此,这里通过一些小的实验,结合现有资料以及源码,探索一下Ansible ...
- ansible playbook实践(四)-如何调试写好的playbook文件
有时,我们写了一个长长,功能很强悍的yaml文件,但是,我们有可能会担心,写的yaml文件是否正确,是否有漏洞危机,毕竟是要修改线上的机器,那么,有可能我们可以从以下几个检查维度来进行,确保在大规模应 ...
- ansible playbook批量改ssh配置文件,远程用户Permission denied
最近手里的数百台服务器需要改/etc/ssh/sshd_config的参数,禁止root直接登陆,也就是说 [root@t0 ~]# cat /etc/ssh/sshd_config | grep R ...
- ansible笔记(11):初识ansible playbook(二)
ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...
- ansible笔记(10):初识ansible playbook
ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...
- Ansible playbook 批量修改服务器密码 先普通后root用户
fsckzy Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...
- 写Ansible playbook添加zabbix被监控的对象
本主题达到的效果是能通过编写Ansible Playbook,创建zabbix主机组,把被监控的对象加入到zabbix监控系统中,同时链接到对象的模板. 1.准备工作 在zabbix服务器上面,我们需 ...
- Ansible playbook基础组件介绍
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
- ansible playbook基本操作
一.ansible playbook简单使用 相当于是把模块写入到配置文件里面 vim /etc/ansible/test.yml //写入如下内容: --- - hosts: 127.0.0.1 r ...
- ansible入门四(Ansible playbook基础组件介绍)
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
随机推荐
- 理解ffmpeg
ffmpeg是一个完整的.跨平台的音频和视频录制.转换和流媒体解决方案. 它的官网:https://ffmpeg.org/ 这里有一份中文的文档:https://ffmpeg.p2hp.com/ ff ...
- java根据配置文件读取值
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> ...
- 【Redis】基础命令
声明:本篇文章参考于该作者的# Redis从入门到精通:中级篇,大家有兴趣,去关注一下. 1.字符串(String) String(字符串)是Redis中最简单的一种数据结构,和MemCache数据结 ...
- 【Redis】字符串sds
sds,即 Simple Dynamic Strings,是Redis中存储绝大部分字符串所采用的数据结构. typedef char *sds; 一.类型 sds的类型包括SDS_TYPE_5, S ...
- Python根目录中没有Scripts文件夹问题
电脑版本是win10,配置好python的环境变量,确保可以运行python命令. 1.打开cmd命令行输入 python -m ensurepip 2.查看Python根目录下,有没有新生成Scri ...
- VuePress@next 使用数学公式插件
VuePress@next 使用数学公式插件 搞了一个VuePress1.0的 现在升级了一下,但是使用数学公式的插件老报错啊!经过不懈努力,终于搞定了.现在记录一下. VuePress 介绍 Vue ...
- [selenium]相对定位器
前言 Relative Locators,相对定位器,是Selenium 4引入的一个新的定位器,相对定位器根据源点元素去定位相对位置的其它元素. 相对定位方法其实是基于JavaScript的 get ...
- 3、Spring之入门案例
3.1.创建module 3.1.1.右击project,创建新module 3.1.2.选择maven 3.1.3.设置module名称和路径 3.1.4.module初始状态 3.1.5.配置打包 ...
- 12、Mybatis之分页插件
12.1.引入依赖 <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> ...
- sublime运行php文件
sublime 运行 php 文件 使用 sublime 打开一个php文件 然后 Tools -> Build System -> New Build System 将以上打开的文件内容 ...