如何调试失败的Ansible剧本?

时间:2019-07-28 13:04:05

标签: ansible syntax-error playbook

我不断在用户上出现错误:剧本中的(播放)。 Ansible似乎在抱怨结肠,但我没有尝试。


expression = "-.2"
expression = list(expression)

for key, char in enumerate(expression):
    # If first character in the string is a point, add "0" before it if there is a digit after the point
    if not key:
        if char == ".":
            try:
                if expression[key+1].isdigit():
                    expression.insert(key, "0")
            except: pass
        continue

    # If a point is not the first character in the string, add "0" before it if there are no digits before the point but one after the point
    if char == "." and not expression[key-1].isdigit():
        try:
            if expression[key+1].isdigit():
                expression.insert(key, "0")
        except: continue

expression = "".join(expression)
print(expression)   # Result is "-0.2"

预期结果是将在预期的主机上创建用户natasha和john。

1 个答案:

答案 0 :(得分:2)

您的YAML语法有错误,很容易修复。如Matthew所述,YAML对间距和分隔符非常严格。

这是您更正的语法:

---

- hosts: all
  become: root
  tasks:

  - name: add user Natasha
    user:
      name: natasha
      comment: "Natasha Ping"
      uid: 1027
      group: ping

  - name: add user John
    user:
      name: john
      comment: "John Pong"
      uid: 1028
      group: ping

我还建议您使用ansible-lint。它将帮助您发现错误并遵循最佳做法。