Score:1

Ansible: How to joint string and integer to become new string

lr flag

I need assistance for ansible playbok on how to joint/combine string (linux) and number (0002) to be linux0002. Also how I can use %04d to format the integer 2 after arithmetic operation.

br flag
If '0002' is a number it's the number 2. Right? I read the question: " Combine string 'linux' and number 2 to be string 'linux0002'.
debug avatar
lr flag
Yes, you are right.
Score:0
br flag

Q: "How to joint/combine string (linux) and number (0002) to be linux0002?"

A: Use format, e.g.

    - debug:
        msg: "{{ _string ~ '%04d'|format(_number) }}"
      vars:
        _string: linux
        _number: 2

gives

  msg: linux0002

Q: "How I can use %04d to format the integer 2 after an arithmetic operation?|

A: The same code works after an arithmetic operation, e.g.

    - debug:
        msg: "{{ _string ~ '%04d'|format(_number + 1) }}"
      vars:
        _string: linux
        _number: 2

gives

  msg: linux0003

Notes

  • The variable '_number: 0002' is equivalent to '_number: 2'
debug avatar
lr flag
It is working. Thanks Vladimir Botka
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.