################################## ansible 剧本 相当于shell脚本、Dockerfile ##################################
剧本playbook基于YAML语言,是一种脚本语言 playbook组件: Target 目标 Variable 变量 Task 定义任务列表 Handler 完成任务后,调用其他任务,类似于if
Target 组件常用参数 hosts user sudo sudo_user
执行剧本
#ansible-playbook nginx_install.yaml-C预演,不是真实执行
#ansible-playbook -C nginx_install.yaml-f 1 每次单台运行 –syntax-check 检测语法
条件:上一条命令为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/nginxvim 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 ;fivim 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