在Ansible中设置环境变量(如PATH、GOPATH等)的通用方法如下:
- name: Set environment variables
become: yes
become_user: root
become_method: sudo
command: echo "export PATH=$PATH:/new/path" >> /etc/profile
environment:
PATH: "{{ ansible_env.PATH }}:/new/path"
这个任务将在目标主机上执行echo "export PATH=$PATH:/new/path" >> /etc/profile
命令,将新的路径添加到/etc/profile
文件中。然后,通过environment
关键字将新的PATH值添加到当前会话的环境变量中。
- name: Set GOPATH environment variable
become: yes
become_user: root
become_method: sudo
command: echo "export GOPATH=/path/to/gopath" >> /etc/profile
environment:
GOPATH: /path/to/gopath
这个任务将在目标主机上执行echo "export GOPATH=/path/to/gopath" >> /etc/profile
命令,将新的GOPATH路径添加到/etc/profile
文件中,并将GOPATH添加到当前会话的环境变量中。
这是一个设置环境变量的基本示例,你可以根据需要进行修改和扩展。