0

For example i have variable A and I want to write a file B with these value from variable A. The value from var A will be vary from the survey input.

- hosts: myhost
      vars:
        A: "{{ input }}"
    

The content of the file B that I needed: A="{{ input }}"

I need to replace the input for the file from the input value in the variable. The input can be anything inserted in the ansible survey.

Is there a way to write the input values from var A to the file B?

asking
  • 175
  • 2
  • 10

2 Answers2

1

Since your question and description are very vague

Is there a way to write the input values from var A to the file B?

the answer might not fit fully to your use case.

You may use the template_module.

First create a file

variable.file.j2

A={{ A }}

and than write it to the destination.

- name: Template a file to /tmp/B
  ansible.builtin.template:
    src: variable.file.j2
    dest: /tmp/B

Thanks to

U880D
  • 8,601
  • 6
  • 24
  • 40
  • actually, the input will be vary to the input in the survey, so input may be different each time.. i tried using the solution but it only echo the exact A="{{ input }}" and not the input A,B,C that i need – asking Nov 30 '21 at 02:09
  • Regarding your objection "_...the input will be vary to the input in the survey, so input may be different each time..._", yes, a template will handle that too. Regarding "_...I tried using..._", can you show by editing in your initial question what you have tried and what was the result? – U880D Nov 30 '21 at 07:29
  • i used it wrong thats why it didnt work, i tried this again and it works fine thankyou – asking Nov 30 '21 at 08:13
0

Templating would indeed be better. The quick 'n dirty method would be:

---
- hosts: my_hosts
  vars:
    A: my_value
  tasks:
    - shell: "echo {{ A }} > /tmp/B"
      register: the_echo

This way you'll see what's happening

Kevin C
  • 4,851
  • 8
  • 30
  • 64