ansible playbook简单应用
1、Role具体实现Lnmp
以LNMP为例,建议使用相同版本的主机,且需要设置基于key验证
1.1、相关准备
#创建文件夹
[root@rocky8 ~]#mkdir /opt/roles/{nginx,mysql,php-fpm,wordpress}/{tasks,handlers,templates} -pv
#准备相关文件
[root@rocky8 ~]#cd /opt
[root@rocky8 opt]#cp /etc/ansible/hosts .
[root@rocky8 opt]#cp /etc/ansible/ansible.cfg .
[root@rocky8 opt]#vim ansible.cfg
inventory = ./hosts #修改主机清单路径为当前目录
[root@rocky8 opt]#vim hosts #准备主机清单
[webservers]
10.0.0.8
10.0.0.18
[webservers:vars]
version="1.20.2"
url="http://nginx.org/download/nginx-{{ version }}.tar.gz"
install_dir="/apps/nginx"
user=www
uid=88
gid=88
group=www
[appservers]
10.0.0.101
10.0.0.102
[dbservers]
10.0.0.28
[root@rocky8 opt]#anxible all -m ping #测试连接
1.2、具体拆分
1.2.1、nginx
#task
[root@rocky8 opt]#cd roles/nginx/
[root@rocky8 nginx]#vim tasks/main.yml #起始位置,文件名固定
- name: install packages
yum:
name: "{{ item }}"
loop:
- gcc
- make
- pcre-devel
- openssl-devel
- zlib-devel
- perl-ExtUtils-Embed
- name: get nginx source
unarchive:
src: "{{ url }}"
dest: "/usr/local/src"
remote_src: yes
- name: compile and install
shell:
cmd: "./configure --prefix={{ install_dir }} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install"
chdir: "/usr/local/src/nginx-{{ version }}"
creates: "{{install_dir}}/sbin/nginx"
- name: create {{ group }}
group:
name: "{{ group }}"
gid: "{{ gid }}"
- name: create {{ user }}
user:
name: "{{ user }}"
uid: "{{ uid }}"
group: "{{ group }}"
system: yes
- name: copy config
template:
src: nginx.conf.j2
dest: "{{install_dir}}/conf/nginx.conf"
notify:
- restart nginx
- name: config dir
file:
path: "{{install_dir}}/conf.d"
state: directory
- mame: config file mode
file:
path: {{install_dir}}"
owner: "{{ user }}"
group: "{{ group }}"
recurse: yes
- name: check nginx config
shell:
cmd: "{{install_dir}}/sbin/nginx -t"
register: check_nginx_config
changed_when:
- check_nginx_config.stdout.find('successful')
- false
- name: service file
template:
src: nginx.service.j2
dest: /lib/systemd/system/nginx.service
- name: start nginx
service:
name: nginx
state: started
enabled: yes
#配置文件准备
[root@rocky8 nginx]#cp /etc/ansible/templates/nginx.conf.j2 /opt/roles/nginx/templates/
[root@rocky8 nginx]#vim templates/nginx.conf.j2 #注意是仅这些内容,而不是更改
user {{ user }};
worker_processes auto;
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include {{install_dir}}/conf.d/*.conf;
default_type application/octet-stream;
}
#准备service文件
[root@rocky8 nginx]#cp /usr/lib/systemd/system/nginx.service templates/nginx.service.j2
[root@rocky8 nginx]#vim templates/nginx.service.j2 #可以yum安装后面拿来更改
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile={{ install_dir }}/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f {{ install_dir }}/logs/nginx.pid
ExecStartPre={{ install_dir }}/sbin/nginx -t
ExecStart={{ install_dir }}/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
root_path=/data/wordpress #php需要用到的变量
fqdn=www.wang.org
app=wordpress-6.2.2-zh_CN #wordpress所用变量
[Install]
WantedBy=multi-user.target
#拆分handlers
[root@rocky8 opt]#vim roles/nginx/handlers/main.yml
- name: restart nginx
service:
name: nginx
state: restartd
调用nginx
[root@rocky8 opt]#vim nginx_role.yml
- hosts: webservers
remote_user: root
roles:
- nginx
[root@rocky8 opt]#tree
.
├── ansible.cfg
├── hosts
├── index.html
├── nginx_role.yml
└── roles
├── mysql
│ ├── handlers
│ ├── tasks
│ └── templates
├── nginx
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ └── templates
│ ├── nginx.conf.j2
│ └── nginx.service.j2
├── php-fpm
│ ├── handlers
│ ├── tasks
│ └── templates
└── wordpress
├── handlers
├── tasks
└── templates
[root@rocky8 opt]#ansible-playbook -i hosts nginx_role.yml
1.2.2、php
[root@rocky18 ~]#scp /etc/php.ini /etc/php-fpm.d/www.conf 10.0.0.152:/opt/roles/php-fpm/templates/
[root@rocky8 templates]#mv php.ini php.ini.j2
[root@rocky8 templates]#grep -Ev "^;|^ *$" www.conf > www.conf.j2 #调整文件格式,不然会报错格式不符
[root@rocky8 templates]#rm -f www.conf
[root@rocky8 templates]#vim www.conf.j2 #监听调整
user = {{ user }}
group = {{ group }}
listen = 127.0.0.1:9000
[root@rocky8 templates]#vim php.ini.j2 #相关优化
date.timezone = Asia/Shanghai
max_execution_time = 360
max_input_time = 600
post_max_size = 80M
upload_max_filesize = 80M
[root@rocky8 ~]#cd /opt/roles/php-fpm
[root@rocky8 php-fpm]#vim templates/php-fpm.conf.j2
server {
listen 80;
server_name {{ fqdn }};
root {{ root_path }};
index index.php;
location ~ \.php$ { #注意是仅php文件
root {{ root_path }};
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@rocky8 php-fpm]#vim tasks/main.yml
- name: install packages
yum:
name: "{{ item }}"
loop:
- php-fpm
- php-mysqld
- php-json
- php-xml
- php-pecl-zip
- php-gd
- name: php path
file:
path: /var/lib/php/
owner: "{{ user }}"
group: "{{ group }}"
recurse: yes
- name: config php.ini
template:
src: php.ini.j2
dest: /etc/php.ini
notify: restart php-fpm
- name: config www.conf
template:
src: www.conf.j2
dest: /etc/php-fpm.d/www.conf
notify: restart php-fpm
- name: config nginx
template:
src: php-fpm.conf.j2
dest: "{{ install_dir }}/conf.d/php-fpm.conf"
notify: restart nginx
- name: create php datadir
file:
name: "{{ root_path }}"
state: "directory
owner: "{{ user }}"
group: "{{ group }}"
- name: test page
copy:
src: test.php
dest: "{{ root_path }}/"
- name: start php-fpm
service:
name: php-fpm
state: started
enabled: yes
[root@rocky8 php-fpm]#vim handlers/main.yml
- name: restart php-fpm
service:
name: php-fpm
state: restarted
- name: restart nginx
service:
name: nginx
state: restarted
[root@rocky8 php-fpm]#mkdir files
[root@rocky8 php-fpm]#vim files/test.php #测试页面
<?php
phpinfo();
?>
调用php
[root@rocky8 opt]#vim php-fpm_role.yml
- hosts: webservers
remote_user: root
roles:
- php-fpm
[root@rocky8 opt]#tree
.
├── ansible.cfg
├── hosts
├── index.html
├── nginx_role.yml
└── roles
├── mysql
│ ├── handlers
│ ├── tasks
│ └── templates
├── nginx
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ └── templates
│ ├── nginx.conf.j2
│ └── nginx.service.j2
├── php-fpm
│ ├── files
│ │ └── test.php
│ ├── handlers
│ │ └── main.yml
│ ├── php-fpm_role.yml
│ ├── tasks
│ │ └── main.yml
│ └── templates
│ ├── php-fpm.conf.j2
│ ├── php.ini.j2
│ └── www.conf.j2
└── wordpress
├── handlers
├── tasks
└── templates
18 directories, 15 files
[root@rocky8 opt]#ansible-playbook php-fpm_role.yml
[root@rocky8 opt]#curl -Hhost:www.wang.org http://10.0.0.8/test.php
1.2.3、mysql
提前准备好二进制包
[root@rocky8 mysql]#pwd
/opt/roles/mysql
[root@rocky8 mysql]#mkdir files vars
#准备配置文件
[root@rocky8 mysql]#vim files/my.cnf
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
#变量定义,也可和前面一样在主机清单中定义
[root@rocky8 mysql]#vim vars/main.yml
mysql_version: 8.0.23
mysql_file: mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.xz
mysql_root_password: 123456
#tasks
[root@rocky8 mysql]#vim tasks/main.yml
- name: install packages
yum:
name:
- libaio
- numactl-libs
- name: create mysql group
group: name=mysql gid=306
- name: create mysql user
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
- name: copy tar to remote host and file mode
unarchive: src={{mysql_file}} dest=/usr/local/ owner=root group=root
- name: create linkfile /usr/local/mysql
file: src=/usr/local/mysql-{{ mysql_version }}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
- name: data dir
shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
tags: data
- name: config my.cnf
copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: enable service
shell: chkconfig --add mysqld;/etc/init.d/mysqld start
tags: service
- name: change password
shell: /usr/local/mysql/bin/mysqladmin -uroot password {{mysql_root_password}}
调用mysql
[root@rocky8 opt]#vim php-fpm_role.yml
- hosts: webservers
remote_user: root
roles:
- mysql
1.2.4、wordpress
提前准备好wordpress软件包,建议不要下载最新版本,以防有bug影响实验
[root@rocky8 ~]#cd /opt/roles/wordpress/
[root@rocky8 wordpress]#mkdir files #把wordpress软件包存放在此
[root@rocky8 wordpress]#vim tasks/main.yml
- name: down wordpress
unarchive:
src: "{{ app }}.tar.gz"
dest: /data/
owner: "{{ user }}"
group: "{{ group }}"
[root@rocky8 opt]#vim wordpress_role.yml
- hosts: webservers
remote_user: root
roles:
- wordpress
#- nginx #都在本机的话也可以写在一个调用文件中,建议还是分开
#- php-fpm
[root@rocky8 opt]#vim lnmp_role.yml #全部调用
- include: mhinx_role.yuml
- include: php-fpm_role.yml
- include: mysql_role.yml
- include: wordpress_role.yml
[root@rocky8 opt]#mkdir roles/wordpress/meta/
[root@rocky8 opt]#vim roles/wordpress/meta/main.yml #也可创建wordpress的依赖部署,即在部署wordpress时需先调用该文件里指定的role执行完成再执行wordpress
dependencies:
- role: nginx
- role: mysql
- role: php-fpm
完整目录
[root@rocky8 opt]#tree
.
├── ansible.cfg
├── hosts
├── index.html #忘删了。。。。。。当它不存在,谢谢
├── lnmp_role.yml
├── nginx_role.yml
├── php-fpm_role.yml
├── roles
│ ├── mysql
│ │ ├── files
│ │ │ └── my.cnf
│ │ ├── handlers
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ └── vars
│ │ └── main.yml
│ ├── nginx
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ ├── nginx.conf.j2
│ │ └── nginx.service.j2
│ ├── php-fpm
│ │ ├── files
│ │ │ └── test.php
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── php-fpm_role.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ ├── php-fpm.conf.j2
│ │ ├── php.ini.j2
│ │ └── www.conf.j2
│ └── wordpress
│ ├── files
│ │ └── wordpress-6.2.2-zh_CN.tar.gz
│ ├── handlers
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ └── templates
└── wordpress_role.yml
22 directories, 24 files
[root@rocky8 opt]#ansible-playbook lnmp_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}]