1.ansible 常用指令总结,并附有相关示例(远程批量安装nginx)
--- - hosts: webserver remote_user: root tasks: - name: install nginx yum: {name: nginx} - name: make datadirectory file: {path: /data/nginx/, state: directory} - name: config datafile copy: {src: files/index.html, dest: /data/nginx/, owner: nginx, group: nginx} - name: subconfiguration file copy: {src: files/www.zyb.com.conf, dest: /etc/nginx/conf.d/} notify: restart service - name: start service service: {name: nginx, state: started, enabled: yes} handlers: - name: restart service service: {name: nginx, state: restarted} ...
2.总结ansible playbook目录结构及文件用途
Ansible 配置文件列表:
/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性,也可以在项目的目录中创建此文件,当前目录下如果也有ansible.cfg,则此文件优先生效,建议每个项目目录下,创建独有的ansible.cfg文 件
/etc/ansible/hosts 主机清单
/etc/ansible/roles/ 存放角色的目录
Ansible 主配置文件:
Ansible 的配置文件可以放在多个不同地方,优先级从高到低顺序如下:
①:ANSIBLE_CONFIG #环境变量,目录下的文件必须存在才能生效
②:./ansible.cfg #当前目录下的ansible.cfg,一般一个项目对应一个专用配置文件,推荐使 用
③:~/.ansible.cfg #当前用户家目录下的.ansible.cfg
④:/etc/ansible/ansible.cfg #系统默认配置文件
Inventory 主机清单文件:
ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory 主机清单文件中将其分组组织。
默认的inventory file为 /etc/ansible/hosts。
inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成
注意:
生产建议在每个项目目录下创建项目独立的hosts文件,通过项目目录下的ansible.cfg文件中的 inventory = ./hosts实现
Ansible相关工具:
/usr/bin/ansible 主程序,临时命令执行工具
/usr/bin/ansible-doc 查看配置文档,模块功能查看工具,相当于man /usr/bin/ansible-playbook 定制自动化任务,编排剧本工具,相当于脚本
/usr/bin/ansible-pull 远程执行命令的工具
/usr/bin/ansible-vault 文件加密工具
/usr/bin/ansible-console 基于Console界面与用户交互的执行工具
/usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台
3.使用ansible playbook实现一个mysql角色
#安装ansible,把ansible.cfg和hosts文件转移到新建的/data/ansible下 #打通和远程主机的key验证 #以下是创建mysql角色和编写playbook ****************************************************************** [root@Rocky8 /data/ansible]#ls ansible.cfg hosts files mysql_role.yml roles #files和roles是文件夹 *****************编写hosts文件************************************** [webserver] 10.0.0.20 10.0.0.30 [webserver:vars] #使用变量 PATH=PATH=/usr/local/mysql/bin:\$PATH ****************在files文件夹下编写配置文件my.cnf******************** [root@Rocky8 /data/ansible]#vim files/my.cnf [mysqld] datadir=/data/mysql skip_name_resolve=1 socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.log pid-file=/data/mysql/mysql.pid [client] socket=/data/mysql/mysql.sock ******************在roles/mysql/tasks/下编写mysql的yml文件*************** [root@Rocky8 /data/ansible]#vim roles/mysql/tasks/main.yml - name: install dependent packages yum: name: "{{ item }}" loop: [libaio, numactl-libs] - name: create group group: {name: mysql} - name: create user user: {name: mysql, shell: /sbin/nologin, group: mysql} - name: prepare mysql binary file unarchive: {src: files/mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz, dest: /usr/local, owner: root, group: root, mode: 0755} - name: create file symbolic link file: {src: /usr/local/mysql-8.0.32-linux-glibc2.12-x86_64, dest: /usr/local/mysql, force: yes, state: link} - name: create work director file: {path: /data/mysql, state: directory, owner: mysql, group: mysql} - name: prepare evm variable shell: echo "{{ PATH }}">/etc/profile.d/mysql.sh;source /etc/profile.d/mysql.sh - name: prepare configure file copy: {src: files/my.cnf, dest: /etc/} - name: initialization mysql shell: mysqld --initialize-insecure --user=mysql --datadir=/data/mysql - name: prepare script service # shell: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld copy: {src: /usr/local/mysql/support-files/mysql.server, dest: /etc/init.d/mysqld, remote_src: yes, mode: 755} - name: system service update shell: chkconfig --add mysqld - name: start mysqld service service: {name: mysqld, state: started, enabled: yes} ********************************************************************* [root@Rocky8 /data/ansible]#tree . ├── ansible.cfg ├── files │ ├── my.cnf │ ├── mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz ├── hosts ├── mysql_role.yml ├── roles ├── mysql └── tasks └── main.yml **********************启动mysql_role.yml*************** [root@Rocky8 /data/ansible]#ansible-playbook mysql_role.yml
4.基于角色完成部署LNMP架构,并支持一键发布,回滚应用。同时基于zabbix角色批量部署zibbix
(1)基于角色完成部署LNMP架构:
*******************目录结构****************** [root@Rocky8 /data/ansible]#tree . ├── ansible.cfg ├── files │ ├── my.cnf │ ├── mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz │ ├── nginx.service │ ├── php.ini #修改时区(拷贝在其他主机通过yum安装的php-fpm生成的文件) │ ├── php_nginx.conf │ ├── wordpress-6.1.1-zh_CN.tar.gz │ └── www.conf #修改5项内容 ├── hosts ├── lnmp.yml ├── mysql_role.yml ├── nginx_role.yml ├── php_role.yml ├── roles │ ├── mysql │ │ └── tasks │ │ └── main.yml │ ├── nginx │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ └── main.yml │ │ │ ├── php │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ └── template │ └── wordpress │ ├── tasks │ └── main.yml │ └── wordpress_role.yml **************************roles/nginx/tasks/main.yml************************** - name: install dependent packages yum: name: "{{ item }}" loop: [gcc, pcre-devel, openssl-devel, zlib-devel] - name: fetch nginx dest: "{{ download_dest }}" remote_src: yes - name: create user user: {name: nginx, group: nginx, shell: /sbin/nologin} - name: compile nginx shell: chdir: /usr/local/src/nginx-1.22.1 creates: /apps/nginx/sbin/nginx - name: create pid directory file: {path: /apps/nginx/run, state: directory} - name: create subconfigure file file: {path: /apps/nginx/conf.d, state: directory} - name: modify nginx config block: [lineinfile: {path: /apps/nginx/conf/nginx.conf, regexp: ^#pid, line: pid /apps/nginx/run/nginx.pid;}, lineinfile: {path: /apps/nginx/conf/nginx.conf, insertafter: '^http {' , line: include /apps/nginx/conf.d/*.conf;}] notify: restart service - name: create a symbolic link file: {src: /apps/nginx/sbin, dest: /usr/local/sbin, force: yes, state: link} - name: service file copy: {src: files/nginx.service, dest: /usr/lib/systemd/system/} - name: modify permission file: {path: /apps/nginx, state: directory, recurse: yes, owner: nginx, group: nginx} - name: start service service: {name: nginx, state: started, enabled: yes} **************************roles/nginx/handlers/main.yml******************** - name: restart service service: {name: nginx, state: restarted} ***************************roles/php/tasks/main.yml*************************** - name: install package yum: name: "{{ item }}" loop: [php-fpm, php-mysqlnd, php-json, php-gd, php-xml] - name: update php-fpm configurationfile block: [copy: {src: files/www.conf, dest: /etc/php-fpm.d/}, copy: {src: files/php.ini, dest: /etc/}] notify: restart service - name: updata nginx subconfigurationfile copy: {src: files/php_nginx.conf, dest: /apps/nginx/conf.d/} - name: start php-fpm service service: {name: php-fpm, state: started, enabled: yes} *************************roles/php/handlers/main.yml**************** - name: restart service service: {name: php-fpm, state: restarted} **************************roles/wordpress/tasks/main.yml********************* - name: download wordpress unarchive: {src: ./wordpress-6.1.1-zh_CN.tar.gz, dest: /apps/nginx/html/, owner: nginx, group: nginx} - name: restart nginx service: {name: nginx, state: restarted} **************************roles/mysql/tasks/main.yml************************** - name: install dependent packages yum: name: "{{ item }}" loop: [libaio, numactl-libs] - name: create group group: {name: mysql} - name: create user user: {name: mysql, shell: /sbin/nologin, group: mysql} - name: prepare mysql binary file - name: create file symbolic link file: {src: /usr/local/mysql-8.0.32-linux-glibc2.12-x86_64, dest: /usr/local/mysql, force: yes, state: link} - name: create work director file: {path: /data/mysql, state: directory, owner: mysql, group: mysql} - name: prepare configure file - name: initialization mysql shell: mysqld --initialize-insecure --user=mysql --datadir=/data/mysql - name: prepare script service # shell: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld copy: {src: /usr/local/mysql/support-files/mysql.server, dest: /etc/init.d/mysqld, remote_src: yes, mode: 755} - name: system service update shell: chkconfig --add mysqld - name: start mysqld service service: {name: mysqld, state: started, enabled: yes} - name: change password shell: "mysqladmin -uroot password '12345'" - name: install PyMySQL shell: pip3 install PyMySQL - name: create wordpress database mysql_db: {login_user: root, login_host: localhost, login_password: 12345, login_unix_socket: /data/mysql/mysql.sock, name: wordpress, state: present} - name: create wordpress user mysql_user: {login_user: root, login_host: localhost, login_password: 12345, login_unix_socket: /data/mysql/mysql.sock, name: wordpress, password: 123456, host: 10.0.0.%, priv: '*.*:ALL,GRANT', state: present} *****************************files/my.cnf************************* [mysqld] datadir=/data/mysql skip_name_resolve=1 socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.log pid-file=/data/mysql/mysql.pid [client] socket=/data/mysql/mysql.sock *******************************files/nginx.service*********************** [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/apps/nginx/run/nginx.pid ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID LimitNOFILE=100000 [Install] WantedBy=multi-user.target ********************************files/php_nginx.conf******************************** server { server_name www.zyb.com; listen 80; root /apps/nginx/html/wordpress; index index.php; location ~ \.php$ { root /apps/nginx/html/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } *****************************nginx_role.yml*********************** - hosts: webserver remote_user: root roles: [nginx] ***************************php_role.yml************************** - hosts: webserver remote_user: root roles: [php] ****************************wordpress_role.yml******************** - hosts: webserver remote_user: root roles: [wordpress] ***************************mysql_role.yml******************************** - hosts: webserver remote_user: root roles: [mysql] ***************************hosts******************************** [webserver] 10.0.0.20 10.0.0.30 [webserver:vars] version=nginx-1.22.1 url=http://nginx.org/download/{{version}}.tar.gz download_dest=/usr/local/src PATH=PATH=/usr/local/mysql/bin:\$PATH ***************************lnmp.yml********************************* - import_playbook: nginx_role.yml - import_playbook: php_role.yml - import_playbook: wordpress_role.yml - import_playbook: mysql_role.yml
(2)基于zabbix角色批量部署zabbix
- name: add repository yum_repository: {name: zabbix, description: zabbix yum repo, baseurl: "https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/8/$basearch/", gpgcheck: 0} - name: clean yum cache shell: dnf clean all - name: install packages yum: name: "{{ item }}" loop: [zabbix-server-mysql, zabbix-web-mysql, zabbix-nginx-conf, zabbix-agent] - name: invoke script script: files/mysql_prepare.sh - name: import init archit and data shell: zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123456 zabbix - name: import database schema shell: mysql -e "set global log_bin_trust_function_creators = 0;" - name: config zabbix server lineinfile: {path: /etc/zabbix/zabbix_server.conf, regexp: '# DBPassword=', line: DBPassword=123456} - name: config php_nginx block: [lineinfile: {path: /etc/nginx/conf.d/zabbix.conf, regexp: '# listen', line: listen 80;}, lineinfile: {path: /etc/nginx/conf.d/zabbix.conf, regexp: '# server_name', line: server_name www.zyb.com;}] - name: config php-fpm lineinfile: {path: /etc/php-fpm.d/zabbix.conf, regexp: '; php_value[date.timezone]', line: 'php_value[date.timezone] = Asia/Shanghai'} - name: start service block: [service: {name: zabbix-server, state: started, enabled: yes}, service: {name: zabbix-agent, state: started, enabled: yes}, service: {name: php-fpm, state: started, enabled: yes}, service: {name: nginx, state: started, enabled: yes}]