Modulo copy argumento dest ( incongruencia al usar variables )

16 views
Skip to first unread message

jrv...@gmail.com

unread,
Oct 20, 2017, 8:57:12 AM10/20/17
to Ansible Project
Hola buenas, 

¿por que se dan estas incongruencias al usar variables en el modulo copy en su argumento dest.?


---
- hosts: localhost
  vars:
    PathLs: /etc/ansible/
    Folder: /ansible/

  tasks:
    - name: Ejecuta script
      script: "{{ PathLs }}ls.sh -al"
      register: Log_Step1

    - name: volcamos la salida a consola
      debug: var=Log_Step1.stdout_lines

    - name: volcamos la salida a fichero
      copy:
         content: |
             {% for lines in  Log_Step1.stdout_lines %}
             {{ lines }}
             {% endfor %}
#         dest: /etc/ansible/logls.txt          # usando esta linea ok
#         dest: /etc/{{ Folder }}/logls.txt    # usando esta linea ok.
#         dest: {{ PathLs }}logls.txt           # usando esto falla.
         dest: "{{ PathLs }}logls.txt"          # usando esta linea ok.


¿por que en el caso de que comience por variable, tiene que usarse comillas, y en los otros casos no?

Un saludo.

Mauricio Tavares

unread,
Oct 20, 2017, 9:28:06 AM10/20/17
to ansible...@googlegroups.com
AFAIK (http://docs.ansible.com/ansible/latest/playbooks_variables.html#hey-wait-a-yaml-gotcha),
you should use quotes around paths and other variables to guarantee
they are properly expanded. Out of curiosity, you could print {{
PathLs }}logls.txt with and without quotes (say, using a debug
statement) to see how different they are.

> Un saludo.
>
> --
> 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/5f4b362a-f71d-4f01-b6e1-5e329ce1a1ab%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

jrv...@gmail.com

unread,
Oct 20, 2017, 9:59:57 AM10/20/17
to Ansible Project
check 1: with and without quotes are the same

---
- hosts: localhost
 vars:
    PathLs: /etc/ansible/
    Folder: /ansible/
  tasks:
   - name: curiosidad con comillas
     debug:
       msg:  "{{ PathLs }}logls.txt"

    - name: curiosidad igual
     debug:
       msg:  esto es sin comillas {{ PathLs }}logls.txt


result check1:


*
ok: [172.18.77.66] => {
   "msg": "/etc/ansible/logls.txt"
}

TASK [curiosidad igual] ***************************************************************************************
ok: [172.18.77.66] => {
   "msg": "esto es sin comillas /etc/ansible/logls.txt"
}


check 2: with quotation marks at the beginning of the sentence return error:
  tasks:
  - name: curiosidad con comillas
    debug:
      msg:  {{ PathLs }}logls.txt


result check2:
ERROR! Syntax Error while loading YAML.


The error appears to have been in '/etc/ansible/playbooks/*******.yml': line 23, column 27, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      debug:
        msg:  {{ PathLs }}logls.txt
                          ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

Brian Coca

unread,
Oct 20, 2017, 10:59:06 AM10/20/17
to Ansible Project
Si usas {{ inmediatamente después de : , necesitas comillas o YAML
asume que es un diccionario.

valido:
msg: "{{variable}}"
invalido:
msg: {{variable}}

No requiere comillas si { no esta tras :

valido:
msg: Hola {{persona}}
invalido:
msg: {{saludo}} Jose


Estos son requerimientos de YAML, que Ansible usa como formato.


--
----------
Brian Coca

Mauricio Tavares

unread,
Oct 20, 2017, 11:55:21 AM10/20/17
to ansible...@googlegroups.com

jrv...@gmail.com

unread,
Oct 23, 2017, 1:06:32 AM10/23/17
to Ansible Project
Mauricio: Sorry, I did not check your link on Friday. Yes now.
Both: thanks for explaining this to me.
Is there any place to see these rare cases of yaml?
Reply all
Reply to author
Forward
0 new messages