Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate string in YAML?

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

like image 830
Roopendra Avatar asked Mar 12 '14 11:03

Roopendra


Video Answer


2 Answers

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 }}"
like image 195
Kashyap Avatar answered Oct 13 '22 08:10

Kashyap


Try like this.

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

Where instance name is a another variable

like image 32
Vijay S B Avatar answered Oct 13 '22 09:10

Vijay S B