[root@server ~]# cd /etc/ansible/[root@server ansible]# lsansible.cfghostsroles[root@server ansible]# vim hosts //在末尾添加以下内容node1[root@server ansible]# cd roles/[root@server roles]# ansible-galaxy init apache- Role apache was created successfully[root@server roles]# ansible-galaxy init mysql- Role mysql was created successfully[root@server roles]# ansible-galaxy init php- Role php was created successfully[root@server roles]# lsapachemysqlphp
部署 apache
[root@server roles]# cd apache/[root@server apache]# lsdefaultshandlersREADME.mdtemplatesvarsfiles metataskstests
[root@server ~]# cd /etc/ansible/roles/apache/vars/[root@server vars]# vim main.yml ---# vars file for apachepkgs: - bzip2 - make - wget - openssl-devel - pcre-devel - expat-devel - libtool - gcc - gcc-c++ - libxml2-devel
单元文件
[root@server ~]# cd /etc/ansible/roles/apache/templates/[root@server templates]# vim httpd.service.j2[Unit]Description=httpd server daemonAfter=network.target [Service]Type=forkingExecStart=/usr/local/apache/bin/apachectl startExecStop=/usr/local/apache/bin/apachectl stopExecReload=/bin/kill -HUP $MAINPID[Install]WantedBy=multi-user.target
[root@server ansible]# cd roles/mysql/[root@server mysql]# lsdefaultshandlersREADME.mdtemplatesvarsfiles metataskstests
playbook
[root@server mysql]# vim tasks/main.yml ---# tasks file for mysql- name: create user mysqluser:name: mysqlsystem: yesshell: /sbin/nologincreate_home: nostate: present- name: install pkgsyum:name: "libncurses*"state: present- name: unzipunarchive:src: mysql-5.7.37-linux-glibc2.12-x86_64.tar.gzdest: /usr/local/- name: creat linkfile:src: /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64dest: /usr/local/mysqlowner: mysqlgroup: mysqlstate: link- name: create data directoryfile:path: /opt/dataowner: mysqlgroup: mysqlstate: directory- name: mysql-chushi.shscript: mysql-chushi.sh- name: cp configtemplate:src: my.cnf.j2dest: /etc/my.cnf- name: replace file1replace:path: /usr/local/mysql/support-files/mysql.serverregexp: "#^(basedir=).*"replace: "basedir=/usr/local/mysql"- name: replace file2replace:path: /usr/local/mysql/support-files/mysql.serverregexp: "#^(datadir=).*"replace: "datadir=/opt/data"- name: cp mysqld.servicetemplate:src: mysqld.service.j2dest: /usr/lib/systemd/system/mysqld.service- name: apply configshell:cmd: systemctl daemon-reload- name: restart mysqldservice:name: mysqldstate: startedenabled: yes- name: set mysql passwdshell:cmd: /usr/local/mysql/bin/mysql -uroot -e "set password=password('redhat')"- name: set mysql envscript: mysql.sh[root@server mysql]# cd /etc/ansible/[root@server ansible]# vim mysql.yml---- name: use mysql rolehosts: node1roles:- mysql
Xftp 上传文件
[root@server files]# cd /etc/ansible/roles/mysql/files/[root@server files]# lsmysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
脚本
[root@server files]# cd /etc/ansible/roles/mysql/files/[root@server files]# vim mysql-chushi.sh#!/bin/bash/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ln -sv /usr/local/mysql/include/ /usr/local/include/mysqlecho '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.confldconfig[root@server files]# vim mysql.sh#!/bin/bashecho 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile.d/mysql.sh
配置文件
[root@server templates]# cd /etc/ansible/roles/mysql/templates/[root@server templates]# vim my.cnf.j2[mysqld]basedir = /usr/local/mysqldatadir = /opt/datasocket = /tmp/mysql.sockport = 3306pid-file = /opt/data/mysql.piduser = mysqlskip-name-resolve
单元文件
[root@server templates]# cd /etc/ansible/roles/mysql/templates/[root@server templates]# vim mysqld.service.j2[Unit]Description=mysql server daemonAfter=network.targe[Service]Type=forkingExecStart=/usr/local/mysql/support-files/mysql.server startExecStop=/usr/local/mysql/support-files/mysql.server stopExecReload=/bin/kill -HUP \$MAINPID[Install]WantedBy=multi-user.target
[root@server templates]# cd /etc/ansible/roles/php/templates/[root@server templates]# vim index.php.j2执行 playbook
[root@server ansible]# ansible-playbook php.yml PLAY [use php role] ************************************************************TASK [Gathering Facts] *********************************************************ok: [node1]TASK [php : install pkgs] ******************************************************changed: [node1]TASK [php : unzip] *************************************************************changed: [node1]TASK [php.sh] ******************************************************************changed: [node1]TASK [php : modify apache config] **********************************************changed: [node1]TASK [php : rm index.html] *****************************************************[WARNING]: Consider using the file module with state=absent rather than running'rm'.If you need to use command because file is insufficient you can add'warn: false' to this command task or set 'command_warnings=False' inansible.cfg to get rid of this message.changed: [node1]TASK [edit index.php] **********************************************************changed: [node1]TASK [php : restart httpd] *****************************************************changed: [node1]PLAY RECAP *********************************************************************node1: ok=8changed=7unreachable=0failed=0skipped=0rescued=0ignored=0