代码之家  ›  专栏  ›  技术社区  ›  George Shuklin

为ansible中的字符串生成数字哈希

  •  0
  • George Shuklin  · 技术社区  · 7 年前

    我需要为给定的字符串生成一个唯一的TCP端口号(我需要这个来模拟来自服务器的响应,每个字符串都是唯一的)。

    我想使用一个字符串参数作为这个端口号的源。换句话说,我需要从任意字符串生成给定范围(1000-32767)内的数值散列。

    我可以在python中毫无问题地做到这一点,但我不知道如何在ansible中做到这一点。

    有没有办法从ansible中的字符串生成稳定的数值范围内哈希?

    一出戏剧的例子:

     - hosts: localhost
       gather_facts: no
       tasks:
         - debug: msg="Hash for {{ item }} is {{ item |HELP_ME_HERE }}"
           with_items:
            - string1
            - string_two
    
    0 回复  |  直到 7 年前
        1
  •  1
  •   Kevin C    7 年前
     - set_fact:
         r: "{{ range(1000, 37272) | random(seed=item) }}"
       run_once: yes
       with_items:
         - string
    
     - debug:
         msg: "{{ r }}"