Skip to main content

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.0.0.1: Permission denied (publickey,password).", "unreachable": true}

PLAY RECAP **************************************************************************
vm1                        : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   

ansible sử dụng user inventory_user cấu hình trong file inventory.yml để kết nối tới máy vm1 chứ không dùng cli_user và thất bại:

Failed to connect to the host via ssh: inventory_user@127.0.0.1: Permission denied.

Đừng cảm thấy cô đơn, vì bạn không phải người duy nhất: ansible -u does not take precedence over ansible_ssh_user from inventory #4622

Ansible set CLI option ở mức ưu tiên thấp nhất

Some behavioral parameters that you can set in variables you can also set in Ansible configuration, as command-line options, and using playbook keywords. For example, you can define the user that Ansible uses to connect to remote devices as a variable with ansible_user, in a configuration file with DEFAULT_REMOTE_USER, as a command-line option with -u, and with the playbook keyword remote_user. If you define the same parameter in a variable and by another method, the variable overrides the other setting. This approach allows host-specific settings to override more general settings. For examples and more details on the precedence of these various settings, see Controlling how Ansible behaves: precedence rules.

ansible_user là 1 variable, nó tuân theo luật ưu tiên:

Ansible does apply variable precedence, and you might have a use for it. Here is the order of precedence from least to greatest (the last listed variables override all other variables):

. Command-line values (for example, -u my_user, these are not variables)

...

. Inventory file or script host vars [2]_

tức command-line value có mức ưu tiên thấp nhất.

When you type something directly at the command line, you may feel that your hand-crafted values should override all others, but Ansible does not work that way. Command-line options have low precedence - they override configuration only. They do not override playbook keywords, variables from inventory or variables from playbooks. source

Giải pháp

#. Extra vars (for example, -e "user=my_user")(always win precedence) source

extra vars được gán qua option -e có độ ưu tiên cao nhất, thay vì -u cli_user, hãy dùng -e ansible_user=cli_user.

Kết luận

Ansible rất thành công so với các đối thủ lừng lẫy một thời của nó: Salt, Puppet, Chef. Người thành công thường có lối đi riêng ???!!!

Hết.

HVN at https://pymi.vn and https://www.familug.org.

Ủng hộ tác giả 🍺

Comments

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