Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file to ansible host with custom variables substituted

I'm working on an ansible-playbook which should help to generate build agents for a continuous delivery pipeline. Among other issues, I'll need to install an oracle client on such an agent. I want to do something like

- name: "Provide response file"
  copy: src=/custom.rsp dest=/opt/oracle 

Within the custom.rsp file I've got some variables to be substituted. Normally, one could do it with a separate shell command like this:

- name: "Substitute Vars"
  shell: "sed 's|<PARAMETER>|<VALUE>|g' -i /opt/oracle/custom.rsp"

I don't like it, though. There should be a more convinient way to do this. Anybody giving me a hint?

like image 586
ferdy Avatar asked Nov 04 '15 17:11

ferdy


2 Answers

You want to be using a template rather than copying a static file.

Also, when using the copy or template modules, the dest parameter is a full path AND filename, not just a path. So if you want to end up with a copy of custom.rsp in the directory /opt/oracle then you need to do this:

- name: "Provide response file"
  template: src=/custom.rsp dest=/opt/oracle/custom.rsp
like image 190
Bruce P Avatar answered Sep 20 '22 01:09

Bruce P


I'm going to extend Bruce's answer with an example: This is part of my inventory.yaml:

kafka_stage:
  children:
    kafka_with_zookeeper_stage:
    kafka_only_stage:
  vars:
    zookeeper_hosts: "kafka-stage01:2181,kafka-stage02:2181,kafka-stage03:2181"
kafka_with_zookeeper_stage:
  hosts:
    kafka-stage01:
      broker_id: 0
    kafka-stage02:
      broker_id: 1
  vars:
    services:
      kafka:
      zookeeper:

This is part of a configuration file:

# The id of the broker. This must be set to a unique integer for each broker.
broker.id={{ broker_id }}
# {{ zookeeper_hosts }}
advertised.listeners=PLAINTEXT://{{ ansible_host }}:9092
# {{ services }}

This command in a playbook:

- name: Copy to Host
  ansible.builtin.template:
    src: my_configfile.properties
    dest: /tmp/hejsan.properties

Gave me this on the remote host kafka-stage02:

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=1
# kafka-stage01:2181,kafka-stage02:2181,kafka-stage03:2181
advertised.listeners=PLAINTEXT://kafka-stage02:9092
# {'kafka': None, 'zookeeper': None}
like image 45
Samuel Åslund Avatar answered Sep 19 '22 01:09

Samuel Åslund