ansible 基础playbook篇_nginx_tomcat

 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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值