1,自动部署nginx
[root@aghj-11 ~]# cat nginx.yaml
# main.yml
---
- hosts: webservers
vars:
hello: Ansible
tasks:
- name: Add repo
yum_repository:
name: nginx
description: nginx repo
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
enabled: 1
- name: Install nginx
yum:
name: nginx
state: latest
- name: Copy nginx configuration file
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf
- name: Start nginx
service:
name: nginx
state: started
- name: Create wwwroot directory
file:
dest: /var/www/html
state: directory
- name: Create test page index.html
shell: echo "hello {{hello}}" > /var/www/html/index.html
添加的配置文件
[root@aghj-11 ~]# cat site.conf
server {
listen 80;
server_name www.ctnrs.com;
location / {
root /var/www/html;
index index.html;
}
}
检查yaml 文件,没有报错,就ok
[root@aghj-11 ~]# ansible-playbook nginx.yaml --syntax-check
playbook: nginx.yaml
执行
ansible-playbook nginx.yaml
访问
[root@aghj-11 test]# curl 10.1.1.38 -H "Host:www.ctnrs.com"
hello Ansible
更新配置
[root@aghj-11 test]# cat reload_nginx.yaml
---
- hosts: webservers
gather_facts: no
tasks:
- name: Copy nginx configuration file
copy:
src: site.conf
dest: /etc/nginx/conf.d/site.conf
notify:
- restart nginx
handlers:
- name: restart nginx
service: name=nginx state=reloaded
检查文件
ansible-playbook reload_nginx.yaml --syntax-check
更新文件
ansible-playbook reload_nginx.yaml
访问
[root@aghj-11 test]# curl 10.1.1.38 -H "Host:test.ctnrs.com"
hello Ansible
2,部署tomcat
[root@aghj-11 tomcat]# cat tomcat.yaml
---
- hosts: webservers
gather_facts: no
vars:
tomcat_version: 8.5.54
tomcat_install_dir: /usr/local
tasks:
- name: Install jdk1.8
yum: name=java-1.8.0-openjdk state=present
- name: Download tomcat
get_url: url=http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz dest=/tmp
- name: Unarchive tomcat-{{ tomcat_version }}.tar.gz
unarchive:
src: /tmp/apache-tomcat-{{ tomcat_version }}.tar.gz
dest: "{{ tomcat_install_dir }}"
copy: no
- name: Start tomcat
shell: cd {{ tomcat_install_dir }} &&
mv apache-tomcat-{{ tomcat_version }} tomcat8 &&
cd tomcat8/bin && nohup ./startup.sh &
检查配置
ansible-playbook tomcat.yaml --syntax-check
安装
ansible-playbook tomcat.yaml
访问
[root@aghj-11 tomcat]# curl 10.1.1.38:8080