How to concatenate string in YAML?

23,377

Solution 1

stdout_lines was created for convenience. Back in the day we only had stdout. Which is what you want, I think:

command:  php -f /path/to/php/test.php '{"stdout_lines": {{output.stdout}} }'

and if you want to really concat yourself, say because you have your own list of strings then you can use the Jinja2 built-in filter join:

- hosts: localhost
  gather_facts: False
  tags: so9
  vars:
    - str_list: ['Hello', 'world', '!', ]
  tasks:
    - debug: msg="orig list={{ str_list }}"
    - debug: msg="concated = {{ str_list | join(' ') }}"
    - set_fact: concated_str="{{ str_list | join(' ') }}"
    - debug: msg="assigned to a variable concated_str = {{ concated_str }}"

Solution 2

This will do

  - name: Generate JSON output based on template
    template: src=colors.json.j2 dest=/tmp/colors.json
    with_items: colors

It will generate a file like

{'colors':
    {
        'blue',
        'red',
        'green',
        }
}

Solution 3

Try like this.

  vars:
- img_path: "/var/lib/libvirt/images/{{ instance_name }}-disk2.img"

Where instance name is a another variable

Share:
23,377
Roopendra
Author by

Roopendra

This is Roopendra Vishwakarma, A DevOps Engineer and Blogger from India. Having experience in DevOps and Web-Development Technology. Tags Given By Me:- ansible-playbook and elasticsearch-jdbc-river Blog :- http://techieroop.com

Updated on July 29, 2020

Comments

  • Roopendra
    Roopendra almost 4 years

    I am writing a playbook in which I need to execute mysql query and pass output in json format. playbook code working fine just I want facing error in string concatenate part. If I am passing sample json string it is working fine.

    - name: Execute php script with json parameter
      command:  php -f /path/to/php/test.php "{'colors':{'red','blue','yellow'}}"
      register: php_output
    

    output.stdout_lines is variable already set in my playbook which contains output in {'red','blue','yellow'} format.

    - name: Execute php script with json parameter
      command:  php -f /path/to/php/test.php '{"stdout_lines": {{output.stdout_lines}} }'
      register: php_output
    

    So how can I concatenate output.stdout_lines variable in '{"stdout_lines": {{output.stdout_lines}} }' ? any suggestions