The Ansible Debug Module

29/12/2020
Chưa phân loại
When you are working with Ansible playbooks, it’s great to have some debug options. Ansible provides a debug module that makes this task easier. It’s a handy tool to figure out any problem areas.

Getting Started

Ansible debug module is easy to use. For our first example, let’s try a simple hello world playbook. You can set up the 1_debug_example.yml playbook file:


– name: Debug Example – Hello World
hosts: localhost
tasks:
– name: Print debug message
debug:

The msg argument prints the information. Now if you run the playbook, you see the debug message:

Running Loops

Let’s try a more complicated example. In this example, the playbook 2_debug_example.yml has the following code:


– name: Debug Example with Loop
hosts: localhost
connection: local
gather_facts: no

tasks:
– name: Find Server
debug:
msg: "{{ item.name }} is {{ item.cpu }}"
with_items:
– name: machine1
cpu: 2.4GHz
memory: 2MB
disk: 100GB
network: 10Mbps
– name: machine2
cpu: 3GHz
memory: 1MB
disk: 500GB
network: 40Mbps
– name: machine3
cpu: 1.7GHz
memory: 4MB
disk: 200GB
network: 100Mbps
loop_control:
label: "{{ item.name }}"

It’s going to print out the server names and CPU speeds.

Debug Module Verbosity Control

You can set up your debug module to control the verbosity levels. Let’s create the playbook 3_debug_example.yml:


– name: Debug Example Uptime
hosts: localhost
connection: local
 
tasks:
– name: Find Uptime
shell: /usr/bin/uptime
register: result
 
– name: Print debug message
debug:
var: result
verbosity: 2

Here we are registering a variable called result to store debug information. When you run the playbook without any flags:

You don’t see any debug information.

Now let’s try running the playbook command with “-v” flag:

You have a lot more information than before. It’s printing out the uptime information that you ran on the shell.

If you try the “-vv” flag, you get more verbose information:

Conclusion

You can set up the Ansible debug module to make it easier to find problems. If you are working with others, you can set up the debug module in such a way that you can get verbose output only when you want.

Further Study:
ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Wine 2.0.2 Stable Release on Ubuntu 17.04 and Below

Wine 2.0.2 is the latest stable release to Wine 2.0 series. This release comes as a maintainance release to address various...
28/12/2020

Installing Komodo IDE and Komodo Edit on Ubuntu

Komodo IDE is from ActiveState, the company behind ActiveState Perl, Python, Ruby distribution. Komodo IDE supports many...
29/12/2020

[Graylog] [LAB] [Phần 11] Cấu hình Graylog server tích hợp cảnh báo qua Slack

Chắc là mọi người đã biết đến Slack nơi tạo ra một room chat để mọi người có thể chia sẻ...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024