N
N
Nikolay Baranenko2020-02-11 16:45:19
bash
Nikolay Baranenko, 2020-02-11 16:45:19

How to pass a context that has double quotes to the remote host?

I would like to make a reservation right away that yes in ansible it is possible to save the context in a file on the remote host

- hosts: "{{ TARGET_HOSTS }}"
  tasks:
    - name: create target file on target host
      copy:
        content: "{{ TARGET_FILE_CONTEXT }}"
        dest: "{{ TARGET_PATH_FILE }}"
        force: yes
        owner: "{{ TARGET_USERNAME }}"
        group: "{{ TARGET_USERGROUP }}"
        mode: "u=rwx,g=rx,o=rx"


but I wanted to use only

- hosts: "{{ TARGET_HOSTS }}"
  tasks:
    - name: execute shell script
      shell: "{{ SHELL_SCRIPT }}"


from jenkinsfile broadcast type context

def CONSUL_HCL_FILE_CONTEXT(name,port,target_host){
def CONTEXT="""
service {
name = "${name}"
port = ${port}
enable_tag_override = false
tags = ["prometheus"]
check {
name = "${name}"
tcp = "${target_host}:${port}"
interval = "60s"
}
}
"""
return CONTEXT;
}


in which there are double quotes in the file on the remote host
so that the result is

service {
name = "metrics_from_hive"
port = 8006
enable_tag_override = false
tags = ["prometheus"]
check {
name = "metrics_from_hive"
tcp = "remote-001:8006"
interval = "60s"
}
}


Here is an attempt

deploy = sh(script: "ansible -m shell -a 'echo \"${CONSUL_HCL_FILE_CONTEXT("${project}","${port}","${target_host}")}\" | tee /etc/consul.d/${project}.hcl' ${prometheus_host}", returnStdout: true).trim() as String


leads to the fact that

service {
name = metrics_from_hive
port = 8006
enable_tag_override = false
tags = [prometheus]
check {
name = metrics_from_hive
tcp = remote-001:8006
interval = 60s
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2020-02-20
@saboteur_kiev

A difficult example, too lazy to read so much text.
But at a minimum, you don't escape quotes at all. As I understand it, you send ${CONSUL_HCL_FILE_CONTEXT... as is, without changes, and inside there are ordinary quotes. Naturally, echo will not make friends with them in this form.
Understand escaping your templates.
At the extreme, you can generally encode the entire file in base64, transfer it as a string, and then decode it into a file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question