gpt4 book ai didi

centos - Ansible - 如何在 CentOS 中添加/修改 PATH 变量?

转载 作者:太空宇宙 更新时间:2023-11-03 17:20:25 25 4
gpt4 key购买 nike

我正在尝试将 /usr/pgsql-10/bin 添加到 $PATH,因为我希望使用该机器的每个人都能够运行psql 命令。

试图关注 this示例:

- name: add {{extra_path}} to path
lineinfile:
dest: /etc/environment
state: present
backrefs: yes
regexp: 'PATH=(["]*)((?!.*?{{extra_path}}).*?)(["]*)$'
line: "PATH=\1\2:{{extra_path}}\3"

首先,我不太明白我应该如何修改它。我应该只用我的路径 (/usr/pgsql-10/bin) 替换 extra_path 还是整个 {{extra_path}}

我尝试了两种方法,但都遇到了不同的错误。更糟糕的是,我的 /etc/environment 甚至不包含 PATH

最佳答案

仅声明附加路径

  vars:
extra_path: /usr/pgsql-10/bin

以下任务基于 Response to updating PATH with ansible - system wide 的想法

  • 如果文件在 Controller 中测试本地文件
    - name: 'Add {{ extra_path }} if PATH does not exist'
lineinfile:
path: /etc/environment
line: 'PATH="{{ extra_path }}"'
insertafter: EOF
when: lookup('file', '/etc/environment') is not search('^\s*PATH\s*=')

- name: 'Add {{ extra_path }} to PATH'
lineinfile:
path: /etc/environment
regexp: 'PATH=(["])((?!.*?{{ extra_path }}).*?)(["])$'
line: 'PATH=\1\2:{{ extra_path }}\3'
backrefs: yes
  • 如果文件在远程主机上,首先获取它们。为了使游戏幂等,不要在获取时报告更改。使目的地符合您的需求

- name: 'Fetch /etc/environment to {{ playbook_dir }}/environments'
fetch:
src: /etc/environment
dest: "{{ playbook_dir }}/environments"
changed_when: false

- name: 'Add {{ extra_path }} if PATH does not exist'
lineinfile:
path: /etc/environment
line: 'PATH="{{ extra_path }}"'
insertafter: EOF
when: lookup('file', path) is not search('^\s*PATH\s*=')
vars:
path: "{{ path_items|path_join }}"
path_items:
- "{{ playbook_dir }}"
- environments
- "{{ inventory_hostname }}"
- etc/environment

- name: 'Add {{ extra_path }} to PATH'
lineinfile:
path: /etc/environment
regexp: 'PATH=(["])((?!.*?{{ extra_path }}).*?)(["])$'
line: 'PATH=\1\2:{{ extra_path }}\3'
backrefs: yes

参见 Python regex .

关于centos - Ansible - 如何在 CentOS 中添加/修改 PATH 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56560173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com