首页 > 其他分享 >ansible 变量优先级示例

ansible 变量优先级示例

时间:2024-08-17 23:26:24浏览次数:7  
标签:107.151 优先级 vars 示例 default ansible role 199.209 ok

目录

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 

标签:107.151,优先级,vars,示例,default,ansible,role,199.209,ok
From: https://www.cnblogs.com/anyux/p/18365160

相关文章

  • ansible自动化部署zookeeper
    一、准备环境1、准备机器本次以192.168.190.155,192.168.190.156,192.168.190.157,192.168.190.158四台虚机为例,本文操作全在管理机中。机器IP节点CPU&内存系统盘存储192.168.190.155Ansible管理机2C4G50GB50GB192.168.190.158Master/Worker(被管理机)2......
  • 快速掌握 LaTeX:实战示例与基础概念【文末送书】
    文章目录快速掌握LaTeX:实战示例与基础概念LaTeX文档结构LaTeX实战示例LaTeX入门实战【文末送书】快速掌握LaTeX:实战示例与基础概念LaTeX是一种广泛使用的排版系统,特别适用于数学和科学文档。它提供了强大的格式控制功能,使用户能够创建高质量的文档。本文将介......
  • 【CPP】C++模板:初阶到进阶语法与实用编程示例
    关于我:睡觉待开机:个人主页个人专栏:《优选算法》《C语言》《CPP》生活的理想,就是为了理想的生活!作者留言PDF版免费提供:倘若有需要,想拿我写的博客进行学习和交流,可以私信我将免费提供PDF版。留下你的建议:倘若你发现本文中的内容和配图有任何错误或改进建......
  • 合宙Air780EP模组LuatOS脚本开发MQTT应用示例
    本文详细讲解了基于合宙Air780EP模组LuatOS开发的多个MQTT应用示例。本文同样适用于合宙的以下型号:Air780EPA/Air780EPT/Air780EPSAir780E/Air780EX/Air201…一、相关准备工作1.1硬件准备合宙EVB_Air780EP开发板一套,包括天线、SIM卡;USB线PC电脑1.2软件准备登录合宙......
  • 2024.8.16(ansible)
    一、回顾1、mysql和python    1.mysql5.7        1.1不需要执行mysql_ssl_rsa_setup        1.2Change_master_to.不需要getpublickey    2.可以使用pymysql非交互的管理mysql        2.1 conn......
  • 【JAVA】深入理解守护线程与非守护线程:概念、应用及示例
    文章目录介绍1.线程的基础知识2.守护线程与非守护线程2.1什么是守护线程?特点:2.2什么是非守护线程?特点:3.为什么需要守护线程?示例:后台任务处理示例:日志记录4.非守护线程的应用场景示例:数据库连接处理5.守护线程与非守护线程的对比6.总结更多相关内容可查......
  • ansible【自动化配置】(thirty day)
    回顾1、mysql和python(1)不需要执行mysql_ssl_rsa_setup(2)Change_master_to.不需要getpublickey2、可以使用pymysql非交互的管理mysql(1)conn=pymysql.connect(host,user,password,database,prot)(2)cursor=conn.cursor();(3)Cursor.execute("createuser....")(4)cursor.ex......
  • DuckDB_SQL-使用示例以及和PG之间的概念
    duckdbCatalog(目录):表示整个数据库或数据库管理系统。一个数据库服务器可以包含多个数据库,每个数据库都有自己的Catalog1.database--catalogcatalog_namedatabase:In‑Memoryvs.PersistentDatabasedatabase--database_listnew_db.my_schema:system......
  • 前端新手必看:掌握CSS样式优先级和!important,让你的样式不再冲突!(CSS优先级和!important
    文章目录......
  • IMU惯性测量模块在ROS环境下的应用示例
    Ubuntu版本:20.04;ROS环境:noetic;IMU型号:亚博10轴IMU惯导模块目录一.ROS环境配置1、在终端运行对应的命令 2、安装ROS串口驱动二、IMU软件包使用1、新建、编译工作空间 2、绑定IMU端口3、修改参数配置 三、运行可视化界面 1、运行launch文件2、可能遇到的问题3、......