playbook一键源码部署nginx、Mysql主从复制

本文介绍了如何使用Ansible playbook自动化部署Nginx和配置MySQL主从复制。首先,展示了一键部署Nginx的步骤,包括安装依赖、解压源码、编译安装等。接着,详细说明了两种方法实现MySQL主从复制,通过mysql_user和mysql_replication模块以及shell模块进行配置。每一步都提供了具体的playbook任务示例。
摘要由CSDN通过智能技术生成

注意:playbook格式规范,有些符号在这里打不出来,或是在显示的时候会有所变动,切记要按格式来
playbook一键源码部署nginx

  • hosts: webserver
    tasks:
    • name: cp nginx.tar.gz
      copy: src=/root/nginx-1.17.6.tar.gz dest=/root/
    • name: install env
      yum: name=openssl-devel,pcre-devel,zlib-devel,gcc,gcc-c++,make state=present
    • name: uncomepress nginx.tar.gz
      unarchive: src=/root/nginx-1.17.6.tar.gz dest=/root/ copy=no
    • name: make nginx group
      group: name=nginx gid=996
    • name: make nginx user
      user: name=nginx uid=996 group=998 shell=/sbin/nologin create_home=no
    • name: configure nginx
      shell: cd nginx-1.17.6 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install && ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginxctl && nginxctl

playbook一键部署Mysql主从复制
第一种方法(mysql模块)
需要安装MySQL-python


  • hosts: Master,Slave
    tasks:
    • name: install mariadb MySQL-python
      yum: name=mariadb,mariadb-server,MySQL-python state=present
    • name: start mariadb
      service: name=mariadb state=started enabled=yes
    • mysql_user: user=root password=123 update_password=always
  • hosts: Master
    tasks:
    • mysql_user: user=root login_host=localhost login_password=123 name=repl password=123 priv=’.:ALL’ state=present host=’%’
    • name: vi /etc/my.cnf
      copy: src=/root/.ansible/m/my.cnf dest=/etc/my.cnf
    • name: restart mariadb
      service: name=mariadb state=restarted
  • hosts: Slave
    tasks:
    • name: vi /etc/my.cnf
      copy: src=/root/.ansible/s/my.cnf dest=/etc/my.cnf
    • name: restart mariadb
      service: name=mariadb state=restarted
    • name: get mysql-master status
      mysql_replication: login_user=‘root’ login_password=‘123’ mode=“getmaster”
      delegate_to: 192.168.70.142
      register: master1
    • name: configure replication on the slave.
      mysql_replication: login_user=‘root’ login_password=‘123’ mode=changemaster master_host=‘192.168.70.142’ master_user=‘repl’ master_password=‘123’ master_log_file={{ master1.File }} master_log_pos={{ master1.Position }}
    • name: start slave
      mysql_replication: login_user=‘root’ login_password=‘123’ mode=startslave

第二种方法(shell模块)


  • hosts: Master,Slave
    tasks:
    • name: install mariadb
      yum: name=mariadb,mariadb-server state=present
    • name: start mariadb
      service: name=mariadb state=started enabled=yes
    • name: make maraidb password
      shell: mysqladmin -uroot password 123
  • hosts: Master
    tasks:
    • name: make user and give privileges
      shell: mysql -uroot -p’123’ -e “create user ‘repl’;grant all privileges on . to ‘repl’@‘192.168.70.131’ identified by ‘123’ with grant option;”
    • name: copy my.cnf
      copy: src=/root/.ansible/m/my.cnf dest=/etc/my.cnf
    • name: restart mariadb
      service: name=mariadb state=restarted
  • hosts: Slave
    tasks:
    • name: copy my.cnf
      copy: src=/root/.ansible/s/my.cnf dest=/etc/my.cnf
    • name: restart mariadb
      service: name=mariadb state=restarted
    • name: change Master
      shell: mysql -uroot -p’123’ -e “change master to master_host=‘192.168.70.142’,master_port=3306,master_user=‘repl’,master_password=‘123’,master_log_file=‘master-bin.000001’,master_log_pos=245;start slave;”
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SZX@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值