【发布时间】:2020-07-17 10:40:10
【问题描述】:
我正在尝试使用 Ansible 2.8.4 和 awx 执行以下操作:
- 从 Cisco IOS 设备中阅读一些事实(有效)
- 使用模板将结果放入本地文件(工作)
- 将生成的文件复制/移动到其他服务器
由于我必须使用不同的用户来访问 IOS 设备和服务器,而且这些服务器不是用于剧本的清单的一部分,因此我尝试使用 become_user 和 delegate_to 来实现这一点。 初始用户(在 awx 模板中定义)被允许连接到 IOS 设备,而 different_user 可以使用 ssh 私钥连接到服务器。
剧本:
---
- name: Read Switch Infos
hosts: all
gather_facts: no
tasks:
- name: Gather IOS Facts
ios_facts:
- debug: var=ansible_net_version
- name: Set Facts IOS
set_fact:
ios_version: "{{ ansible_net_version }}"
- name: Create Output file
file: path=/tmp/test state=directory mode=0755
delegate_to: 127.0.0.1
run_once: true
- name: Run Template
template:
src: ios_firmware_check.j2
dest: /tmp/test/output.txt
delegate_to: 127.0.0.1
run_once: true
- name: Set up keys
become: yes
become_method: su
become_user: different_user
authorized_key:
user: different_user
state: present
key: "{{ lookup('file', '/home/different_user/.ssh/key_file') }}"
delegate_to: 127.0.0.1
run_once: true
- name: Copy to remote server
remote_user: different_user
copy:
src: /tmp/test/output.txt
dest: /tmp/test/output.txt
delegate_to: remote.server.fqdn
run_once: true
运行时,playbook 在尝试使用 ssh 密钥访问主目录的设置密钥任务中失败:
TASK [Set up keys] *************************************************************
task path: /tmp/awx_2206_mz90qvh9/project/IOS/ios_version.yml:23
[WARNING]: Unable to find '/home/different_user/.ssh/key_file' in expected paths
(use -vvvvv to see paths)
File lookup using None as file
fatal: [host]: FAILED! => {
"msg": "An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /home/different_user/.ssh/key_file"
}
我假设我的错误与哪个用户试图访问哪个设备上的 /home/ 目录有关。 有没有更好/更优雅/有效的方式来使用 ssh 键连接到不同的服务器来移动文件? 我知道一种可能性是只使用 shell 模块进行 scp,但这总是感觉有点 hacky。
【问题讨论】:
标签: ansible ansible-awx