简体   繁体   English

Ansible playbook 中下次使用的环境集

[英]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.在我的下一本剧本中,我想使用 env。 {siguiente_shard} to change number of shard server (shard-3, shard-4 and etc.) FOR this I have some shell command: {siguiente_shard}更改分片服务器的数量(shard-3、shard-4 等)为此我有一些 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" ))

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我如何在剧本中设置环境siguiente_shard以便将来在文件/roles/Jitsi/vars/main.yaml使用它

Could you please help me with advice... Thank you!你能帮我提供建议吗...谢谢!

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. var:用于——顾名思义——变量名或表达式。 If you wanted to do your own jinja2 expressions, then msg: is closer to what you're looking for如果您想做自己的 jinja2 表达式,那么msg:更接近您要查找的内容

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:您得到的确切错误是因为没有.stdout内容,因此"{{ siguiente_shard.stdout }}"解析为"" ,但是(无论好坏) var: of debug:实际上是实现的你写了msg: "{{ var }}"所以这个片段:

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

is actually this snippet实际上是这个片段

    - debug:
        msg: "{{ }}"

which is illegal jinja2, and thus your error message.这是非法的 jinja2,因此是您的错误消息。

The fix is to either remove the jinja2 mustaches, or change var: to msg:修复方法是删除 jinja2 胡子,或将var:更改为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 .然后,与此相关的 bug 是因为您在shell:分配了一个shell变量,这是在 shell 中做的非常好的和合法的事情 - 但它不发出output Thus, the register: did as it was told and stored the stdout from your shell operation, but there wasn't any因此, register:按照它的指示执行并存储了来自您的 shell 操作的标准输出,但没有任何

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 ...撇开在 ansible 中使用这样一个怪物 shell 管道确实不是 ansible 思考世界的方式,而且你拥有的“动态”碎片碰撞逻辑使你的剧本的幂等性处于危险之中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM