用于打印输出的Ansible Jinja2产生重复结果

时间:2017-01-22 11:08:20

标签: ansible jinja2 ansible-template

Ansible 2.2版,Mac OS X El Capitan

我正在尝试生成端口映射列表。

当我运行这样的代码时

---
- hosts: [localhost]
  sudo: True 
  pre_tasks:
    - debug: 
        var: "{% for id in range(100,102) %}  
                '{{ id }}'
            {% endfor %}"

我希望得到这样的结果:

  

"' 100' ' 101'"

但我得到了结果:

  

" ' 100' ' 101' ":" 100101"

为什么我得到这样的结果并不是很明显。看起来它从结果中创建了某种地图,但我不明白为什么。

1 个答案:

答案 0 :(得分:0)

您获得了预期的结果,因为您在msg模块中使用了debug参数而不是- hosts: localhost tasks: - debug: msg: "{% for id in range(100,102) %} '{{ id }}' {% endfor %}" ,所以您只看到输出不一致。

尝试:

- hosts: localhost
  tasks:
    - copy: 
        dest: ./result.txt
        content: "{% for id in range(100,102) %}  
                    '{{ id }}'
                  {% endfor %}"

在更复杂的情况下,您可以通过将内容保存到文件并检查内容来考虑更可靠的调试方式。这将消除显示Ansible日志的回调插件的影响。

cat ./result.txt

然后:

 Problem 1
    - Installation request for fgrosse/phpasn1 1.5.2 -> satisfiable by fgrosse/phpasn1[1.5.2].
    - fgrosse/phpasn1 1.5.2 requires ext-gmp * -> the requested PHP extension gmp is missing from your system.
  Problem 2
    - Installation request for mdanter/ecc v0.4.2 -> satisfiable by mdanter/ecc[v0.4.2].
    - mdanter/ecc v0.4.2 requires ext-gmp * -> the requested PHP extension gmp is missing from your system.
  Problem 3
    - Installation request for pusher/pusher-php-server 2.6.3 -> satisfiable by pusher/pusher-php-server[2.6.3].
    - pusher/pusher-php-server 2.6.3 requires ext-curl * -> the requested PHP extension curl is missing from your system.
  Problem 4
    - pusher/pusher-php-server 2.6.3 requires ext-curl * -> the requested PHP extension curl is missing from your system.
    - laravel-notification-channels/pusher-push-notifications 1.0.2 requires pusher/pusher-php-server 2.6.* -> satisfiable by pusher/pusher-php-server[2.6.3].
    - Installation request for laravel-notification-channels/pusher-push-notifications 1.0.2 -> satisfiable by laravel-notification-channels/pusher-push-notifications[1.0.2].

  To enable extensions, verify that they are enabled in your .ini files:
    - /etc/php/7.0/cli/php.ini
    - /etc/php/7.0/cli/conf.d/10-mysqlnd.ini
    - /etc/php/7.0/cli/conf.d/10-opcache.ini
    - /etc/php/7.0/cli/conf.d/10-pdo.ini
    - /etc/php/7.0/cli/conf.d/20-calendar.ini
    - /etc/php/7.0/cli/conf.d/20-ctype.ini
    - /etc/php/7.0/cli/conf.d/20-exif.ini
    - /etc/php/7.0/cli/conf.d/20-fileinfo.ini
    - /etc/php/7.0/cli/conf.d/20-ftp.ini
    - /etc/php/7.0/cli/conf.d/20-gettext.ini
    - /etc/php/7.0/cli/conf.d/20-iconv.ini
    - /etc/php/7.0/cli/conf.d/20-json.ini
    - /etc/php/7.0/cli/conf.d/20-mbstring.ini
    - /etc/php/7.0/cli/conf.d/20-mysqli.ini
    - /etc/php/7.0/cli/conf.d/20-pdo_mysql.ini
    - /etc/php/7.0/cli/conf.d/20-phar.ini
    - /etc/php/7.0/cli/conf.d/20-posix.ini
    - /etc/php/7.0/cli/conf.d/20-readline.ini
    - /etc/php/7.0/cli/conf.d/20-shmop.ini
    - /etc/php/7.0/cli/conf.d/20-sockets.ini
    - /etc/php/7.0/cli/conf.d/20-sysvmsg.ini
    - /etc/php/7.0/cli/conf.d/20-sysvsem.ini
    - /etc/php/7.0/cli/conf.d/20-sysvshm.ini
    - /etc/php/7.0/cli/conf.d/20-tokenizer.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.