1
+ ---
2
+ - debug :
3
+ msg : Install Java, Create Dockerfile, and setup Java application
4
+
5
+ # create a directory to host java application and newrelic
6
+ - name : Create myJavaApp directory
7
+ file :
8
+ path : " /home/{{ ansible_user }}/myJavaApp"
9
+ state : directory
10
+
11
+ # create a sample Dockerfile
12
+ - name : Create a sample Dockerfile
13
+ copy :
14
+ content : |
15
+ FROM openjdk:8
16
+ WORKDIR /app
17
+ COPY . /app
18
+ EXPOSE 80
19
+ CMD ["java", "Main"]
20
+ dest : " /home/{{ ansible_user }}/myJavaApp/Dockerfile"
21
+ mode : " 0644"
22
+ when : ansible_os_family == 'RedHat'
23
+
24
+ - name : Install Java
25
+ yum :
26
+ name : java-1.8.0-openjdk-devel
27
+ state : present
28
+ become : yes
29
+ when : ansible_os_family == 'RedHat'
30
+
31
+ # create a java app
32
+ - name : Create sample Java application
33
+ copy :
34
+ content : |
35
+ public class Main {
36
+ public static void main(String[] args) {
37
+ // define the number of iterations for the loop
38
+ int iterations = 500;
39
+
40
+ // loop to print "Hello, Java!" multiple times with a delay
41
+ for (int i = 0; i < iterations; i++) {
42
+ System.out.println("Hello, Java!");
43
+
44
+ // Delay for 5 secsonds (5000 ms)
45
+ try {
46
+ Thread.sleep(5000);
47
+ } catch (InterruptedException e) {
48
+ e.printStackTrace();
49
+ }
50
+ }
51
+ }
52
+ }
53
+ dest : " /home/{{ ansible_user }}/myJavaApp/Main.java"
54
+ mode : 0644
55
+
56
+ # compile java file
57
+ - name : compile java code
58
+ shell : javac Main.java
59
+ args :
60
+ chdir : " /home/{{ ansible_user }}/myJavaApp"
61
+
62
+ # create Manifest
63
+ - name : Create Manifest.txt file
64
+ copy :
65
+ content : |
66
+ Main-Class: Main
67
+ dest : " /home/{{ ansible_user }}/myJavaApp/Manifest.txt"
68
+
69
+ # create jar file for the Java app
70
+ - name : Create JAR file
71
+ command : jar cfm Main.jar Manifest.txt Main.class
72
+ args :
73
+ chdir : " /home/{{ ansible_user }}/myJavaApp"
74
+
75
+ # Give all permissions to the jar file
76
+ - name : Change permissions for the JAR file
77
+ file :
78
+ path : " /home/{{ ansible_user }}/myJavaApp/Main.jar"
79
+ mode : " 0777" # Adjust the permissions as needed
0 commit comments