Logo

Personal Ops Runbook

Personal runbook covering infrastructure operations for Cloud, Kubernetes, OpenStack, and Ceph environments. Includes deployment and teardown procedures, node management, cluster monitoring setup, and incident response workflows compiled from day-to-day operational work. Intended strictly for personal reference — configurations and scripts are environment-specific and not guaranteed to work as-is elsewhere.

[Ansible] Shell và register


Để lấy kết quả của lệnh $(lsb_release -rs) và sử dụng biến đó trong các task tiếp theo, bạn có thể sử dụng module shell trong Ansible như sau:

- name: Get Ubuntu version
  shell: lsb_release -rs
  register: ubuntu_version

- name: Install Docker
  apt:
    name: docker-ce
    state: latest
  when: ubuntu_version.stdout == "20.04"

Trong ví dụ này, module shell được sử dụng để chạy lệnh lsb_release -rs và lưu kết quả vào biến ubuntu_version. Sau đó, biểu thức when được sử dụng để kiểm tra xem phiên bản Ubuntu có phải là 20.04 hay không. Nếu đúng, task cài đặt Docker sẽ được thực thi.

Để in biến lấy được ra màn hình trong Ansible, bạn có thể sử dụng module debug. Module này được sử dụng để in ra các giá trị của các biến hoặc các thông tin khác trên stdout.

Ví dụ, nếu bạn muốn in biến ubuntu_version ở ví dụ trước đó, bạn có thể sử dụng module debug như sau:

- name: Get Ubuntu version
  shell: lsb_release -rs
  register: ubuntu_version

- name: Print Ubuntu version
  debug:
    var: ubuntu_version.stdout

Trong đoạn mã này, module debug được sử dụng để in ra giá trị của biến ubuntu_version.stdout. Khi chạy playbook, giá trị của biến này sẽ được in ra trên stdout.