ansible本地代理执行
在需要向远端主机传送文件,但远端主机没有python或没有rsync时,可以使用本地代理执行,使用scp来上传文件。
- name: upload file to remote hosts
hosts: all
gather_facts: no
# 当远端主机没有rsync时,使用scp上传文件
tasks:
- name: 上传文件夹或文件到服务器
local_action: |
ansible.builtin.command sshpass -p {{ ansible_password }} scp /mnt/d/mjxtPackage/front-install/etc_data/ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm root@{{ ansible_host }}:/etc_data/
上面是在本地代理的一个快捷方法,其正常模式是使用delegate_to参数设置代理执行的机器:
- name: upload file to remote hosts
hosts: all
gather_facts: no
# 当远端主机没有rsync时,使用scp上传文件
tasks:
- name: 上传文件夹或文件到服务器
ansible.builtin.command: |
ansible.builtin.command sshpass -p {{ ansible_password }} scp /mnt/d/mjxtPackage/front-install/etc_data/ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm root@{{ ansible_host }}:/etc_data/
delegate_to: 127.0.0.1
由于我是在inventory中存放密码,所以在调用时使用{{ ansible_password }}来调用当前被控主机的密码,用于scp连接。
all:
vars:
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
ansible_executable: '/bin/bash'
g00000000000001:
hosts:
ipc01:
ansible_host: 1.2.3.4
ansible_user: root
ansible_password: xxabc23@cf