unarchive a file is failing

38 views
Skip to first unread message

anushake...@gmail.com

unread,
Nov 12, 2018, 12:39:08 PM11/12/18
to Ansible Project
Hi Team,

I am trying to unarchive a file is failing with below errors. Could someone please look into it.

Playbook:

---

- hosts: all

  vars_files:

      - /etc/ansible/xx/xyz/vars/main.yml

  tasks:

  - name: test

    unarchive:

      src: "{{ item.src_tar }}"

      dest: “{{ item.dest_tar }}”

    with_items: "{{ TAR }}"


vars_files:


TAR:

- { src_tar: '/home/virtual/xx/sample.tar.gz', dest_tar: '/tmp/sss/' }


Error:

"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'dest_tar'\n\nThe error appears to have been in '/etc/ansible/xx/xyz/tasks/main.yml': line 23, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    with_items: \"{{ TAR }}\"\n  - name: test\n    ^ here\n"}

Mohan L

unread,
Nov 13, 2018, 1:26:19 AM11/13/18
to Ansible Project


You have to quote the dictionary key as well. 

FROM

- { src_tar: '/home/virtual/xx/sample.tar.gz', dest_tar: '/tmp/sss/' } 

TO

- { 'src_tar': '/home/virtual/xx/sample.tar.gz', 'dest_tar': '/tmp/sss/' }


Here is the working example. If you write a role and call that role from playbook then you no need to use var_files section. vars/main.yml is one of the default ansible search path. 

See how my tasks/main.yml :

# cat roles/xyz/tasks/main.yml 

---

# tasks file for xyz

- name: Extract /tmp/xx/sample.tar.gz into /tmp/sss

  unarchive:

    src: "{{ item.src_path }}"

    dest: "{{ item.dest_path }}"

  with_items: "{{ TAR }}"



#See the vars/main.yml

# cat roles/xyz/vars/main.yml 

---

# vars file for xyz

TAR:

 - { "src_path": "/tmp/xx/sample.tar.gz", "dest_path": "/tmp/sss/" }



# See the Ansible playbook

# cat /etc/asnible/site.yml 

---

- hosts: all

  

  roles:

    - xyz 


# ansible-playbook site.yml


TASK [xyz : Extract /tmp/xx/sample.tar.gz into /tmp/sss] *******************************************

ok: [localhost] => (item={u'src_path': u'/tmp/xx/sample.tar.gz', u'dest_path': u'/tmp/sss/'})


PLAY RECAP *****************************************************************************************

localhost                  : ok=2    changed=0    unreachable=0    failed=0   


# ls /tmp/xx/sample.tar.gz 

/tmp/xx/sample.tar.gz


# ls /tmp/sss/

sample

Keshipeddy Anusha

unread,
Nov 13, 2018, 2:16:58 AM11/13/18
to ansible...@googlegroups.com
Thank you Mohan that worked for me, but may I know why we need to place them in double quotes??

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/95a45bc1-676f-496b-8e03-709c10a845eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

anushake...@gmail.com

unread,
Nov 13, 2018, 2:23:39 AM11/13/18
to Ansible Project
It is unpacking a tar file successfully but at the same time I am getting below error. Could you please help.


 {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'dest_tar'\n\nThe error appears to have been in '/etc/ansible/xxx/xy/tasks/main.yml': line 26, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: test\n    ^ here\n"}
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Mohan L

unread,
Nov 13, 2018, 3:34:44 AM11/13/18
to Ansible Project
Ansible think src_tar as variable If you don't place quotes so through error saying undefined variable.   


On Tuesday, November 13, 2018 at 12:46:58 PM UTC+5:30, Keshipeddy Anusha wrote:

Mohan L

unread,
Nov 13, 2018, 3:36:40 AM11/13/18
to Ansible Project

It looks like again you are not placing quotes around dictionary key. Can you please show your code which through this error?
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

anushake...@gmail.com

unread,
Nov 13, 2018, 3:44:25 AM11/13/18
to Ansible Project
Please find the files below.


vars/main.yml

XYZ:
- { src: '/tmp/abc/ab.warr', dest: '/tmp/abc/' }

TAR:
- { "src_tar": "/tmp/xx/sample.tar.gz", "dest_tar": "/tmp/sss/" }

tasks/main.yml

---
- hosts: all
  vars_files:
      - /etc/ansible/xxx/xyz/vars/main.yml
  tasks:
  - name: test
    unarchive:
      src: "{{ item.src_tar }}"
      dest: "{{ item.dest_tar }}"
    with_items: "{{ TAR }}"


Mohan L

unread,
Nov 13, 2018, 3:58:55 AM11/13/18
to Ansible Project

I am not clear about what you are trying to achieve here. are you using XYZ in with_items? Please note you have to quote src and dest in XYZ other Ansible will src/dest are variables.  

Mohan L

unread,
Nov 13, 2018, 4:00:39 AM11/13/18
to Ansible Project

I am not clear about what you are trying to achieve here. are you using XYZ in with_items? Please note you have to quote src and dest in XYZ otherwise Ansible will treat src/dest are variables.  
Reply all
Reply to author
Forward
0 new messages