Skip to main content

Posts

Đọc code Python xem Ansible group priority thay đổi thứ tự gộp group vars

Ansible group dùng để nhóm các máy lại thành từng nhóm trong kho inventory. Khi một máy nằm trong nhiều group, các group vars sẽ được gộp lại, theo thứ tự children sẽ ghi đè parent, group sau sẽ ghi đè group trước . Sau và trước mặc định dựa theo tên group theo thứ tự bảng chữ cái. Trong Ansible inventory có thể cấu hình group priority cho group, khi không set có giá trị mặc định là 1 , giá trị priority có kiểu int, có thể là số âm. # lib/ansible/inventory/group.py class Group: """A group of ansible hosts.""" def __init__(self, name: str) -> None: name = helpers.remove_trust(name) self.depth: int = 0 self.name: str = to_safe_group_name(name) self.hosts: list[Host] = [] self._hosts: set[str] | None = None self.vars: dict[str, t.Any] = {} self.child_groups: list[Group] = [] self.parent_groups: list[Group] = [] self._hosts_cache: list[Host] | None = None self.pri...

Thẻ HTML link rel=alternate

Trong HTML, thẻ <link> thường dùng để liên kết các file file CSS hay favicon: <link rel="stylesheet" type="text/css" href="./theme/css/custom.css" media="screen"> <link rel="icon" href="favicon.ico" /> The <link> HTML element specifies relationships between the current document and an external resource. This element is most commonly used to link to stylesheets, but is also used to establish site icons (both "favicon" style icons and icons for the home screen and apps on mobile devices) among other things. https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/link Ngoài hai giá trị rel (relation) stylesheet , icon nói trên, còn nhiều giá trị khác, trong đó đáng chú ý là alternate The rel attribute defines the relationship between a linked resource and the current document Alternate representations of the current document. rel="alternate" dùng để liên kế...

Ansible không ưu tiên CLI option --user cao bằng inventory

99% các chương trình có giao diện dòng lệnh CLI đều ưu tiên option được người dùng chỉ định mức ưu tiên cao nhất. Ansible đặc biệt, vì nó nằm trong 1% còn lại. Mặc định, ansible sử dụng user của người dùng gõ lệnh $USER để ssh vào máy cần quản lý. Người dùng có thể thay đổi giá trị này với option --user REMOTE_USER . Nhưng khi trong inventory có chỉ định ansible_user thì khác: all: hosts: vm1: ansible_host: 127.0.0.1 ansible_user: inventory_user Chạy: $ ansible-playbook -i inventory.yml order.yml --user cli_user PLAY [all] ************************************************************************** TASK [Gathering Facts] ************************************************************** [ERROR]: Task failed: Failed to connect to the host via ssh: inventory_user@127.0.0.1: Permission denied (publickey,password). fatal: [vm1]: UNREACHABLE! => {"changed": false, "msg": "Task failed: Failed to connect to the host via ssh: inventory_user@127....

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...

[TIL] Docker trên MacOS share toàn bộ thư mục $HOME /Users/you với Docker Machine

Docker chạy được trên các hệ điều hành không phải Linux là 1 điều kỳ diệu. Điều kỳ diệu ấy nhờ việc các hệ điều hành tạo ra một máy ảo Linux rồi chạy container trên đó. Máy ảo này thường gọi là Docker machine, Podman machine, hay cái gì đó machine. Nhưng làm thế nào khi bind mount: -v $PWD:/app , thư mục hiện tại trên MacOS, ví dụ /Users/pikachu/fml lại xuất hiện trong container? Share /Users cho máy ảo Hầu hết người dùng đều mount từ 1 thư mục nào đó trong "$HOME" của user vào container, vậy nên mặc định Docker machine share sẵn thư mục này, bao gồm tất cả mọi thứ, kể cả .bashrc hay .ssh, ... Virtual file shares By default the /Users, /Volumes, /private, /tmp and /var/folders directory are shared. If your project is outside this directory then it must be added to the list, otherwise you may get Mounts denied or cannot start service errors at runtime. https://docs.docker.com/desktop/settings-and-maintenance/settings/#virtual-file-shares Vì thư mục được mount nằm tron...

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