Posts Jenkins ant script to ssh and chef servers by using ssh key
Post
Cancel

Jenkins ant script to ssh and chef servers by using ssh key

  • Call target from pom.xml
1
2
3
4
5
6
7
<configuration>
    <target>
        <ant antfile="${basedir}/build.xml">
            <target name="app-house-keeping" />
        </ant>
    </target>
</configuration>
  • Setup profile and add servers for that environment
1
2
3
4
5
6
7
8
9
<profile>
    <id>dev</id>
    <properties>
        <classifier.type>DEV</classifier.type>
        <server1.hostname>server1</server1.hostname>
        <server2.hostname>server2</server2.hostname>
        <server3.hostname>server3</server3.hostname>
    </properties>
</profile>
  • Ant sort server host names to list
1
 <propertyselector property="hostnames" match="server([^\.]*)\.hostname"/>
  • Ant retry on command failure
1
2
3
<retry retrycount="500" retrydelay="10000">
    <get src="https://${node}:8443/health" dest="/dev/null"/>
</retry>
  • Ant Loop command
1
2
<for list="${sorted.hostnames}" param="hostname">
</for>
  • Ant if else condition
1
2
3
4
5
6
7
8
9
<if>
    <equals arg1="${stop.server}" arg2="true" />
    <then>	
       ....
    </then>
    <else>
       ....
    </else>
</if>
  • Ant ssh and execute commands on remote
1
2
3
4
5
6
7
<sshexec 
    host="${node}" 
    username="server" 
    keyfile="${user.home}/.ssh/id_rsa"  
    command=". .bash_profile; server stop;" 
    trust="true" 
    timeout="10000"/>

Source

This post is licensed under CC BY 4.0 by the author.