Ansible 简明教程

Ansible - Troubleshooting

用于调试 Ansible 剧本的最常用策略是使用下面提供的模块 −

The most common strategies for debugging Ansible playbooks are using the modules given below −

Debug and Register

这两个是 Ansible 中可用的模块。为了调试目的,我们需要谨慎地使用这两个模块。示例演示如下。

These two are the modules available in Ansible. For debugging purpose, we need to use the two modules judiciously. Examples are demonstrated below.

Use Verbosity

使用 Ansible 命令,可以提供详细级别。你可以使用详细级别一 (-v) 或二 (-vv) 运行命令。

With the Ansible command, one can provide the verbosity level. You can run the commands with verbosity level one (-v) or two (-vv).

Important Points

在本节中,我们将介绍几个示例来理解几个概念。

In this section, we will go through a few examples to understand a few concepts.

如果你没有引用以变量开头的参数。例如,

If you are not quoting an argument that starts with a variable. For example,

vars:
   age_path: {{vivek.name}}/demo/

{{vivek.name}}

这将引发错误。

This will throw an error.

Solution

vars:
   age_path: "{{vivek.name}}/demo/" – marked in yellow is the fix.

How to use register -> Copy this code into a yml file say test.yml and run it
---
#Tsting
- hosts: tomcat-node
   tasks:

   - shell: /usr/bin/uptime
      register: myvar
      - name: Just debugging usage
         debug: var = myvar

当我通过命令 Ansible-playbook -i hosts test.yml 运行此代码时,我将得到如下所示的输出。

When I run this code via the command Ansible-playbook -i hosts test.yml, I get the output as shown below.

如果你看到 yaml,我们会将命令的输出注册到变量中 – myvar ,然后只打印输出。

If you see the yaml , we have registered the output of a command into a variable – myvar and just printed the output.

标记为黄色的文本告诉我们变量 – myvar 的属性,可用于进一步的流程控制。通过这种方式,我们可以找出特定变量公开的属性。下面的调试命令对此有所帮助。

The text marked yellow, tells us about property of the variable –myvar that can be used for further flow control. This way we can find out about the properties that are exposed of a particular variable. The following debug command helps in this.

$ ansible-playbook -i hosts test.yml

PLAY [tomcat-node] ***************************************************************
**************** ****************************************************************
*************** ******************************

TASK [Gathering Facts] *****************************************************************
************** *****************************************************************
************** **************************
Monday 05 February 2018  17:33:14 +0530 (0:00:00.051) 0:00:00.051 *******
ok: [server1]

TASK [command] ******************************************************************
************* ******************************************************************
************* **********************************
Monday 05 February 2018  17:33:16 +0530 (0:00:01.697) 0:00:01.748 *******
changed: [server1]

TASK [Just debugging usage] ******************************************************************
************* ******************************************************************
************* *********************
Monday 05 February 2018  17:33:16 +0530 (0:00:00.226) 0:00:01.974 *******
ok: [server1] => {
   "myvar": {
      "changed": true,
      "cmd": "/usr/bin/uptime",
      "delta": "0:00:00.011306",
      "end": "2018-02-05 17:33:16.424647",
      "rc": 0,
      "start": "2018-02-05 17:33:16.413341",
      "stderr": "",
      "stderr_lines": [],
      "stdout": " 17:33:16 up 7 days, 35 min,  1 user,  load average: 0.18, 0.15, 0.14",
      "stdout_lines": [
         " 17:33:16 up 7 days, 35 min,  1 user,  load average: 0.18, 0.15, 0.14"
      ]
   }
}

PLAY RECAP ****************************************************************************
**********************************************************************************
 **************************************
server1 : ok = 3    changed = 1    unreachable = 0    failed = 0

Common Playbook Issues

在本节中,我们将了解几个常见的剧本问题。问题为 −

In this section, we will learn about the a few common playbook issues. The issues are −

  1. Quoting

  2. Indentation

剧本以 yaml 格式编写,上面两个是最常见的 yaml/剧本问题。

Playbook is written in yaml format and the above two are the most common issues in yaml/playbook.

Yaml 不支持基于制表符的缩进,而是支持基于空格的缩进,因此需要小心处理。

Yaml does not support tab based indentation and supports space based indentation, so one needs to be careful about the same.

Note −完成 yaml 编写后,打开此网站 ( https://editor.swagger.io/ ),并将你的 yaml 复制粘贴到左侧,以确保 yaml 正确编译。这只是一个提示。

Note − once you are done with writing the yaml , open this site(https://editor.swagger.io/) and copy paste your yaml on the left hand side to ensure that the yaml compiles properly. This is just a tip.

Swagger 将错误归类为警告和错误。

Swagger qualifies errors in warning as well as error.