[root@ansible ansible]# cat jenkins.yml
- hosts: ansible-web1
user: root
vars_files:
- /etc/ansible/vars/file.yml
tasks:
##配置JDK,上傳jdk、tomcat的安裝包到/usr/src
- name: configure JDK1.8
copy: src={{ src_jdk_path }} dest={{ dest_jdk_path }}
- name: unzip JDK
shell: tar -xvzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local
- name: rename to java
shell: mv /usr/local/jdk1.8.0_211 /usr/local/java
- name: configure JDK envirement1
shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile
- name: configure JDK envirement2
shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
##Tomcat
- name: copy tomcat
copy: src={{ src_tomcat_path }} dest={{ dest_tomcat_path }}
- name: unzip tomcat
shell: tar -xvzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local
- name: rename to tomcat
shell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat
- name: copy startup file
copy: src=/usr/src/startup.sh dest=/usr/local/tomcat/bin
##Jenkins
- name: copy jenkins
copy: src=/usr/src/jenkins.war dest=/usr/local/tomcat/webapps/
notify: start jenkins
handlers:
- name: start jenkins
shell: nohup /usr/local/tomcat/bin/startup.sh &
或者直接修改被控節點的啓動文件
- hosts: webserver
user: root
tasks:
- name: copy file
copy: src=/usr/src/jdk-8u211-linux-x64.tar.gz dest=/usr/src
- name: unzip
shell: tar -xzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local
- name: as file
shell: mv /usr/local/jdk1.8.0_211 /usr/local/java
- name: configure env1
shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile
- name: configure env2
shell: echo 'PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH' >> /etc/profile
- name: copy tomcat
copy: src=/usr/src/apache-tomcat-8.5.45.tar.gz dest=/usr/src
- name: unzip tomcat
shell: tar -xzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local/
- name: mv tomcat
shell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat
- name: sed tomcat
shell: sed -ri '1a source /etc/profile' /usr/local/tomcat/bin/startup.sh
notify: start tomcat
handlers:
- name: start tomcat
shell: nohup /usr/local/tomcat/bin/startup.sh &