Debug for the winners !

存储架构 Arrfab's Blog (源链)

Recently I had to dive back into Ansible
playbooks I wrote (quite) some time ago. I had to add some logic to generate different application templates based on facts/packages being installed on the managed nodes. Long story short (I’ll not describe the use case here as it’s quite complex), I decided that injecting directly some kind of logic in the Jinja2 templates
was enough .. but not.

Let’s take a very simplified example here (don’t even look at the tasks but rather at the logic explained how to get there, once again this is a ‘stupid’ playbook) :

- hosts: localhost  
  connection: local  
  user: root  
    - myrole: httpserver

    - name: registering a variable only if myrole is httpserver  
      command: /bin/rpm -q --qf '%{version}' httpd  
      register: httpd_version  
      when: myrole == 'httpserver'  
    - name: pushing the generated template  
      template: src=../templates/logic.txt.j2 dest=/tmp/logic.txt


Now let’s have a look at the (very) simple logic.txt.j2 :

{% if httpd_version is defined -%}  
 You're using an Apache http server version : {{ httpd_version.stdout> }}  
{% else %}  
  You're not using an http server, or not defined in the ansible> machine role  
{% endif -%}

Easy, and it seems it was working when myrole was indeed httpserver :

cat /tmp/logic.txt  
  You're using an Apache http server version : 2.2.15

But things didn’t work as expected when myrole was something else, like for example dbserver

TASK: [registering a variable only if myrole is httpserver]
  skipping: [localhost]

 TASK: [pushing the generated template]
  fatal: [localhost] => {'msg': "One or more undefined variables:
 'dict' object has no attribute 'stdout'", 'failed': True}

hmm, as the register: task was skipped, I was wondering why it then complained about the httpd_version.stdout as I thought that httpd_version wasn’t defined .. but I was wrong : even if ‘skipped’ the variable exists for that host. I quickly discovered it when adding a debug
task in between the other tasks in my playbook :

– debug: msg=”this is http_version value {{ httpd_version }}”

Now let’s see what can be wrong :

 TASK: [debug msg="this is http_version value {{httpd_version}}"]
  ok: [localhost] => {"msg": "this is http_version value {u'skipped': True, u'changed': False}"}

Very interesting : so even when skipped, the variable httpd_version is still “registered” by the register: feature but marked as skipped.

Let’s so change our “logic” in the Jinja2 template then ! :

{% if httpd_version.skipped -%}  
  You're not using an http server, or not defined in the ansible machine role  
{% else %}  
  You're using an Apache http server version : {{ httpd_version.stdout }}  
{% endif -%}

And now it works in all cases ..

It’s a (very,very,very) simplified example, but you get the idea and using the debug module (don’t forget to call ansible-playbook with -vvv to see those messages too !) can quickly show you where your issue is when having to troubleshoot something. As Patrick Debois
was saying
: “you gotta love Ansible for its simplicity” :-)


诊断Web应用的网络连接问题 问题:Web App 连接不到DB,访问Web页面显示错误信息 1. 登录到Web Server,根据Web Service的配置文件找到DB服务器host。 2. MySQL和MariaDB默认端口号是3306。从Web Server上执行: telenet db.ex.c...
Why You Should Attend AnsibleFest It’s that time again! The time when automators from all over converge at the official event for all things Ansible — AnsibleFest San Francisco! F...
Creating Ansible Custom Module for AWR Report Gene... Creating AWR reports is a common task for many DBAs. At times, you may need to generate multiple AWR reports from multiple databases for performance a...
Full web server setup with Debian 9 (Stretch) Setup bash and update the system cp /etc/skel/.bashrc /root/.bashrc apt-get update apt-get dist-upgrade Configure hostname correctly Make sure ...
【免费直播】后端实战:搭建一个简易Flask Web服务器... 你们要的Python课程来啦,简单易上手,不容错过! 主讲老师:康安老师 曾任职于搜狐、完美世界从事游戏客户端开发;目前专注于Flask后端开发。 课程适合群体 根据提供的预习资料,提前了解python基础语法知识 Web前端基础知识,课程...
责编内容来自:Arrfab's Blog (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Debug for the winners !

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录