Ansible-inlcude与tags应用【选择版本安装tomcat实战】

1、需求

通过指定标签 tags ,来说明是安装 tomcat8 还是 tomcat9
1、准备入口 main.yml 文件,然后包含 install_tomcat8.yml 以及install_tomcat9.yml
2、在执行 main.yml 时,需要通过 --tags 指明要安装的版本

2、实战-示例

2.1、编写 main.yml 入口文件

cat << 'CAT_END' > main.yaml
- name: install tomcat 8 version
  include: install_tomcat8.yaml
  tags: tomcat8
- name: install tomcat 9 version
  include: install_tomcat9.yaml
  tags: tomcat9
CAT_END

2.2、编写tomcat8 playbook 

cat << 'CAT_END' > install_tomcat8.yaml
- hosts: httpd
  vars:
    - tomcat_version: 8.5.88
    - install_dir: /usr/local
  tasks:
    - name: install java-1.8.0-openjdk
      yum: name=java-1.8.0-openjdk state=present
    - name: download tomcat
      get_url:
        url: "https://dlcdn.apache.org/tomcat/tomcat-8/v{{tomcat_version}}/bin/apache-tomcat-{{tomcat_version}}.tar.gz"
        dest: /tmp
        validate_certs: no
    - name: "unarchive tomcat-{{tomcat_version}}"
      unarchive:
        src: "/tmp/apache-tomcat-{{tomcat_version}}.tar.gz"
        dest: "{{install_dir}}/"
        copy: no
    - name: link tomcat home
      file:
        src: "{{install_dir}}/apache-tomcat-{{tomcat_version}}"
        path: "{{install_dir}}/tomcat8"
        state: link
    - name: start tomcat server
      shell:
        chdir: "{{install_dir}}/tomcat8/bin"
        cmd: nohup ./startup.sh &
CAT_END

2.3、编写tomcat9 playbook 

cat << 'CAT_END' > install_tomcat9.yaml
- hosts: httpd
  vars:
    - tomcat_version: 9.0.75
    - install_dir: /usr/local
  tasks:
    - name: install java-1.8.0-openjdk
      yum: name=java-1.8.0-openjdk state=present
    - name: download tomcat
      get_url:
        url: "https://dlcdn.apache.org/tomcat/tomcat-9/v{{tomcat_version}}/bin/apache-tomcat-{{tomcat_version}}.tar.gz"
        dest: /tmp
        validate_certs: no
    - name: "unarchive tomcat-{{tomcat_version}}"
      unarchive:
        src: "/tmp/apache-tomcat-{{tomcat_version}}.tar.gz"
        dest: "{{install_dir}}/"
        copy: no
    - name: link tomcat home
      file:
        src: "{{install_dir}}/apache-tomcat-{{tomcat_version}}"
        path: "{{install_dir}}/tomcat9"
        state: link
    - name: start tomcat server
      shell:
        chdir: "{{install_dir}}/tomcat9/bin"
        cmd: "nohup ./startup.sh &"
CAT_END

2.4、运行测试

ansible-playbook main.yaml --tags tomcat8
ansible-playbook main.yaml --tags tomcat9

 

posted @ 2023-05-13 22:11  小粉优化大师  阅读(16)  评论(0编辑  收藏  举报