linuxenvironment-variablessystemctl

systemd show <service-name> --property=environment" shows only one value


I am working on a script to extract a specific environment variable - CONFLUENT_SECURITY_MASTER_KEY defined in the unit for the service confluent-server.service and use that value in the script to perform other actions.

I tried systemctl show confluent-server.service --property=Environment but it shows only the first ENVIRONMENT= value - Environment=KAFKA_HEAP_OPTS. There are 5 ENVIRONMENT variables defined for the service. How do I list all 5 and extract the value of CONFLUENT_SECURITY_MASTER_KEY.

I thought the original question was fully explaining the situation, but have added more details.

Below is the output of the command

`systemctl show confluent-server.service --property=Environment`

This is the whole output. Modified the masterkey value, but format is same

Environment=KAFKA_OPTS=-Djdk.tls.ephemeralDHKeySize=2048 -javaagent:/opt/jolokia/jolokia.jar=config=/etc/kafka/kafka_jolokia.properties KAFKA_HEAP_OPTS=-Xms6g -Xmx6g -XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX:MaxMetaspaceFreeRatio=80 KAFKA_LOG4J_OPTS=-Dlog4j.configuration=file:/etc/kafka/log4j.properties LOG_DIR=/var/log/kafka CONFLUENT_SECURITY_MASTER_KEY=abcdefghYRkHGYhKM4CRQOBVt9C9wKp8QAZ3i5Nzs=

@Thiru has already provided the answer. And I have accepted it as it resolves the issue


Solution

  • As discussed, here is the solution.

    Using awk you can just filter the value you would want like below but note that it works fine only if the variable CONFLUENT_SECURITY_MASTER_KEY output value is at the end of the line.

    # systemctl show confluent-server.service --property=Environment | awk -F "CONFLUENT_SECURITY_MASTER_KEY=" '{print $2}'
    abcdefghYRkHGYhKM4CRQOBVt9C9wKp8QAZ3i5Nzs=
    

    If the CONFLUENT_SECURITY_MASTER_KEY value comes in the middle of the line. example :

    systemctl show confluent-server.service --property=Environment
    Environment=KAFKA_OPTS=-Djdk.tls.ephemeralDHKeySize=2048 -javaagent:/opt/jolokia/jolokia.jar=config=/etc/kafka/kafka_jolokia.properties KAFKA_HEAP_OPTS=-Xms6g -Xmx6g -XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX:MaxMetaspaceFreeRatio=80  CONFLUENT_SECURITY_MASTER_KEY=abcdefghYRkHGYhKM4CRQOBVt9C9wKp8QAZ3i5Nzs= KAFKA_LOG4J_OPTS=-Dlog4j.configuration=file:/etc/kafka/log4j.properties LOG_DIR=/var/log/kafka
    

    Then the awk result would be as below which is not right . (of course using the awk we can further filter the value like this : awk -F "CONFLUENT_SECURITY_MASTER_KEY=" '{print $2}' | awk '{print $1}')

    systemctl show confluent-server.service --property=Environment | awk -F "CONFLUENT_SECURITY_MASTER_KEY=" '{print $2}'
    abcdefghYRkHGYhKM4CRQOBVt9C9wKp8QAZ3i5Nzs= KAFKA_LOG4J_OPTS=-Dlog4j.configuration=file:/etc/kafka/log4j.properties LOG_DIR=/var/log/kafka
    

    Simple workaround is that we can simply split the lines using sed (replace space with new line /n) and then we can filter the value

    systemctl show confluent-server.service --property=Environment | sed 's/ /\n/g'
    Environment=KAFKA_OPTS=-Djdk.tls.ephemeralDHKeySize=2048
    -javaagent:/opt/jolokia/jolokia.jar=config=/etc/kafka/kafka_jolokia.properties
    KAFKA_HEAP_OPTS=-Xms6g
    -Xmx6g
    -XX:MetaspaceSize=96m
    -XX:+UseG1GC
    -XX:MaxGCPauseMillis=20
    -XX:InitiatingHeapOccupancyPercent=35
    -XX:G1HeapRegionSize=16M
    -XX:MinMetaspaceFreeRatio=50
    -XX:MaxMetaspaceFreeRatio=80
    
    CONFLUENT_SECURITY_MASTER_KEY=abcdefghYRkHGYhKM4CRQOBVt9C9wKp8QAZ3i5Nzs=
    KAFKA_LOG4J_OPTS=-Dlog4j.configuration=file:/etc/kafka/log4j.properties
    LOG_DIR=/var/log/kafka
    

    and finally do a grep

    systemctl show confluent-server.service --property=Environment | sed 's/ /\n/g' | grep CONFLUENT_SECURITY_MASTER_KEY
    CONFLUENT_SECURITY_MASTER_KEY=abcdefghYRkHGYhKM4CRQOBVt9C9wKp8QAZ3i5Nzs=