Answer the question
In order to leave comments, you need to log in
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"
- hosts: "{{ TARGET_HOSTS }}"
tasks:
- name: execute shell script
shell: "{{ SHELL_SCRIPT }}"
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;
}
service {
name = "metrics_from_hive"
port = 8006
enable_tag_override = false
tags = ["prometheus"]
check {
name = "metrics_from_hive"
tcp = "remote-001:8006"
interval = "60s"
}
}
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
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
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 questionAsk a Question
731 491 924 answers to any question