Thank you for reading this post, don't forget to subscribe!
Установка tomcat с помощью роли ансибл.
создаём структуру директорий:
mkdir -p /etc/ansible/{playbooks/roles_play,roles/tomcat8/{defaults,handlers,meta,tasks,templates}}
директория роли:
/etc/ansible/roles/tomcat8
содержимое директории
/etc/ansible/roles/tomcat8/tasks:
cat addusers.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- name: Ensure group tomcat exists group: name: "{{ item }}" state: present with_items: - tomcat - name: add user tomcat user: name: "{{ item }}" group: tomcat shell: /sbin/nologin append: yes with_items: - tomcat |
cat systemct_conf.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
- name: Copy tomcat service config to remote host for centos 7 template: src=/etc/ansible/roles/tomcat8/templates/tomcat.service dest=/etc/systemd/system/tomcat.service notify: - restart tomcat when: - ansible_distribution_major_version == "7" - ansible_distribution == "CentOS" - name: Copy tomcat service config to remote host for centos 6 template: src: /etc/ansible/roles/tomcat8/templates/tomcat.service.centos6 dest: /etc/init.d/tomcat force: no notify: - restart tomcat when: - ansible_distribution_major_version == "6" - ansible_distribution == "CentOS" - name: Change permissions on /etc/init.d/tomcat file: dest: /etc/init.d/tomcat owner: tomcat group: tomcat mode: u=rwX,g=rX,o=rX when: - ansible_distribution_major_version == "6" - ansible_distribution == "CentOS" - name: Add tomcat to chkconfig shell: "chkconfig --add tomcat" when: - ansible_distribution_major_version == "6" - ansible_distribution == "CentOS" |
cat defaultpackages.yml
данная проверка производится по средтсвам shell команды
(работает но не кошерно)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
- name: Check install java shell: java -version 2>&1 | grep version | awk '{print $1}' | sed 's/"//g' register: java_result - debug: var: java_result - name: Install default packages yum: name: "{{item}}" state: present with_items: - java-1.8.0-openjdk when: java_result.stdout != "java" and java_result.stdout != "openjdk" - name: Copy tomcat src to remote host copy: src: /etc/ansible/roles/tomcat8/templates/apache-tomcat-8.5.41.tar.gz dest: "{{ directory_for_tomcat }}" - name: Unarchive a file that is already on the remote machine unarchive: src: apache-tomcat-8.5.41.tar.gz dest: "{{ directory_for_tomcat }}/apache-tomcat-8.5.41.tar.gz" remote_src: yes - name: Move dir and remove archive shell: mv {{ directory_for_tomcat }}/apache-tomcat-8.5.41 {{ directory_for_tomcat }}/tomcat && rm -rf {{ directory_for_tomcat }}/apache-tomcat-8.5.41.tar.gz - name: Change permissions on tomcat folder file: dest: "{{ directory_for_tomcat }}/tomcat/" owner: tomcat group: tomcat mode: u=rwX,g=rX,o=rX recurse: yes |
Рассмотрим плейбук который проверяет наличие 2 пакетов:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
--- - hosts: 192.168.1.175 user: ansible become: true gather_facts: yes ignore_errors: yes become_method: sudo tasks: - name: "Check if python and tcpdump is installed" package_facts: manager: "auto" - name: "python or tcpdump is installed" debug: msg: "Packpage FOUND" when: - "'python' in ansible_facts.packages" - "'tcpdump' in ansible_facts.packages" - name: "python or tcpdump is NOT installed" debug: msg: "Packpage NOT found" when: "'python' not in ansible_facts.packages or 'tcpdump' not in ansible_facts.packages" |
поэтому используем данный формат для defaultpackages
cat defaultpackages.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
- name: "Check java installed" package_facts: manager: "auto" - name: "if java installed all OK" debug: msg: "Packpage FOUND and all ok" when: "'java' in ansible_facts.packages or 'openjdk' in ansible_facts.packages or 'java-1.8.0-openjdk' in ansible_facts.packages" - name: "java NOT installed" debug: msg: "Packpage NOT installed" when: - "'java' not in ansible_facts.packages" - "'openjdk' not in ansible_facts.packages" - "'java-1.8.0-openjdk' not in ansible_facts.packages" - name: Install java yum: name: "java-1.8.0-openjdk" state: present when: "'java' not in ansible_facts.packages or 'openjdk' not in ansible_facts.packages or 'java-1.8.0-openjdk' not in ansible_facts.packages" - name: Copy tomcat src to remote host copy: src: /etc/ansible/roles/tomcat8/templates/apache-tomcat-8.5.41.tar.gz dest: "{{ directory_for_tomcat }}" - name: Unarchive a file that is already on the remote machine unarchive: src: apache-tomcat-8.5.41.tar.gz dest: "{{ directory_for_tomcat }}" remote_src: yes - name: Move dir and remove archive shell: mv {{ directory_for_tomcat }}/apache-tomcat-8.5.41 {{ directory_for_tomcat }}/tomcat && rm -rf {{ directory_for_tomcat }}/apache-tomcat-8.5.41.tar.gz - name: Change permissions on tomcat folder file: dest: "{{ directory_for_tomcat }}/tomcat/" owner: tomcat group: tomcat mode: u=rwX,g=rX,o=rX recurse: yes |
1 2 3 4 5 6 7 8 9 10 |
cat main.yml --- - import_tasks: addusers.yml tags: adduser - import_tasks: defaultpackages.yml tags: default - import_tasks: systemct_conf.yaml tags: tomcat_systemctl_conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
cat /etc/ansible/roles/tomcat8/templates/tomcat.service # Systemd unit file for tomcat [Unit] Description=Apache Tomcat Web Application Container After=syslog.target network.target [Service] Type=forking #Environment=JAVA_HOME=/usr/bin/java Environment=CATALINA_PID={{ directory_for_tomcat}}/tomcat/temp/tomcat.pid Environment=CATALINA_HOME={{ directory_for_tomcat}}/tomcat Environment=CATALINA_BASE={{ directory_for_tomcat}}/tomcat Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC' Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom' ExecStart={{ directory_for_tomcat}}/tomcat/bin/startup.sh ExecStop=/bin/kill -15 $MAINPID User=tomcat Group=tomcat UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target |
cat /etc/ansible/roles/tomcat8/templates/tomcat.service.centos6
#!/bin/bash
export CATALINA_HOME={{ directory_for_tomcat}}/tomcat
service_name="tomcat"
start() {
echo "Starting Tomcat …"
/bin/su -s /bin/bash tomcat -c $CATALINA_HOME/bin/startup.sh
}
stop() {
echo "Stopping Tomcat …"
/bin/su -s /bin/bash tomcat -c $CATALINA_HOME/bin/shutdown.sh
}
status() {
if (( $(ps -ef | grep -v grep | grep $service_name | wc -l) > 0 )); then
echo "$service_name is running!!!"
else
echo "$service_name is down!!!"
fi
}
case $1 in
start|stop|status) $1;;
restart) stop; start;;
*) echo "Usage : $0 <start|stop|restart>"; exit 1;;
esac
exit 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<strong>cat /etc/ansible/playbooks/roles_play/tomcat.yml </strong>--- - hosts: 192.168.1.175 become: true ignore_errors: yes become_method: sudo gather_facts: yes vars: directory_for_tomcat: /var roles: - tomcat8 в переменной directory_for_tomcat указывается директория в которую будет установлен tomcat |
В директорию /etc/ansible/roles/tomcat8/templates/ скачиваем архив томката apache-tomcat-8.5.41.tar.gz