我正在尝试将/usr/pgsql-10/bin
添加到$PATH
,因为我希望每个使用机器的人都能够运行psql
命令。
尝试遵循此示例:
- name: add {{extra_path}} to path
lineinfile:
dest: /etc/environment
state: present
backrefs: yes
regexp: 'PATH=(["]*)((?!.*?{{extra_path}}).*?)(["]*)$'
line: "PATH=12:{{extra_path}}3"
首先,我不太明白我应该如何修改它。我应该只用我的路径替换extra_path
还是整个{{extra_path}}
(/usr/pgsql-10/bin
(。
我尝试了任何一种方式,但都得到了不同的错误。更糟糕的是,我的/etc/environment
甚至不包含PATH
。
仅声明其他路径
vars:
extra_path: /usr/pgsql-10/bin
以下任务基于从响应到使用 ansible 更新 PATH 的想法 - 系统范围
- 如果文件在控制器上,请测试本地文件
- 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*PATHs*=')
- name: 'Add {{ extra_path }} to PATH'
lineinfile:
path: /etc/environment
regexp: 'PATH=(["])((?!.*?{{ extra_path }}).*?)(["])$'
line: 'PATH=12:{{ 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*PATHs*=')
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=12:{{ extra_path }}3'
backrefs: yes
请参阅 Python 正则表达式。
这是另一个很棒的解决方案:
- name: Add a path to system-wide $PATH.
ansible.builtin.copy:
dest: /etc/profile.d/custom-path.sh
content: 'PATH=$PATH:{{ path_var }}'
在这种情况下,path_var
是指要添加到系统范围$PATH
的路径。下次登录时,您应该会在echo $PATH
时看到自定义路径!
学分:杰夫·格尔林