Ansible-variable containing double quotes changed

1,394 views
Skip to first unread message

Barun kumar

unread,
Feb 17, 2020, 6:19:59 AM2/17/20
to ansible...@googlegroups.com

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.

 
Regards,
Barun Kumar
Mobile: +91 8297380006, India
Coming Soon....... 

Dick Visser

unread,
Feb 17, 2020, 8:37:55 AM2/17/20
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CADG%3DkotAgP8RdwBmyHjG9nv8GO-0Kqr5Zt%3DH_FTfmfk09Aomwg%40mail.gmail.com.


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Barun kumar

unread,
Feb 17, 2020, 7:19:14 PM2/17/20
to ansible...@googlegroups.com, dick....@geant.org

 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

 

#

=====================================Debug Value======================================================================================

#ansible-playbook –I inventory disk.yml –vvv

 

TASK [asm_pdb_disk_mapping : debug] ************************************************************************************************************************************

task path: /infra/infracode/automation_deployment/ansible/dynamic_tags/asm_pdb_disk_mapping/tasks/asm_pdb_disk_mapping.yml:219

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.089253",

        "end": "2020-02-18 10:28:59.483863",

        "failed": false,

        "rc": 0,

        "start": "2020-02-18 10:28:59.394610",

        "stderr": "",

        "stderr_lines": [],

        "stdout": "[\"dev/xvdj\",\"dev/xvdk\"]",      #This output we want to capture in subsequent tasks

        "stdout_lines": [

            "[\"dev/xvdj\",\"dev/xvdk\"]"

        ]

    }

}

 

 

TASK [asm_pdb_disk_mapping : debug] ************************************************************************************************************************************

task path: /infra/infracode/automation_deployment/ansible/dynamic_tags/asm_pdb_disk_mapping/tasks/asm_pdb_disk_mapping.yml:230

ok: [10.0.0.10] => {

    "PLAY1VAR_NEW01": {

        "add_host": {

            "groups": [],

            "host_name": "10.0.0.20",

            "host_vars": {

                "PLAY1VAR_NEW01": [

                    "dev/xvdj",

                    "dev/xvdk"

                ]

            }

        },

        "changed": true,

        "failed": false

    }

}

 

 

TASK [asm_pdb_disk_mapping : debug] ************************************************************************************************************************************

task path: /infra/infracode/automation_deployment/ansible/dynamic_tags/asm_pdb_disk_mapping/tasks/asm_pdb_disk_mapping.yml:243

ok: [10.0.0.10] => {

    "terraform_disk": {

        "changed": false,

        "skip_reason": "Conditional result was False",

        "skipped": true

    }

}

ok: [10.0.0.20] => {

    "terraform_disk": {

        "changed": true,

        "cmd": "echo [u'dev/xvdj', u'dev/xvdk'] > /tmp/device_name\n",     #Expected Value  >> "[\"dev/xvdj\",\"dev/xvdk\"]",     

        "delta": "0:00:00.004322",

        "end": "2020-02-17 23:29:15.619772",

        "failed": false,

        "rc": 0,

        "start": "2020-02-17 23:29:15.615450",

        "stderr": "",

        "stderr_lines": [],

        "stdout": "",

        "stdout_lines": []

    }

}

Regards,
Barun Kumar
Mobile: +91 8297380006, India
Will get you.


Angel Rengifo Cancino

unread,
Feb 17, 2020, 9:13:13 PM2/17/20
to ansible...@googlegroups.com, dick....@geant.org
Hello:

On Mon, Feb 17, 2020 at 7:19 PM Barun kumar <barun....@gmail.com> wrote:

 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.


Reply all
Reply to author
Forward
0 new messages