Ansible 设置被控节点环境变量和设置模块默认值

Ansible 为被控节点设置环境变量

Ansible Playbook 通过 environment 设置被控主机的变量。

- name: test environment
  hosts: all
  become: true
  gather_facts: true
  environment:
    test_path: testpath
    PATH: "/usr/local/custon_software/bin/:{{ ansible_env.PATH }}"
  tasks:
  - name: get env
    ansible.builtin.shell: /bin/env
    register: shell_result
  - name: print env
    ansible.builtin.debug:
      var: shell_result.stdout_lines

这个例子里,Ansible 为被控主机添加了一个 test_path=testpath 变量,并修改 PATH 变量为 /usr/local/custon_software/bin/:$PATH

Ansible 为模块设置默认值

如果某个模块经常被调用,Ansible 可以通过 module_defaults 设置模块默认值以提高使用效率。

- hosts: localhost
  module_defaults:
    ansible.builtin.file:
      owner: root
      group: root
      mode: 0755
  tasks:
    - name: Create file1
      ansible.builtin.file:
        state: touch
        path: /tmp/file1

    - name: Create file2
      ansible.builtin.file:
        state: touch
        path: /tmp/file2

    - name: Create file3
      ansible.builtin.file:
        state: touch
        path: /tmp/file3

module_defaults 可以在 Playbook、block 和 tasks 级别使用。

在 tasks 中指定的参数值会覆盖默认值。

可以用空字典来删除之前的默认值:

- name: Create file1
  ansible.builtin.file:
    state: touch
    path: /tmp/file1
  module_defaults:
    file: {}

设置默认值可能会造成意想不到的问题,比方说设置的默认值影响动态导入静态导入的任务执行。

更多信息查看:https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_module_defaults.html

Ansible 设置被控节点环境变量和设置模块默认值
https://www.linuxstudynotes.com/2025/09/07/ansible/ansible-%e8%ae%be%e7%bd%ae%e8%a2%ab%e6%8e%a7%e8%8a%82%e7%82%b9%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f%e3%80%81%e8%ae%be%e7%bd%ae%e6%a8%a1%e5%9d%97%e9%bb%98%e8%ae%a4%e5%80%bc%e5%92%8c%e4%bb%bb%e5%8a%a1/
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇