ansible批量部署的问题

1. playbook 里面通过with_items 多次copy。打包到一个文件里面的弊端:打包后,包里的内容不可见了,以后需要新增、修改包里的文件时,重新打包,这样查看修改历史时比较麻烦,而且对于后来接手的人,也是个黑盒。2. 环境变量已经在.bash_profile中配置了,那就在执行start前,source一次.bash_profile啊。demo:---- hosts: "{{ host }}" remote_user: root gather_facts: false vars_files: - common_vars.yml tasks: - name: copy files copy: src=https://www.zhihu.com/source_path/{{ item }} dest=/dest_path/{{ item }} owner=root group=root mode=0644 with_items: - file1 - file2 - name: start service shell: source ~/.bash_profile /u0026amp;/u0026amp; start_command
■网友
- hosts: 192.168.0.76 gather_facts: no vars: MY_ENV_VARIABLE: whatever_value tasks: - name: Echo my_env_var debug: msg={{MY_ENV_VARIABLE}}如果你想ansible那台机器显示变量值的话可以这么干,当然做法还是要像@大撸子 那样写
【ansible批量部署的问题】 不过这个只是针对单一的变量,如果变量有多个,而且不同机器不一样的话,那最好还是用.bash_profile来控制

■网友
我觉得问题1很多时候不用特别关心,除非在使用的时候碰到什么特俗问题了。终于能翻墙了,对于问题2的正确解决办法。tasks: - name: Echo my_env_var shell: "echo $MY_ENV_VARIABLE" environment: MY_ENV_VARIABLE: whatever_valueSetting the Environment (and Working With Proxies)ansible官方文档已经提到。


    推荐阅读