ansibleenvironment-variablesjitsi

Set of environment for next using in Ansible playbook


I have a file with some configuration:

org.jitsi.videobridge.xmpp.user.shard-2.HOSTNAME=localhost
org.jitsi.videobridge.xmpp.user.shard-2.DOMAIN=auth.js.name.com
org.jitsi.videobridge.xmpp.user.shard-2.USERNAME=rrr
org.jitsi.videobridge.xmpp.user.shard-2.PASSWORD=ENrewqsd
org.jitsi.videobridge.xmpp.user.shard-2.MUC_JIDS=JvbBrewery@internal.auth.js.name.com
org.jitsi.videobridge.xmpp.user.shard-2.MUC_NICKNAME=28bjrkd046-5891-bc2a-c6426a58966r4

In the one of my next playbook I would like to use env. {siguiente_shard} to change number of shard server (shard-3, shard-4 and etc.) FOR this I have some shell command:

siguiente_shard=$(expr 1 + $(grep 'shard-' /etc/jitsi/videobridge/sip-communicator.properties | awk -F '.' '{print $6}' |uniq | sort | tail -n1 | cut -d '-' -f2 | xargs printf "%d" ))

I have a playbook:

---
- hosts: jitsi
  become: true
  tasks:

    - name: Next number of shard script
      shell: siguiente_shard=$(expr 1 + $(grep 'shard-' /etc/jitsi/videobridge/sip-communicator.properties | awk -F '.' '{print $6}' |uniq | sort | tail -n1 | cut -d '-' -f2 | xargs printf "%d" ))
      register: siguiente_shard

    - debug:
        var: "{{ siguiente_shard.stdout }}"
     
    - name: Echo my_env_var
      shell: echo $siguiente_shard
      environment:
        siguiente_shard_env: siguiente_shard.stdout
      register: siguiente_shard
    - debug:
        var: "{{ siguiente_shard.stdout }}"

But got error:

fatal: [jitsi]: FAILED! => {"msg": "template error while templating string: Expected an expression, got 'end of print statement'. String: {{}}"}

How I can set environment siguiente_shard in playbook for using it in the future in file /roles/Jitsi/vars/main.yaml

Could you please help me with advice... Thank you!


Solution

  • It's because you have one bug and one erroneous module usage; we'll start with the erroneous module usage, because that's your specific question:

    The var: is for -- as its name implies -- variable names or expressions. If you wanted to do your own jinja2 expressions, then msg: is closer to what you're looking for

    The exact error you're getting is because there is no .stdout content, and thus "{{ siguiente_shard.stdout }}" resolves to "", but (for good or bad) the var: of debug: is actually implemented as if you had written msg: "{{ var }}" and thus this snippet:

        - debug:
            var: "{{ siguiente_shard.stdout }}"
    

    is actually this snippet

        - debug:
            msg: "{{ }}"
    

    which is illegal jinja2, and thus your error message.

    The fix is to either remove the jinja2 mustaches, or change var: to msg:


    Then then bug, which is related to that, is because you have assigned a shell variable in the shell:, which is a perfectly fine and legal thing to do in shell -- but it emits no output. Thus, the register: did as it was told and stored the stdout from your shell operation, but there wasn't any

    Thus, what you likely wanted is:

    
        - name: Next number of shard script
          shell: expr 1 + $(grep 'shard-' /etc/jitsi/videobridge/sip-communicator.properties | awk -F '.' '{print $6}' |uniq | sort | tail -n1 | cut -d '-' -f2 | xargs printf "%d" )
          register: siguiente_shard
    

    ... setting aside that using such a monster shell pipeline in ansible really isn't the way ansible thinks about the world, and also that "dynamic" shard bumping logic you have places the idempotency of your playbook at risk