ansible剧本(playbook)案例

    科技2024-10-29  13

    ################################## ansible 剧本 相当于shell脚本、Dockerfile ##################################

    剧本简述

    剧本playbook基于YAML语言,是一种脚本语言 playbook组件: Target 目标 Variable 变量 Task 定义任务列表 Handler 完成任务后,调用其他任务,类似于if

    Target 组件常用参数 hosts user sudo sudo_user

    剧本案例

    案例-装nginx1.16.1

    - hosts: 192.168.0.16 connection: ssh remote_user: root tasks: - 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.18 connection: ssh remote_user: root tasks: - name: check /usr/local/nginx/ dir file: path=/usr/local/nginx/ state=directory notify: - nginx lib - nginx copy - nginx install - nginx start handlers: - name: nginx lib yum: name=make,wget,gzip,pcre-devel,pcre,zlib-devel,gcc state=installed - name: nginx copy copy: src=/root/nginx-1.16.1.tar.gz dest=/tmp/ mode=644 owner=root group=root - name: nginx install 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 - name: nginx start shell: /usr/local/nginx/sbin/nginx

    案例-添加用户

    cat user_add.yaml - hosts: 192.168.0.18 connection: ssh remote_user: root tasks: - name: create jfedu666 user shell: id jfedu666;if [ $? -ne 0 ];then useradd -s /sbin/nologin jfedu666 -M ;fi

    案例-添加用户,使用变量

    vim user_add.yaml

    - hosts: 192.168.0.18 connection: ssh remote_user: root vars: JF_USER: jfedu666 这里可以设置多个 tasks: - name: create {{ JF_USER }} user shell: id {{ JF_USER }};if [ $? -ne 0 ];then useradd -s /sbin/nologin {{ JF_USER }} -M ;fi

    案例-远程执行脚本

    - hosts: 192.168.0.19 connection: ssh remote_user: root tasks: - name: copy nginx install scripts copy: src=/root/auto_install_nginx.sh dest=/tmp/ mode=755 - name: Install Nginx version shell: cd /tmp/ ;/bin/sh auto_install_nginx.sh

    案例-安装nginx,每台客户端口的端口不一样,需要设置模块等

    vim nginx_if_install.yaml

    - hosts: nginx connection: ssh remote_user: root tasks: - name: check /usr/local/nginx/ dir file: path=/usr/local/nginx/ state=directory notify: - nginx lib - nginx copy - nginx install - nginx conf - nginx start handlers: - name: nginx lib yum: name=make,wget,gzip,pcre-devel,pcre,zlib-devel,gcc state=installed - name: nginx copy copy: src=/root/nginx-1.16.1.tar.gz dest=/tmp/ mode=644 owner=root group=root - name: nginx install 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 - name: nginx conf template: src=/root/nginx_install/nginx.conf dest=/usr/local/nginx/conf/nginx.conf - name: nginx start shell: /usr/local/nginx/sbin/nginx

    -----------------end

    Processed: 0.108, SQL: 8