我喜欢使用Ansible中的同步模块通过SSH将文件从一台服务器同步到其他服务器
- name: Copy files to all servers
synchronize:
src: /source/path/
dest: "rsync://{{ ansible_nodename }}:/destination/path/"
delegate_to: src-host
因此,默认情况下,此模块将使用清单名称,但与src主机不同,主机名是不同的。到目前为止,我发现的唯一方法是使用rsync://{{ ansible_nodename }}
,但是随后似乎不再通过SSH发生,我得到了No route to host (113)\nrsync error: error in socket IO (code 10) at clientserver.c(128) [sender=3.1.0]
我也尝试仅为此一项任务覆盖inventory_hostname
,到目前为止还没有运气。
例如,我想象这样的事情
- name: Copy custom config to all servers
synchronize:
src: /opt/app/dir/
dest: /opt/app/dir/
delegate_to: src-host
vars:
inventory_hostname: "{{ ansible_nodename }}"
但是,当使用以下消息来操作ventory_hostname时,它当然会失败
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: jinja2.exceptions.UndefinedError: "hostvars['{{ ansible_nodename }}']" is undefined
fatal: [my-host]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}
答案 0 :(得分:0)
如果要基于某些主机名对目标字符串进行模板化,则可以使用:
special variable inventory_hostname
(如果您的清单主机名不是真实地址) access other hosts variables例如:
{{hostvars[inventory_hostname]['ansible_facts']['ansible_fqdn']}}
答案 1 :(得分:0)
如果您想复制一些东西
- name: Copy custom config from serverA serverB over
synchronize:
src: "/path/to/source/on/serverA/machine"
dest: "rsync://{{ hostvars[serverB]['ansible_facts']['ansible_eth0']['ipv4']['address'] }} }}/path/where/to/put/files/on/serverB/machines"
delegate_to: serverA