Ansible-variable containing double quotes changed

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.

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

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")

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

      do

      a2+=("$i")

      done

      a3=()

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

      skip=

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

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

      done

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

      done

      joined=$(printf ","\""dev/"%s"\"" "${a3[@]:0:{{disk_count.stdout}}}")

      echo "["${joined:1}"]"

    register: device_name

    when: inventory_hostname in groups['dbserver']

  - name: Register linux jump host with variable |device_name

    add_host:

      name: "10.0.0.20"

      PLAY1VAR_NEW01: "{{ device_name.stdout }}"

    register: PLAY1VAR_NEW01

    when: inventory_hostname in groups['dbserver']

  - debug: var=PLAY1VAR_NEW01

    when: inventory_hostname in groups['dbserver']

  - name: terraform variable store tasks |device_name

    shell: |

      echo {{ hostvars["10.0.0.20"]["PLAY1VAR_NEW01"] }} > /tmp/device_name

    when: inventory_hostname in groups['controller_servers']

    register: terraform_disk

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.