Ansible playbook实战
1.基础准备
#1.安装ansible [root@m01 ~]# yum install -y ansible #2.配置ansible [root@m01 ~]# vim /etc/ansible/ansible.cfg host_key_checking = False #这个解开注释 #3.配置主机清单 [root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 ansible_ssh_pass='admin123' web02 ansible_ssh_pass='admin123' [db_group] db01 ansible_ssh_pass='admin123' db02 ansible_ssh_pass='admin123' #4.创建项目目录 [root@m01 ~]# mkdir /project/{web,mariadb,rsyncd,nfs,sersync,base} -p [root@m01 ~]# tree /project/ /project/ ├── base ├── mariadb ├── nfs ├── rsyncd ├── sersync └── web 6 directories, 0 files
2.编写playbook
1)第一部分:所有服务器优化和创建用户
[root@m01 ~]# cd /project/base [root@m01 base]# vim lnmp.yml - hosts: all tasks: - name: Stop Firewalld systemd: name: firewalld state: stopped enabled: no - name: Stop Selinux selinux: state: disabled - name: Create www Group group: name: www gid: 666 state: present - name: Create www User user: name: www uid: 666 group: www create_home: false # 不需要家目录 shell: /sbin/nologin state: present
2)第二部分:安装nginx服务器
1.安装方式选择
#源码包安装 1.上传nginx包 copy 2.解压 unarchive 3.生成 shell 4.编译 shell 5.安装 shell -m shell -a 'cd /nginx/ && ./configure -- && make && make install' 6.编辑system管理nginx文件 copy 7.配置conf.d的目录 8.推配置文件 #官方源安装 1.配置官方源 copy 2.安装nginx yum 3.配置nginx #rpm包安装
2.准备工作
#1.上传rpm包 [root@m01 ~]# rz nginx-1.16.1-1.el7.ngx.x86_64.rpm #2.配置nginx配置文件 [root@m01 ~]# vim nginx.conf user www; ... #3.配置wordpress配置文件 [root@m01 ~]# vim wordpress.conf server { listen 80; server_name blog.linux.com; root /code/wordpress; index index.php; location ~ \.php$ { root /code/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
3.编写playbook
[root@m01 base]# vim lnmp.yml # 结尾继续接上 - hosts: web_group tasks: - name: Copy Nginx Rpm copy: src: /root/nginx-1.16.1-1.el7.ngx.x86_64.rpm dest: /tmp/ - name: Install Nginx Server yum: name: /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm state: present - name: Config Nginx Server copy: src: /root/nginx.conf dest: /etc/nginx/ - name: Config Wordpress Conf copy: src: /root/wordpress.conf dest: /etc/nginx/conf.d/ - name: Start Nginx Server systemd: name: nginx state: started
3)第三部分:安装php服务
1.准备工作
#1.上传php包 [root@m01 ~]# rz php.tar.gz #2.配置php用户 [root@m01 ~]# vim www.conf user = www group = www
2.编写playbook
[root@m01 base]# vim lnmp.yml # 结尾继续接上 - name: Tar PHP Package unarchive: src: /root/php.tar.gz dest: /tmp/ - name: Install PHP Server shell: "yum localinstall -y /tmp/*.rpm" - name: Config PHP Server copy: src: /root/www.conf dest: /etc/php-fpm.d/ - name: Start PHP Server systemd: name: php-fpm state: started
4)第四部分:配置wordpress站点
1.准备工作
#1.上传wordpress包 [root@m01 ~]# rz wordpress-5.0.3-zh_CN.tar.gz
2.编写playbook
[root@m01 base]# vim lnmp.yml # 结尾继续接上 - name: Create Code Dir file: path: /code state: directory - name: Install Wordpress Code unarchive: src: /root/wordpress-5.0.3-zh_CN.tar.gz dest: /code/ - name: Chown Code Dir file: path: /code state: directory owner: www group: www recurse: yes
5)第五部分:安装mariadb
- hosts: db01 tasks: - name: Install Mariadb Server yum: name: mariadb-server state: present - name: Install MySQL-python Server #ansible要控制mysql需要安装这个 yum: name: MySQL-python state: present - name: Start Mariadb Server systemd: name: mariadb state: started enabled: yes - name: Create Database Wordpress mysql_db: name: wordpress state: present - name: Create Database Wordpress User mysql_user: name: "wp" host: "172.16.1.%" password: '123456' priv: 'wordpress.*:ALL' state: present
6)完成wordpress搭建,访问并配置数据库测试
3.迁移(扩展)
1)编写数据迁移
[root@m01 base]# cat sql.yml - hosts: db01 tasks: - name: Dump Database Wordpress mysql_db: #把wordpress库导出到sql中 state: dump name: wordpress target: /root/wordpress.sql - name: Get wordpress.sql fetch: #把db01的sql拉到本地路径 src: /root/wordpress.sql dest: /root/ #会在下面创建 主机名/文件路径 /root/db01/root/wordpress.sql - hosts: db02 tasks: - name: Post wordpress.sql copy: src: /root/db01/root/wordpress.sql dest: /tmp/ - name: Install Mariadb Server #下面同上创建数据库 yum: name: mariadb-server state: present - name: Install MySQL-python Server #ansible要控制mysql需要安装这个 yum: name: MySQL-python state: present - name: Start Mariadb Server systemd: name: mariadb state: started enabled: yes - name: Create Database Wordpress mysql_db: name: wordpress state: present - name: Create Database Wordpress User mysql_user: name: "wp" host: "172.16.1.%" password: '123456' priv: 'wordpress.*:ALL' state: present - name: Import wordpress.sql #导入sql数据 mysql_db: state: import name: wordpress target: /tmp/wordpress.sql
2)一键迁移博客
- hosts: web01 tasks: - name: Tgz Wordpress Code - archive: path: /code/wordpress dest: /root/wordpress.tgz - name: Get Wordpress Code fetch: src: /root/wordpress.tgz dest: /root/ - hosts: web02 tasks: - name: Tar Wordpress Code unarchive: src: /root/web01/root/wordpress.tgz dest: /
Ansible变量
一、变量概述
变量提供了便捷的方式来管理Ansible playbook的每一个项目中的动态值,比如nginx-1.6.3这个软件包的版本,在其他地方或许会反复使用,
那么如果讲此值设置为变量,然后再在其他的playbook中调用,会方便许多。如此一来还方便维护,减少维护的成本。
二、定义变量的方式
1.通过命令行进行变量定义 2.在play文件中进行变量定义 3.通过Inventory主机信息文件中进行变量定义 4.通过vars_files定义变量 5.通过host_vars和group_vars定义变量 如果在定义变量时,变量冲突了 在上述的三个地方分别设置了: 1.命令行中:age=11 2.play文件中:age=12 3.Inventory中:age=13 那么,最终的age结果一定是11 变量的读取优先级为:命令行 > playbook文件 > Inventory文件
1.在playbook定义变量
方式一:在hosts下直接定义变量
[root@m01 base]# vim install.yml - hosts: web_group vars: package: - httpd - mariadb-server - MySQL-python - php - nginx tasks: - name: Install Server yum: name: "{{ package }}"
方式二:在hosts下定义多个变量
[root@m01 base]# vim install.yml - hosts: web_group vars: web: - httpd - nginx db: - mariadb-server - MySQL-python tasks: - name: Install Server yum: name: "{{ web }}"
方式三:在模块下定义变量
- name: Install LNMP yum: name: "{{ packages }}" vars: packages: - nginx - php - mariadb-server
2.通过vars_file定义变量
刚才我们学到在playbook中使用vars定义变量,有一个缺陷,就是其他的playbook无法使用该变量。所以我们可以采取第二种定义变量的方式,在vars_file中定义变量。
#1.配置变量文件 [root@m01 base]# vim vars.yml web_server: nginx db_server: mariadb-server #2.调用变量文件再使用变量 [root@m01 base]# vim host.yml - hosts: db_group vars_files: /project/base/vars.yml tasks: - name: Install Mariadb Server yum: name: "{{ db_server }}" #3.定义多个变量文件 [root@m01 base]# vim host.yml - hosts: db_group vars_files: - /project/base/vars.yml - /project/base/vars2.yml tasks: - name: Install Mariadb Server yum: name: "{{ nginx }}"
标签:26,name,nginx,state,ansible,wordpress,playbook,m01,root From: https://www.cnblogs.com/ludingchao/p/18094640