Skip to main content

Posts

Showing posts from October, 2025

Ansible role luôn chạy trước task

Ansible là công cụ tự động hóa "đơn giản", "dễ dùng". Trong một ansible play, có thể dùng tasks, hoặc roles, hoặc cả hai. Thứ tự chạy của chúng có chút bất ngờ . Thứ tự role và task khi trong cùng play Playbook order.yml - hosts: all tasks: - name: task1 debug: msg: "This is task1" roles: - role: pika Role pika: roles/pika/tasks/main.yml : - name: task in role debug: msg: this is task in a role Output: $ uvx --from 'ansible-core>2.19' ansible-playbook -K -i localhost, order.yml BECOME password: PLAY [all] ************************************************************************** ... TASK [pika : task in role] ********************************************************** ok: [localhost] => { "msg": "this is task in a role" } TASK [task1] ************************************************************************ ok: [localhost] => { "msg": "This is task1" ...

[Python] Tính toán IP trong mạng

Python từ 3.3 có sẵn thư viện ipaddress rất tiện lợi để tính toán IP trong mạng. Thư viện ipaddress $ podman run -it python:3.10-alpine ... Python 3.10.19 (main, Oct 9 2025, 22:43:20) [GCC 14.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ipaddress >>> print(ipaddress.__doc__) A fast, lightweight IPv4/IPv6 manipulation library in Python. This library is used to create/poke/manipulate IPv4 and IPv6 addresses and networks. >>> ipaddress. ipaddress.AddressValueError( ipaddress.IPv4Network( ipaddress.collapse_addresses( ipaddress.ip_network( ipaddress.IPV4LENGTH ipaddress.IPv6Address( ipaddress.functools ipaddress.summarize_address_range( ipaddress.IPV6LENGTH ipaddress.IPv6Interface( ipaddress.get_mixed_type_key( ipaddress.v4_int_to_packed( ipaddress.IPv4Address( ...

[TIL] requests đoán "có giáo dục" encoding và có thể sai

requests là thư viện HTTP client phổ biến nhất của Python, nó phổ biến vì dễ dùng, chỉ cần import requests; requests.get là xong. Trông dễ như vậy bởi requests đã cấu hình mặc định hết rất nhiều thứ như: connection, headers, adapter, session, encoding, ... mà mặc định thì nhiều khi đúng, đôi khi sai. Encoding Vì Python mặc định encoding trên Linux là utf-8 , người dùng dễ mặc định là requests cũng vậy, nhưng thực tế thì: Tài liệu viết: When you make a request, Requests makes educated guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by Requests is used when you access r.text. You can find out what encoding Requests is using, and change it, using the r.encoding property: r.encoding 'utf-8' r.encoding = 'ISO-8859-1' If you change the encoding, Requests will use the new value of r.encoding whenever you call r.text. You might want to do this in any situation where you can apply special logic to work out what the en...

Popular posts from this blog

Tài liệu và hướng dẫn học Python

Để tiết kiệm thời gian, tốt nhất là đi học PyMI Updated: 130617 Sau đây là các tài liệu khuyên dùng: Vì nhiều lý do, nên học python2.7 tại thời điểm hiện tại (giờ là tháng 6/2013 - muốn biết tại sao thì tự tìm hiểu) python 3.5+ (giờ là tháng 2/2017) Chuẩn bị: 1. biết bật tắt máy 2. biết cài python 3. tập gõ 10 ngón - gõ 2 ngón hay 1 ngón cũng không sao, nhưng 10 ngón là cách dễ nhất để gõ nhanh nhất. Tài liệu - Nên dùng tài liệu tại trang chủ của Python làm chính, tham khảo thêm các tài liệu khác tại http://www.familug.org/2016/12/free-ebook.html Căn bản, mới học 1.1 Python PyMI.vn https://pymi.vn/tutorial/ 1.2. Python offical tutorial kết hợp làm bài tập trên HackerRank  (đề bài bằng tiếng Anh, nhưng Google translate 1 lúc cũng ra vì có nhiều ví dụ mẫu đi kèm). Học viên của Pymi.vn có rất nhiều học viên đã tự học với Learn Python the hard way nhưng chưa thấy ai thành công cả. Hai link dưới nên đọc sau khi đã nắm được những phần cơ bản của ngôn ngữ pytho...

The PyMiers

New FAMILUG