ansible 變數優先順序示例

anyux發表於2024-08-17

目錄
  • ansible 變數優先順序示例
    • 1.不是變數的變數
    • 2.角色預設值
    • 3.主機配置清單 或 動態指令碼生成的 group vars
    • 4.主機配置清單 group_vars/all
    • 5.劇本 group_vars/all
    • 6 主機配置清單 group_vars/*
    • 7 劇本 group_vars/*
    • 8.主機清單檔案 or 動態生成主機清單檔案的主機變數
    • 9.主機清單檔案 host_var/*
    • 10.劇本 host_vars/*
    • 11. host facts / cached set_facts
    • 12. 劇本 vars
    • 13. 劇本 vars_prompt
    • 14. play vars_files
    • 15 角色 變數
    • 16 塊 vars (only for tasks in block)
    • 17. 任務 vars (only for the task)
    • 18. include_vars
    • 19. set_facts / registered vars
    • 20.角色與 引入角色 引數
    • 21.include params
    • 22. 額外變數 (for example, -e "user=my_user")(always win precedence)

ansible 變數優先順序示例

ansible多處都可設定變數,但是其中變數優先順序不清楚,這裡按官方示例文件基於最新2.17 ansible-core版本,編寫驗證編寫驗證變數優先順序的示例
以下按從優先順序低到高排序

1.不是變數的變數

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

這裡使用變數ansible_user,它一般對應引數 -u 或 inventory檔案的 ansible_user變數

這裡說的最低的優先順序是: 如果inventory配置了ansible_user 大於 -u 權重

ansible的非 -e 方式傳遞引數權重最低

priority01.yml

---
- hosts: ubuntu
  tasks:
    - name: Display the user running the task
      debug:
        msg: "The user is {{ ansible_user }}"

/etc/ansible/hosts

[ubuntu]
107.151.199.209 ansible_user=root
ansible-playbook -u rootb  priority01.yml

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [Display the user running the task] **********************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "The user is root"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

2.角色預設值

role defaults (as defined in Role directory structure)

先建立一個簡單的role

#手動建立角色的基本目錄結構
#tasks定義任務
#defaults: 角色預設變數
#files: 存放檔案
#templates: 模板檔案
#handlers: 觸發器
#vars: 高於defaults的變數儲存目錄
#meta: 後設資料資訊
mkdir -p my_simple_role/{tasks,defaults,files,templates,handlers,vars,meta}

編輯task檔案
my_simple_role/tasks/main.yml

- name: print vars 
  debug:
    msg: "{{ role_default_vars }}"

編輯預設變數檔案
my_simple_role/defaults/main.yml

role_default_vars: "role_default_vars_with_defaults"

編輯可用playbook檔案
playbook.yaml

---
 - hosts: ubuntu
   roles:
     - my_simple_role

3.主機配置清單 或 動態指令碼生成的 group vars

inventory file or script group vars

這是指在 inventory 檔案或動態 inventory 指令碼中為特定組定義的變數
一般形式是
[groupname:vars]

動態 inventory 指令碼生成的變數暫時用不到

在inventory中也可以配置組變數

/etc/ansible/hosts

[ubuntu]
107.151.199.209 ansible_user=root 

[ubuntu:vars]
role_default_vars="role_default_vars_with_inventory_group_vars"

ansible-playbook playbook.yaml 

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_inventory_group_vars"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

4.主機配置清單 group_vars/all

inventory group_vars/all

這裡指的是 /etc/ansible/group_vars/all檔案

/etc/ansible/group_vars/all

---
role_default_vars: role_default_vars_with_group_vars_all
ansible-playbook playbook.yaml 

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_group_vars_all"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

5.劇本 group_vars/all

這裡指的playbook.yaml檔案成的位置下的 group_vars/all檔案變數

#建立目錄
mkdir group_vars

group_vars/all

---
role_default_vars: role_default_vars_with_playbook_group_vars_all
ansible-playbook playbook.yaml 

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_playbook_group_vars_all"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

6 主機配置清單 group_vars/*

inventory group_vars/*

示例

/etc/ansible/group_vars/ubuntu

指的是整個ubuntu的組變數

*表示group_vars目錄下所有的檔案,一般檔名稱對應組名稱

/etc/ansible/group_vars/ubuntu

---
  tar: zar
  ping: pong
  foo_list:
    - one
    - two
    - three
  role_default_vars: role_default_vars_with_group_vars_ubuntu
ansible-playbook playbook.yaml 

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_group_vars_ubuntu"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

7 劇本 group_vars/*

playbook group_vars/*

示例

group_vars/ubuntu

指的是當前playbook.yaml劇本目錄下

*表示group_vars目錄下所有的檔案,一般檔名稱對應組名稱

---
role_default_vars: role_default_vars_with_playbook_group_vars_ubuntu
ansible-playbook playbook.yaml 

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_playbook_group_vars_ubuntu"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

8.主機清單檔案 or 動態生成主機清單檔案的主機變數

inventory file or script host vars

示例

[webservers]

webserver1 ansible_host=192.168.1.10 http_port=8080

/etc/ansible/hosts

[ubuntu]
107.151.199.209 ansible_user=root role_default_vars="role_default_vars_with_inventory_hosts_vars"
[ubuntu:vars]
role_default_vars="role_default_vars_with_inventory_group_vars"
ansible-playbook /root/handlers/priority/playbook.yaml 

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_inventory_hosts_vars"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

9.主機清單檔案 host_var/*

示例
/etc/ansible/host_vars/107.151.199.209
指定單個主機變數資訊

/etc/ansible/host_vars/107.151.199.209

---
  foo: bar
  baz: qux
  role_default_vars: "role_default_vars_with_inventory_hosts_vars_hosts_vars"
ansible-playbook /root/handlers/priority/playbook.yaml 

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_inventory_hosts_vars_hosts_vars"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

10.劇本 host_vars/*

示例
host_vars/107.151.199.209
在playbook.yaml檔案目錄下的 host_vars/ip檔案中指定單個主機變數資訊

mkdir host_vars

host_vars/107.151.199.209

---
role_default_vars: "role_default_vars_with_playbook_hosts_vars"
ansible-playbook /root/handlers/priority/playbook.yaml 

PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_playbook_hosts_vars"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

11. host facts / cached set_facts

Host Facts 是 Ansible 透過setup自動收集的系統資訊,用於描述目標主機的狀態
Cached Set Facts 是您透過 set_fact 動態設定並選擇快取的自定義變數

cached set_facts 指的是將獲取到的變數快取,可以與redis關聯,或透過內建set_fact模組快取變數,此配置較為複雜,不再詳細描述

當前的檔案裡沒有與facts相關的變數,facts變數有一個叫ansible_os_family的,會列印出作業系統名稱,那麼我使用第10個優先順序的變數配置下

host_vars/107.151.199.209

---
role_default_vars: "role_default_vars_with_playbook_hosts_vars"
ansible_os_family: "None"

修改role
my_simple_role/tasks/main.yml

- name: print vars 
  debug:
    msg: 
     - "{{ role_default_vars }}"
     - "{{ ansible_os_family }}"

修改playbook.yaml
gather_facts: no 禁止獲取系統資訊

--- 
 - hosts: ubuntu
   gather_facts: no
   roles:
     - my_simple_role

此時列印的變數ansible_os_family結果為None是符合設定的變數的

ansible-playbook playbook.yaml 

PLAY [ubuntu] *****

TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_playbook_hosts_vars",
        "None"
    ]
}

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

再次修改playbook.yaml
gather_facts: yes 獲取系統資訊

--- 
 - hosts: ubuntu
   gather_facts: yes
   roles:
     - my_simple_role

此時變數已被facts覆蓋,獲取到真實的系統名稱

ansible-playbook playbook.yaml 

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [107.151.199.209]

TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_playbook_hosts_vars",
        "Debian"
    ]
}

PLAY RECAP *****
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

12. 劇本 vars

play vars
就是在playbook 劇本中設定變數

playbook.yaml

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   roles:
     - my_simple_role

ansible-playbook playbook.yaml 

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [107.151.199.209]

TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_playbook_vars",
        "Debian"
    ]
}

PLAY RECAP *****
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

13. 劇本 vars_prompt

vars_prompt 模組
就是在playbook 劇本中手動設定變數

修改playbook檔案,playbook.yaml
name: "role_default_vars" 指定變數名稱

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   vars_prompt:
    - name: "role_default_vars"
      prompt: "Please enter role_default_vars:"
      private: no
   roles:
     - my_simple_role

手動輸入變數值

ansible-playbook playbook.yaml 
Please enter role_default_vars:: role_default_vars_with_prompt                   

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [107.151.199.209]

TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_prompt",
        "Debian"
    ]
}

PLAY RECAP *****
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

14. play vars_files

vars_files 模組
vars_files 載入變數檔案,將變數定義儲存在單獨的 YAML 檔案中,並在 Playbook 中引用這些檔案,從而使變數管理更加模組化和易於維護

建立目錄

mkdir vars

建立變數檔案
vars/vars.yaml

---
role_default_vars: "role_default_vars_with_playbook_vars_file"

修改playbook.yaml檔案

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   vars_files:
    - "vars/vars.yaml"
   vars_prompt:
    - name: "role_default_vars"
      prompt: "Please enter role_default_vars:"
      private: no
   roles:
     - my_simple_role
ansible-playbook playbook.yaml 
Please enter role_default_vars:: role_default_vars_with_prompt

PLAY [ubuntu] ******

TASK [Gathering Facts] ******
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_playbook_vars_file",
        "Debian"
    ]
}

PLAY RECAP ******
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

15 角色 變數

role vars (as defined in Role directory structure)
在角色變數中定義檔案,需要透過vars/main.yml檔案

vars/main.yaml

---
role_default_vars: "role_default_vars_with_role_vars_main"

playbook.yaml

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   vars_files:
    - "vars/vars.yaml"
   vars_prompt:
    - name: "role_default_vars"
      prompt: "Please enter role_default_vars:"
      private: no
   roles:
     - my_simple_role
ansible-playbook playbook.yaml 
Please enter role_default_vars:: role_default_vars_with_prompt

PLAY [ubuntu] ******

TASK [Gathering Facts] ******
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_role_vars_main",
        "Debian"
    ]
}

PLAY RECAP ******
107.151.199.209            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

16 塊 vars (only for tasks in block)

block vars (only for tasks in block)
block 是 Ansible 中的一種結構化語法,允許您將一組任務組織在一起,並對整個塊應用特定的處理,比如錯誤處理、條件判斷或變數定義

   - block:
       - name: Print a message
         debug:
           msg: "The variable inside block is {{ block_var }}"

       - name: Print another message
         debug:
           msg: "This is another task using the block variable {{ block_var }}"

     vars:
       block_var: "This is a block-level variable"

block塊下的變數 block_var只會作用於block塊區域中

因vars_prompt需要反覆輸入變數,此時移除此程式碼,新增block程式碼
playbook.yaml

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   vars_files:
    - "vars/vars.yaml"
   roles:
     - my_simple_role

my_simple_role/tasks/main.yml

- name: print vars 
  debug:
    msg: 
     - "{{ role_default_vars }}"
     - "{{ ansible_os_family }}"

- block: 
    - name: print block vars
      debug:
        msg:
         - "{{ role_default_vars }}"
         - "{{ ansible_os_family }}"
  vars:
    role_default_vars: "role_default_vars_with_block_vars"
ansible-playbook playbook.yaml

PLAY [ubuntu] ******

TASK [Gathering Facts] ******
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_role_vars_main",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_block_vars",
        "Debian"
    ]
}

PLAY RECAP ******
107.151.199.209            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

17. 任務 vars (only for the task)

task vars (only for the task)

playbook.yaml

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   vars_files:
    - "vars/vars.yaml"
   tasks:
    - name: print task vars
      debug:
        msg: "{{ role_default_vars }}"
      vars:
        role_default_vars: "role_default_vars_with_tasks_vars"

   roles:
     - my_simple_role
ansible-playbook playbook.yaml

PLAY [ubuntu] ******

TASK [Gathering Facts] ******
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_role_vars_main",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_block_vars",
        "Debian"
    ]
}

TASK [print task vars] ******
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_tasks_vars"
}

PLAY RECAP ******
107.151.199.209            : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

18. include_vars

include_vars
用於動態載入變數,這與前面 vars_files 有不同

特性 include_vars vars_files
功能 動態載入變數檔案 靜態載入變數檔案
使用位置 在任務中使用,可以結合條件和迴圈 在 Playbook 頂層定義
載入時機 在任務執行時載入 在 Playbook 開始時載入
靈活性 高,可動態載入不同的變數檔案 低,載入固定的變數檔案
典型使用場景 根據條件或其他動態因素載入變數檔案,可以與條件和迴圈結合使用 預先載入全域性變數檔案,適用於預先載入固定變數檔案的場景

include_vars.yaml

---
role_default_vars: "role_default_vars_with_playbook_include_vars"

playbook.yaml
新增include_vars模組

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   vars_files:
    - "vars/vars.yaml"
   tasks:
    - name: include vars
      include_vars: "include_vars.yaml"
    - name: print task vars
      debug:
        msg: "{{ role_default_vars }}"
      vars:
        role_default_vars: "role_default_vars_with_tasks_vars"

   roles:
     - my_simple_role
ansible-playbook playbook.yaml 

PLAY [ubuntu] ******

TASK [Gathering Facts] ******
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_role_vars_main",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_block_vars",
        "Debian"
    ]
}

TASK [include vars] ******
ok: [107.151.199.209]

TASK [print task vars] ******
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_playbook_include_vars"
}

PLAY RECAP ******
107.151.199.209            : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

19. set_facts / registered vars

set_facts 是一個模組,可顯示設定變數
register 只是用於承接任務的結果標準輸出和錯誤輸出
因為顯示兩者明顯區別要配置facts_cache快取及多playbook任務,較為複雜,因此暫時略過,後結再行構建示例

特性 set_facts register
建立方式 顯式地建立或修改變數 捕獲任務的輸出結果
作用範圍 可配置facts_cache開啟快取,實現多playbook全域性有效,否則與register作用範圍相同 僅對建立此變數的playbook有效
使用場景 動態設定主機變數,用於 playbook 內的變數共享 捕獲 task 的執行結果,用於後續 tasks 的條件判斷或資訊展示

my_simple_role/tasks/main.yml

- name: print vars 
  debug:
    msg: 
     - "{{ role_default_vars }}"
     - "{{ ansible_os_family }}"

- block: 
    - name: print block vars
      debug:
        msg:
         - "{{ role_default_vars }}"
         - "{{ ansible_os_family }}"
  vars:
    role_default_vars: "role_default_vars_with_block_vars"
  
- name: set register vars 
  shell: "echo role_default_vars_with_register_vars"
  register: role_default_vars

- name: print role_default_vars  vars
  debug:
    msg: 
     - "{{ role_default_vars.stdout }}"

- name: set set_fact vars 
  set_fact:
    role_default_vars: "role_default_vars_with_set_fact_vars"

- name: print role_default_vars  vars
  debug:
    msg: 
     - "{{ role_default_vars }}"
ansible-playbook playbook.yaml 

PLAY [ubuntu] *******

TASK [Gathering Facts] *******
ok: [107.151.199.209]

#....#略過

TASK [my_simple_role : set register vars] *******
changed: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_register_vars"
    ]
}

TASK [my_simple_role : set set_fact vars] *******
ok: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_set_fact_vars"
    ]
}

#....#略過

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

20.角色與 引入角色 引數

role (and include_role) params

role引入角色的方式與-tasks類似,必須與hosts繫結,include_role可以在playbook的任意位置引入

這裡說明下,playbook執行順序,在同一檔案中包含tasks與roles時,先執行roles,再執行tasks,tasks中的include_roles按tasks順序正常執行,所以此playbook有兩次引入了role,roles方式載入引入時,變數role_default_vars是有stdout屬性的,而在include_roles引入時,因為變數被引入,導致變數role_default_vars是沒有stdout屬性的,因此配置了ignore_errors
playbook.yaml

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   vars_files:
    - "vars/vars.yaml"
   tasks:
    - name: include vars
      include_vars: "include_vars.yaml"
    - name: print task vars
      debug:
        msg: "{{ role_default_vars }}"
      vars:
        role_default_vars: "role_default_vars_with_tasks_vars"
    - name: "include_role 載入方式"
      include_role: 
        name: my_simple_role
      vars:
        role_default_vars: "role_default_vars_with_include_role_params_vars"
   roles:
     - name: "role 方式載入"
       role: my_simple_role
       vars:
         role_default_vars: "role_default_vars_with_role_params_vars"

/root/handlers/priority/my_simple_role/tasks/main.yml

- name: print vars 
  debug:
    msg: 
     - "{{ role_default_vars }}"
     - "{{ ansible_os_family }}"

- block: 
    - name: print block vars
      debug:
        msg:
         - "{{ role_default_vars }}"
         - "{{ ansible_os_family }}"
  vars:
    role_default_vars: "role_default_vars_with_block_vars"
  
- name: set register vars 
  shell: "echo role_default_vars_with_register_vars"
  register: role_default_vars

- name: print role_default_vars  vars
  debug:
    msg: 
     - "{{ role_default_vars.stdout }}"
  ignore_errors: yes

- name: set set_fact vars 
  set_fact:
    role_default_vars: "role_default_vars_with_set_fact_vars"

- name: print role_default_vars  vars
  debug:
    msg: 
     - "{{ role_default_vars }}"
ansible-playbook playbook.yaml 

PLAY [ubuntu] ******

TASK [Gathering Facts] ******
ok: [107.151.199.209]

TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_role_params_vars",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_block_vars",
        "Debian"
    ]
}

TASK [my_simple_role : set register vars] ******
changed: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_register_vars"
    ]
}

TASK [my_simple_role : set set_fact vars] ******
ok: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_set_fact_vars"
    ]
}

TASK [include vars] ******
ok: [107.151.199.209]

TASK [print task vars] ******
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_set_fact_vars"
}

TASK [include_role 載入方式] ******
included: my_simple_role for 107.151.199.209

TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_include_role_params_vars",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_include_role_params_vars",
        "Debian"
    ]
}

TASK [my_simple_role : set register vars] ******
changed: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] ******
fatal: [107.151.199.209]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'stdout'\n\nThe error appears to be in '/root/handlers/priority/my_simple_role/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print role_default_vars  vars\n  ^ here\n"}
...ignoring

TASK [my_simple_role : set set_fact vars] ******
ok: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] ******
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_include_role_params_vars"
    ]
}

PLAY RECAP ******
107.151.199.209            : ok=16   changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1 

21.include params

include params
includeinclude_tasks 可以使用 params 來傳遞變數

- name: 包含另一個任務檔案
 include_tasks: other_tasks.yml
 vars:
   param1: "value1"
   param2: "value2"

include_params.yaml

---
- name: Display vars
  debug:
    msg: "The user is {{ role_default_vars }}"

playbook.yaml

--- 
 - hosts: ubuntu
   gather_facts: yes
   vars:
    role_default_vars: "role_default_vars_with_playbook_vars"
   vars_files:
    - "vars/vars.yaml"
   tasks:
    - name: include vars
      include_vars: "include_vars.yaml"
    - name: print task vars
      debug:
        msg: "{{ role_default_vars }}"
      vars:
        role_default_vars: "role_default_vars_with_tasks_vars"
    - name: "include_role 載入方式"
      include_role: 
        name: my_simple_role
      vars:
        role_default_vars: "role_default_vars_with_include_role_params_vars"
    - name: print include_tasks_params_vars
      include_tasks: "include_params.yaml"
      vars:
        role_default_vars: "role_default_vars_with_include_task_params_vars"
   roles:
     - name: "role 方式載入"
       role: my_simple_role
       vars:
         role_default_vars: "role_default_vars_with_role_params_vars"

ansible-playbook playbook.yaml 

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [107.151.199.209]

TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_role_params_vars",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_block_vars",
        "Debian"
    ]
}

TASK [my_simple_role : set register vars] *****
changed: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_register_vars"
    ]
}

TASK [my_simple_role : set set_fact vars] *****
ok: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_set_fact_vars"
    ]
}

TASK [include vars] *****
ok: [107.151.199.209]

TASK [print task vars] *****
ok: [107.151.199.209] => {
    "msg": "role_default_vars_with_set_fact_vars"
}

TASK [include_role 載入方式] *****
included: my_simple_role for 107.151.199.209

TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_include_role_params_vars",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_include_role_params_vars",
        "Debian"
    ]
}

TASK [my_simple_role : set register vars] *****
changed: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *****
fatal: [107.151.199.209]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'stdout'\n\nThe error appears to be in '/root/handlers/priority/my_simple_role/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print role_default_vars  vars\n  ^ here\n"}
...ignoring

TASK [my_simple_role : set set_fact vars] *****
ok: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "role_default_vars_with_include_role_params_vars"
    ]
}

TASK [print include_tasks_params_vars] *****
included: /root/handlers/priority/include_params.yaml for 107.151.199.209

TASK [Display vars] *****
ok: [107.151.199.209] => {
    "msg": "The user is role_default_vars_with_include_task_params_vars"
}

PLAY RECAP *****
107.151.199.209            : ok=18   changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1 

22. 額外變數 (for example, -e "user=my_user")(always win precedence)

extra vars (for example, -e "user=my_user")(always win precedence)
這屬於開掛,像極了css的 !import 提升權重

ansible-playbook playbook.yaml  -e "role_default_vars=123"

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [107.151.199.209]

TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "123",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "123",
        "Debian"
    ]
}

TASK [my_simple_role : set register vars] *****
changed: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *****
fatal: [107.151.199.209]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'str object' has no attribute 'stdout'\n\nThe error appears to be in '/root/handlers/priority/my_simple_role/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print role_default_vars  vars\n  ^ here\n"}
...ignoring

TASK [my_simple_role : set set_fact vars] *****
ok: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "123"
    ]
}

TASK [include vars] *****
ok: [107.151.199.209]

TASK [print task vars] *****
ok: [107.151.199.209] => {
    "msg": "123"
}

TASK [include_role 載入方式] *****
included: my_simple_role for 107.151.199.209

TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "123",
        "Debian"
    ]
}

TASK [my_simple_role : print block vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "123",
        "Debian"
    ]
}

TASK [my_simple_role : set register vars] *****
changed: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *****
fatal: [107.151.199.209]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'str object' has no attribute 'stdout'\n\nThe error appears to be in '/root/handlers/priority/my_simple_role/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print role_default_vars  vars\n  ^ here\n"}
...ignoring

TASK [my_simple_role : set set_fact vars] *****
ok: [107.151.199.209]

TASK [my_simple_role : print role_default_vars  vars] *****
ok: [107.151.199.209] => {
    "msg": [
        "123"
    ]
}

TASK [print include_tasks_params_vars] *****
included: /root/handlers/priority/include_params.yaml for 107.151.199.209

TASK [Display vars] *****
ok: [107.151.199.209] => {
    "msg": "The user is 123"
}

PLAY RECAP *****
107.151.199.209            : ok=18   changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=2 

相關文章