##################################
ansible 剧本
相当于shell脚本、Dockerfile
##################################
剧本简述
剧本playbook基于YAML语言,是一种脚本语言
playbook组件:
Target 目标
Variable 变量
Task 定义任务列表
Handler 完成任务后,调用其他任务,类似于if
Target 组件常用参数
hosts
user
sudo
sudo_user
剧本案例
案例-装nginx1.16.1
- hosts: 192.168.0.16connection: sshremote_user: roottasks:- name: Jfedu Pcre-devel and Zlib LIB Install.yum: name=make,wget,gzip,pcre-devel,pcre,zlib-devel,gcc state=installed- name: copy nginx file to remote server.copy: src=/root/nginx-1.16.1.tar.gz dest=/tmp/ mode=644 owner=root group=root- name: Jfedu Nginx WEB Server Install Process.shell: cd /tmp;wget -c http://nginx.org/download/nginx-1.16.1.tar.gz;tar xzf nginx-1.16.1.tar.gz;cd nginx-1.16.1;./configure;
make;make install
执行剧本
#ansible-playbook nginx_install.yaml
-C预演,不是真实执行
#ansible-playbook -C nginx_install.yaml
-f 1 每次单台运行
–syntax-check 检测语法
案例-使用notify来安装nginx
条件:上一条命令为chenged为 true时,则执行操作---------
vim nginx_if_install.yaml
- hosts: 192.168.0.18connection: sshremote_user: roottasks:- name: check /usr/local/nginx/ dirfile: path=/usr/local/nginx/ state=directorynotify:- nginx lib- nginx copy- nginx install- nginx starthandlers:- name: nginx libyum: name=make,wget,gzip,pcre-devel,pcre,zlib-devel,gcc state=installed- name: nginx copycopy: src=/root/nginx-1.16.1.tar.gz dest=/tmp/ mode=644 owner=root group=root- name: nginx installshell: cd /tmp;wget -c http://nginx.org/download/nginx-1.16.1.tar.gz;tar xzf nginx-1.16.1.tar.gz;cd nginx-1.16.1;./configure;
make;make install- name: nginx startshell: /usr/local/nginx/sbin/nginx
案例-添加用户
cat user_add.yaml
- hosts: 192.168.0.18connection: sshremote_user: roottasks:- name: create jfedu666 usershell: id jfedu666;if [ $? -ne 0 ];then useradd -s /sbin/nologin jfedu666 -M ;fi
案例-添加用户,使用变量
vim user_add.yaml
- hosts: 192.168.0.18connection: sshremote_user: rootvars:JF_USER: jfedu666 这里可以设置多个tasks:- name: create {
{
JF_USER }} usershell: id {
{
JF_USER }};if [ $? -ne 0 ];then useradd -s /sbin/nologin {
{
JF_USER }} -M ;fi
案例-远程执行脚本
- hosts: 192.168.0.19connection: sshremote_user: roottasks:- name: copy nginx install scriptscopy: src=/root/auto_install_nginx.sh dest=/tmp/ mode=755- name: Install Nginx versionshell: cd /tmp/ ;/bin/sh auto_install_nginx.sh
案例-安装nginx,每台客户端口的端口不一样,需要设置模块等
vim nginx_if_install.yaml
- hosts: nginxconnection: sshremote_user: roottasks:- name: check /usr/local/nginx/ dirfile: path=/usr/local/nginx/ state=directorynotify:- nginx lib- nginx copy- nginx install- nginx conf- nginx starthandlers:- name: nginx libyum: name=make,wget,gzip,pcre-devel,pcre,zlib-devel,gcc state=installed- name: nginx copycopy: src=/root/nginx-1.16.1.tar.gz dest=/tmp/ mode=644 owner=root group=root- name: nginx installshell: cd /tmp;wget -c http://nginx.org/download/nginx-1.16.1.tar.gz;tar xzf nginx-1.16.1.tar.gz;cd nginx-1.16.1;./configure;
make;make install- name: nginx conftemplate: src=/root/nginx_install/nginx.conf dest=/usr/local/nginx/conf/nginx.conf- name: nginx startshell: /usr/local/nginx/sbin/nginx
-----------------end