注意: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
- name: cp nginx.tar.gz
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
- name: install mariadb MySQL-python
- 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
- name: vi /etc/my.cnf
第二种方法(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
- name: install mariadb
- 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
- name: make user and give privileges
- 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;”
- name: copy my.cnf