# Ansible-variable containing double quotes changed

**URL:** <https://forum.ansible.com/t/ansible-variable-containing-double-quotes-changed/32249>\
**Category:** Ansible Project\
**Created:** [February 17, 2020, 11:19am UTC](https://forum.ansible.com/t/ansible-variable-containing-double-quotes-changed/32249 "2020-02-17T11:19:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barun\_kumar](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@Barun\_kumar](https://forum.ansible.com/u/Barun_kumar)\
**Post date:** [February 17, 2020, 11:19am UTC](https://forum.ansible.com/t/ansible-variable-containing-double-quotes-changed/32249/1 "2020-02-17T11:19:28Z")

</div>

When I assign a variable containing double quotes to another variable the double quotes (") sometimes get converted to single quotes ('). .can you please suggest how to solve this.

TASK [asm\_pdb\_disk\_mapping : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [10.0.0.10] =\> {

“device\_name”: {

“changed”: true,

“cmd”: “a1=( "xvda" "xvdb" "xvdc" "xvdd" "xvde" "xvdf" "xvdg" "xvdh" "xvdi" "xvdj" "xvdk" "xvdl" "xvdm" "xvdn" "xvdo" "xvdp" "xvdq" "xvdr" "xvds" "xvdt" "xvdu" "xvdv" "xvdw" "xvdx" "xvdy" "xvdz")\nfor i in `cat /tmp/volume.txt | sed -e 's/\\[//' | sed -e 's/\\]//' | sed -e 's/\\\"//g' | sed -e 's/,/ /g'`\ndo\na2+=("$i")\ndone\na3=()\nfor i in "${a1[@]}"; do\nskip=\nfor j in "${a2[@]}"; do\n [[$i == $j]] && { skip=1; break; }\ndone\n [[-n $skip]] || a3+=("$i")\ndone\njoined=$(printf ","\""dev/"%s"\"" "${a3[@]:0:2}") \necho "["${joined:1}"]"\n”,

“delta”: “0:00:00.088902”,

“end”: “2020-02-17 21:50:56.868731”,

“failed”: false,

“rc”: 0,

“start”: “2020-02-17 21:50:56.779829”,

“stderr”: “”,

“stderr\_lines”: ,

“stdout”: “["dev/xvdj","dev/xvdk"]”,

“stdout\_lines”: [

“["dev/xvdj","dev/xvdk"]”

]

}

}

Expected value to store in variable : ["dev/xvdj","dev/xvdk"]

But it is storing this variable value: [u’dev/xvdj’, u’dev/xvdk’]

Tel me know how to solve this issue.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 17, 2020, 1:37pm UTC](https://forum.ansible.com/t/ansible-variable-containing-double-quotes-changed/32249/2 "2020-02-17T13:37:21Z")

</div>

Hi Barun

You say “When I assign a variable containing double quotes to another variable” - what variable are you assigning to what other variable?  
What you post appears to be the output of a cmd task (I’m guessing, since you didn’t post the original playbook, nor the task).  
That is JSON formatted output and looks fine to me.

But then you say:

Expected value to store in variable : ["dev/xvdj","dev/xvdk"]  
But it is storing this variable value: [u’dev/xvdj’, u’dev/xvdk’]

What variables are you referring to, and what values do you think are assigned to them?

Maybe first things first: what are you actually trying to achieve? What do you need?

Dick

---

<div class="post-metadata">

**Author:** ![Barun\_kumar](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@Barun\_kumar](https://forum.ansible.com/u/Barun_kumar)\
**Post date:** [February 18, 2020, 12:18am UTC](https://forum.ansible.com/t/ansible-variable-containing-double-quotes-changed/32249/3 "2020-02-18T00:18:41Z")

</div>

Hi Disk,

More explanation about playbook, hope you did not chance to see that,  
please check once.

cat /tmp/volume.txt #Has list of disk attached to server, which needs to  
excluded

["xvda", "xvdb", "xvdc", "xvdd", "xvde", "xvdf", "xvdg", "xvdh", "xvdi"]

# disk.yml

disk\_count.stdout: 2 #number of disk need to add to server.

- name: Call devicecheck script

&nbsp;&nbsp;&nbsp;&nbsp;shell: |

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a1=( "xvda" "xvdb" "xvdc" "xvdd" "xvde" "xvdf" "xvdg" "xvdh" "xvdi"  
"xvdj" "xvdk" "xvdl" "xvdm" "xvdn" "xvdo" "xvdp" "xvdq" "xvdr" "xvds"  
"xvdt" "xvdu" "xvdv" "xvdw" "xvdx" "xvdy" "xvdz")

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in `cat /tmp/volume.txt | sed -e 's/\[//' | sed -e 's/\]//' |  
sed -e 's/\"//g' | sed -e 's/,/ /g'`

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a2+=("$i")

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;done

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a3=()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in "${a1[@]}"; do

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;skip=

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for j in "${a2[@]}"; do

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[$i == $j]] && { skip=1; break; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;done

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[-n $skip]] || a3+=("$i")

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;done

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;joined=$(printf ","\""dev/"%s"\"" "${a3[@]:0:{{disk\_count.stdout}}}")

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo "["${joined:1}"]"

&nbsp;&nbsp;&nbsp;&nbsp;register: device\_name

&nbsp;&nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups['dbserver']

&nbsp;&nbsp;- name: Register linux jump host with variable |device\_name

&nbsp;&nbsp;&nbsp;&nbsp;add\_host:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "10.0.0.20"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PLAY1VAR\_NEW01: "{{ device\_name.stdout }}"

&nbsp;&nbsp;&nbsp;&nbsp;register: PLAY1VAR\_NEW01

&nbsp;&nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups['dbserver']

&nbsp;&nbsp;- debug: var=PLAY1VAR\_NEW01

&nbsp;&nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups['dbserver']

&nbsp;&nbsp;- name: terraform variable store tasks |device\_name

&nbsp;&nbsp;&nbsp;&nbsp;shell: |

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo {{ hostvars["10.0.0.20"]["PLAY1VAR\_NEW01"] }} \> /tmp/device\_name

&nbsp;&nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups['controller\_servers']

&nbsp;&nbsp;&nbsp;&nbsp;register: terraform\_disk

---

<div class="post-metadata">

**Author:** ![Angel\_Rengifo\_Cancin](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Angel\_Rengifo\_Cancin](https://forum.ansible.com/u/Angel_Rengifo_Cancin)\
**Post date:** [February 18, 2020, 2:12am UTC](https://forum.ansible.com/t/ansible-variable-containing-double-quotes-changed/32249/4 "2020-02-18T02:12:43Z")

</div>

Hello:

> Hi Disk,
> 
> More explanation about playbook, hope you did not chance to see that, please check once.
> 
> cat /tmp/volume.txt #Has list of disk attached to server, which needs to excluded
> 
> [“xvda”, “xvdb”, “xvdc”, “xvdd”, “xvde”, “xvdf”, “xvdg”, “xvdh”, “xvdi”]
> 
> # disk.yml
> 
> disk\_count.stdout: 2 #number of disk need to add to server.
> 
> - name: Call devicecheck script
> 
> shell: |
> 
> a1=( “xvda” “xvdb” “xvdc” “xvdd” “xvde” “xvdf” “xvdg” “xvdh” “xvdi” “xvdj” “xvdk” “xvdl” “xvdm” “xvdn” “xvdo” “xvdp” “xvdq” “xvdr” “xvds” “xvdt” “xvdu” “xvdv” “xvdw” “xvdx” “xvdy” “xvdz”)

Do you need to do this by using a shell task? I’m not sure how you get the list of disks which you pretend to filter, but let me know if this approach works for you:

- name: Set the list of all disks  
set\_fact:  
a1: [“xvda”, “xvdb”, “xvdc”, “xvdd”, “xvde”, “xvdf”, “xvdg”, “xvdh”, “xvdi”, “xvdj”, “xvdk”, “xvdl”] #complete the list of all disks here

- name: Import the contents from file as a list  
set\_fact:  
excluded\_disks: “{{ lookup(‘file’, ‘volume.txt’) }}”

- name: Set the list of device names without considering excluded disks  
set\_fact:  
device\_name: “{{ device\_name | default() + [item] }}”  
when: item not in excluded\_disks  
with\_items: “{{ a1 }}”

- name: Show the final list of device names  
debug:  
var: device\_name

I assumed you disks to exclude are defined in a volume.txt file. As I don’t know how you get the list of all original disks (which you set in a1 variable within your shell script), I’ve just defined in an ansible variable.  
This way, there’s no need to deal with quoting, because that might be a little hard to solve, read and troubleshoot.

If you can share more lines from your code or what you pretend to achieve, maybe we can suggest some better ideas.
