Ansible版本和操作系统信息:
ansible 2.5.0
config file = None
configured module search path = [u'/home/naftuli/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /home/naftuli/.local/lib/python2.7/site-packages/ansible
executable location = /home/naftuli/.local/bin/ansible
python version = 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]
它是基本的OS Loki,它是Ubuntu 16.04,运行内核4.13.0-38-generic。
One interesting issue reported to me中的 one of my Ansible modules是synchronize
任务在远程计算机上执行该角色的用户失败。
由于我一直使用connection: local
在我自己的机器上应用东西,所以在我向我报告之前我没有遇到过这种情况。
任务定义如下:
- name: sync man pages
synchronize:
src: "{{ rust_user_home }}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/share/man/man1/"
dest: "/usr/local/share/man/man1/"
become: true
因为我总是在本地运行,所以"遥控器"机器和本地机器一样,所以效果很好; according to the module documentation使用上述代码,src
将从本地主机复制到远程主机上的dest
。
然后我在那个PR做了改变:
- name: sync man pages
synchronize:
src: "{{ rust_user_home }}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/share/man/man1/"
dest: "/usr/local/share/man/man1/"
delegate_to: "{{ inventory_hostname }}"
become: true
但是,现在在本地运行时,Ansible会尝试SSH到我的本地计算机。使用synchronize
:
rsync -a /src/dir/ /dest/dir/
如何使用synchronize
模块近似此行为?
答案 0 :(得分:0)
我似乎能够使用un(der)记录的ansible_connection
事实找到解决方法:
- name: establish connection type
set_fact:
is_connection_local: |-
{%- if ansible_connection == "local" -%}
true
{%- else -%}
false
{%- endif -%}
- name: sync man pages (local)
synchronize:
src: "{{ rust_user_home }}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/share/man/man1/"
dest: "/usr/local/share/man/man1/"
when: is_connection_local
become: true
- name: sync man pages (remote)
synchronize:
src: "{{ rust_user_home }}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/share/man/man1/"
dest: "/usr/local/share/man/man1/"
delegate_to: "{{ inventory_hostname }}"
when: not is_connection_local
become: true
如果远程连接,它使用委托在正确的位置运行,如果本地它只是按预期使用本地文件系统。