asking
asking

Reputation: 305

How to use variable value for a spesific variable file content in Ansible Playbook?

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?

Upvotes: 0

Views: 302

Answers (2)

U880D
U880D

Reputation: 12009

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

Upvotes: 1

Kevin C
Kevin C

Reputation: 5720

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

Upvotes: 0

Related Questions