tasks:专门存储任务的目录
handlers:
templates:模板配置文件
files:拷贝的文件
vars:存放变量目录
241
安装ansible
密钥
ssh-keygen
ssh-copy-id IP
vim /etc/ansible/hosts ip
ansible]# cd roles/
mkdir nginx/{tasks,templates,files,handlers,vars} -p
yum -y install tree
tree nginx/
[root@ansible roles]# vi nginx/files/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
vim nginx/tasks/main.yaml
vim nginx/handlers/main.yaml
name: start nginx
service: name=nginx state=restarted
vim nginx/files/default.conf
1.20nginx
vim nginx/templates/nginx.conf.j2
vim nginx/vars/main.yaml
worker_processes: 2
vim site.yaml // 调用角色目录文件
ansible-playbook site.yaml --syntax-check
ansible-playbook site.yaml
案例一
使用facts变量
[root@ansible roles]# mkdir file/{tasks,templates,files,handlers,vars} -p
[root@ansible roles]# vim file/templates/file.conf.j2
hostname: {{ ansible_fqdn }}
memtatol: {{ ansible_memtotal_mb }}
memfree: {{ ansible_memfree_mb}}
[root@ansible roles]# vim file/tasks/main.yaml
- name: template file
template: src=file.conf.j2 dest=/opt/
[root@ansible roles]# vim site.yaml
- hosts: web
user: root
roles:
- file
[root@ansible roles]# ansible-playbook --syntax-check site.yaml
[root@ansible roles]# ansible-playbook site.yaml
web2查看
案例二
nginx负载均衡变量
[root@localhost ~]# vi /etc/yum.repos.d/nginx.repo
[root@localhost ~]# systemctl stop nginx // 停1.20nginx
[root@localhost ~]# yum -y remove nginx
[root@localhost ~]# yum -y install nginx//安装nginx1.24
模板配置文件
[root@localhost ~]# cp /etc/nginx/nginx.conf lb/templates/nginx.conf.j2
[root@localhost ~]# cp /etc/nginx/conf.d/default.conf lb/templates/default.conf.j2
[root@localhost ~]# cd lb/templates/
[root@localhost templates]# vim nginx.conf.j2
[root@localhost templates]# vi default.conf.j2
[root@localhost templates]# vim ../vars/main.yaml
nginx_port: 81
[root@localhost templates]# vim ../tasks/main.yaml
- name: template conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: template config
template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf
[root@ansible roles]# vi site.yaml
- hosts: web
user: root
roles:
- lb
[root@ansible roles]# ansible-playbook --syntax-check site.yaml
案例三:keepalived主从
或