Ansible does not give the output as same as the script itself

There is a script that I need to run on a specified server. To do that I use ansible playbooks. Only purpose of the playbook is running the script and getting the output but it is not same when I run the script on its own server.

This is the script I run:

echo ""
cd /kafka/confluent-7.0.1/bin
connectors=$(curl --silent -X GET http://connector-server:8083/connectors)
total=$(echo $connectors|jq '. | length')

counter=0
while [[ $counter < $total ]]
do
    single=$(echo $connectors|jq .[$counter] -r)
    if [[ $single = sink* ]] ;
    then
       silent=$(source kafka-consumer-groups --describe --group connect-$single --bootstrap-server kafka-server:9072 --command-config /kafka/vty/cli_sasl.properties)
       partitions=$(echo -n "$silent" | grep -c '^')
       partitions=$(( partitions - 2 ))

       part_counter=0
       echo "${single}" 
       while [[ $part_counter < $partitions ]]
       do
           lagpos=$(( 15 + $part_counter * 9 ))
           lag=$(echo $silent | cut -d " " -f $lagpos)
           echo "partition${part_counter}: ${lag} " 
           ((part_counter++))
       done
    echo ""
    fi
    ((counter++))
done

It gives an simple output like:

sink_connector1
partition0: 0 

sink_connector2
partition0: 2 
partition1: 3 
partition2: 3 

sink_connector3
partition0: 0 

And here is the playbook I run on ansible server:


- hosts: all
  gather_facts: no
  connection: ssh
  become: yes
  become_user: kafka
  tasks:
    - name: Run Command
      shell: cd /kafka/vty && ./lagcheck.sh
      register: out
    - debug: var=out.stdout_lines

But in the output lag values are empty:

TASK [Run Command] ************************************************************************************************************************************************************
changed: [connector-server]

TASK [debug] ******************************************************************************************************************************************************************
ok: [connector-server] => {
    "out.stdout_lines": [
        "", 
        "sink_connector1", 
        "partition0:  ", 
        "partition1:  ", 
        "partition2:  ", 
        "", 
        "sink_connector2", 
        "partition0:  ", 
        "partition1:  ", 
        "partition2:  ", 
        "", 
        "sink_connector3", 
        "partition0:  ", 
        "partition1:  ", 
        "partition2:  "
    ]
}

What could be the reason? I was suspicious about no-new-line echos but it does not matter.

Upvotes: 0

Views: 171

Answers (1)

mdaniel
mdaniel

Reputation: 33203

Due to all the "bash-isms" in the script, it is likely there is a difference between your interactive user's shell configuration and the non-interactive user that ansible uses. Explicitly using bash to run the script can tell ansible about your preference:

- hosts: all
  gather_facts: no
  connection: ssh
  become: yes
  become_user: kafka
  tasks:
    - name: Run Command
      shell: cd /kafka/vty && bash ./lagcheck.sh
      register: out

Upvotes: 1

Related Questions