【Ansible 的脚本 --- playbook 剧本】

目录


一、playbook 剧本介绍

playbooks 本身由以下各部分组成

bash 复制代码
(1)Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
(2)Variables:变量
(3)Templates:模板
(4)Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
(5)Roles:角色

二、示例

bash 复制代码
vim  /etc/ansible/playbook/deamo1.yml
bash 复制代码
---
- name: the first play for install apache
  #gather_facts: false
  hosts: dbservers
  remote_user: root
  tasks:
  - name: disable firewwalld
    service: name=firewalld state=stopped enabled=no
  - name: disable selinux
    command: '/usr/sbin/setenforce 0'
    ignore_errors: True
  - name: disable selinux forever
    replace: path=/etc/selinux/config regexp="enforcing" replace="disabled"
  - name: mount cdrom
    mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
  - name: copy local yum configuration file
    copy: src=/etc/yum.repos.d/repo.bak/local.repo dest=/etc/yum.repos.d/local.repo
  - name: install apache
    yum: name=httpd state=latest
  - name: prepare httpd configuration file
    copy: src=/etc/ansible/playbook/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: "reload httpd"
  - name: start apache
    service: name=httpd state=started enabled=yes
  handlers:
  - name: reload httpd
    service: name=httpd state=reloaded

1、运行playbook

bash 复制代码
ansible-playbook deamo1.yml
//补充参数:
-k(--ask-pass):用来交互输入ssh密码
-K(-ask-become-pass):用来交互输入sudo密码
-u:指定用户

2、定义、引用变量

三、使用playbook部署lnmp集群

bash 复制代码
- name: the first play for install nginx
  hosts: dbservers
  remote_user: root
  tasks:
  - name: disable firewwalld
    service: name=firewalld state=stopped enabled=no
  - name: disable selinux
    command: '/usr/sbin/setenforce 0'
    ignore_errors: True
  - name: disable selinux forever
    replace: path=/etc/selinux/config regexp="enforcing" replace="disabled"
  - name: mount cdrom
    mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
  - name: copy local yum configuration file
    copy: src=/etc/yum.repos.d/nginx.repo  dest=/etc/yum.repos.d/nginx.repo
  - name: install nginx
    yum: name=nginx
  - name: prepare nginx configuration file
    copy: src=/etc/ansible/playbook/default.conf dest=/etc/nginx/conf.d/default.conf
  - name: start nginx
    service: name=nginx state=started enabled=yes
  - name: wordpress
    copy: src=/usr/share/nginx/html/wordpress  dest=/usr/share/nginx/html/

- name: mysql
  hosts: dbservers
  remote_user: root
  tasks:
  - name: remove mariadb
    shell: yum remove mariadb* -y
    ignore_errors: True
  - name: yum
    command: wget https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm
  - name: install mysql57
    command: rpm -ivh mysql57-community-release-el7-11.noarch.rpm
  - name: change mysql-community-server
    shell: sed -i 's/gpgcheck=1/gpgcheck=0/' /etc/yum.repos.d/mysql-community.repo
  - name: install mysql-server
    yum: name=mysql-server
  - name: start mysql
    service: name=mysqld.service state=started enabled=yes
  - name: mysql congruation file
    copy: src=/etc/ansible/playbook/mysql.sh dest=/var/lib/mysql
  - name: echo password
    shell: grep "password" /var/log/mysqld.log | awk 'NR==1{print $NF}'  #在日志文件中找出root用户的初始密码
    register: mysql_password   #将初始密码导入到mysql_password的变量中
  - name: echo
    debug:
     msg: "{{ mysql_password }}"  #输出变量mysql_password的值
  - name: grant location
    shell:  mysql --connect-expired-password -uroot -p"{{ mysql_password['stdout'] }}" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin@123';"
  - name: grant
    shell: mysql --connect-expired-password -uroot -pAdmin@123 -e "grant all privileges on *.* to 'root'@'%' identified by 'Admin@123456' with grant option;"
  - name: create database
    shell: mysql --connect-expired-password -uroot -pAdmin@123 -e "create database wordpress;"
  - name: grant
    shell: mysql --connect-expired-password -uroot -pAdmin@123 -e "grant all on wordpress.* to 'admin'@'%' identified by 'Admin@123456';"
  - name: grant
    shell: mysql --connect-expired-password -uroot -pAdmin@123 -e "grant all on wordpress.* to 'admin'@'localhost' identified by 'Admin@123456';"
  - name: flush
    shell: mysql --connect-expired-password -uroot -pAdmin@123 -e 'flush privileges;'
  - name: yum remove
    command: yum -y remove mysql57-community-release-el7-10.noarch

- name: php
  hosts: dbservers
  remote_user: root
  tasks:
  - name: yum rpm
    command: yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
    ignore_errors: true
  - name: yum-utils
    yum: name=yum-utils
  - name: yum-config
    command: yum-config-manager --enable remi-php74
  - name: list php
    command: yum list php
  - name: yilaibao
    command: yum -y install php  php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis
  - name: start php
    service: name=php-fpm state=started enabled=yes

将yum安装的nginx里面的配置文件进行修改,后传输到对应的远程主机

解压wordpress压缩文件,放入到对应的html网页目录底下

进行传输到远程主机里的网页页面目录上

使用浏览器进行访问测试

相关推荐
学Linux的语莫3 小时前
Ansible使用简介和基础使用
linux·运维·服务器·nginx·云计算·ansible
学Linux的语莫11 小时前
Ansible Playbook剧本用法
linux·服务器·云计算·ansible
Nightwish512 小时前
ansible操作随记(一)
ansible
qq_3831398415 小时前
ansible playbook安装nacos
ansible
Aimyon_364 天前
⾃动化运维利器 Ansible-Jinja2
运维·ansible
柒月VII6 天前
【Ansible常用命令+模块+Playbook+Roles】
linux·服务器·ansible
Linux运维技术栈6 天前
生产环境centos8 & Red Hat8部署ansible and 一键部署mysql两主两从ansible脚本预告
运维·数据库·mysql·自动化·ansible
Aimyon_366 天前
⾃动化运维利器 Ansible-最佳实战
linux·运维·ansible
饭桶也得吃饭6 天前
运维工具Ansible部署、配置
运维·服务器·ansible
陪小七许个愿6 天前
Ansible一键部署Kubernetes集群
容器·kubernetes·ansible