ansuble:Role角色

tasks:专门存储任务的目录

handlers: 

templates:模板配置文件

files:拷贝的文件

vars:存放变量目录

241

安装ansible

密钥

ssh-keygen

ssh-copy-id   IP

vim /etc/ansible/hosts   ip

ansuble:Role角色_第1张图片

ansible]# cd roles/

mkdir nginx/{tasks,templates,files,handlers,vars} -p

yum -y install tree

ansuble:Role角色_第2张图片

tree nginx/

ansuble:Role角色_第3张图片

[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

ansuble:Role角色_第4张图片

vim nginx/handlers/main.yaml

  • name: start nginx

  • service: name=nginx state=restarted

vim nginx/files/default.conf

ansuble:Role角色_第5张图片

1.20nginx

vim nginx/templates/nginx.conf.j2

ansuble:Role角色_第6张图片

vim nginx/vars/main.yaml

worker_processes: 2

ansuble:Role角色_第7张图片

vim site.yaml  // 调用角色目录文件

ansuble:Role角色_第8张图片

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查看

ansuble:Role角色_第9张图片

案例二

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 

ansuble:Role角色_第10张图片

ansuble:Role角色_第11张图片

[root@localhost templates]# vi default.conf.j2 

ansuble:Role角色_第12张图片

ansuble:Role角色_第13张图片

[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主从

ansuble:Role角色_第14张图片

你可能感兴趣的:(运维)