ansible - Ansible 查找模块给出错误“似乎不是有效目录或无法访问”绝对路径
问题描述
Ansible 查找模块未按预期工作。
所以我有三个实例一个是测试节点,第二个控制器节点,第三个是我运行我的 ansible 剧本的地方
我正在尝试在 test_nodes 上生成 ssh-keys,然后从这些节点中获取公钥。这工作正常。
然后我试图将这些公钥附加到不同主机(controller_node)的authorized_keys文件中。为此,我使用 find 模块来获取文件列表,然后在 authorized_key 模块中遍历这些文件。
我正在使用:
- name: Set authorized key file taken from file
authorized_key:
user: absrivastava
key: "{{ lookup('file','item') }}"
state: present
#with_file:
- "/home/absrivastava/ANSIBLE/ssh-keys/*/home/ribbon/.ssh/id_rsa.pub" This didnt work
#with_filetree:
- "/home/absrivastava/ANSIBLE/ssh-keys/*/home/ribbon/.ssh/id_rsa.pub" This was not appending data
但它似乎没有工作。所以我使用 find 来获取文件列表,然后遍历它们。
- name: Generate ssh keys
hosts: media_nodes
gather_facts: false
tasks:
- name: key generation
openssh_keypair:
path: ~/.ssh/id_ssh_rsa
force: True
register: public_key
- debug:
var: public_key.public_key
- name: fetch public key from all nodes
fetch:
src: ~/.ssh/id_ssh_rsa.pub
dest: ssh-keys/
- name: Controller play
hosts: controller
gather_facts: false
tasks:
- name: Find list of public key files
find:
paths: /home/abhilasha/ANSIBLE/ssh-keys/
file_type: file
recurse: yes
patterns: ".*pub"
use_regex: yes
register: files_matched
- name: debug files matched
debug:
var: files_matched.files
- name: Debug files_matched loop
debug:
var: item.path
loop: "{{ files_matched.files|flatten(levels=1) }}"
loop_control:
label: "{{ item.path }}"
- name: Set authorized key file taken from file
authorized_key:
key: "{{ lookup('file','item') }}"
state: present
with_file:
- "{{ files_matched.files }}"
- name: Find list of public key files
This play is not working giving error
TASK [Find list of public keys] *****************************************************************************************************************************************************************************************************************
ok: [test_controller] => {"changed": false, "examined": 0, "files": [], "matched": 0, "msg": "/home/abhilasha/ANSIBLE/ssh-keys/ was skipped as it does not seem to be a valid directory or it cannot be accessed\n"}
解决方案
好的,所以我遇到了问题,我正在使用 hosts: controller 进行此播放,但文件在我的测试 VM 实例上。
但我不确定如何仍然解决我的问题。我想在本地使用 publoc 密钥,然后将其附加到控制器服务器
- name: Fetch public key files from localhost
gather_facts: false
hosts: 127.0.0.1
connection: local
tasks:
- name: Find list of public keys
find:
paths: ssh-keys/
file_type: file
recurse: yes
patterns: "pub"
use_regex: yes
hidden: yes
register: files_matched
- name: Debug files_matched loop
debug:
var: item.path
loop: "{{ files_matched.files|flatten(levels=1) }}"
loop_control:
label: "{{ item.path }}"
- name: Add Public keys to controller authorized keys
hosts: controller
gather_facts: false
tasks:
- name: Set authorized key file taken from file
authorized_key:
key: "{{ lookup('file','item') }}"
state: present
with_file:
- "{{ files_matched.files }}"
我无法在该播放范围之外使用 files_matched 变量。我怎样才能使这项工作。提前致谢
推荐阅读
- mysql - 'select * from ALargeTable' 在 MySQL 中执行非常慢
- sqldelight - 不是从迁移中生成的查询
- python-3.x - 如何在 Sagemaker 2 中使用序列化器和反序列化器
- c++ - 当派生对象声明没有新成员时是否会发生多态切片
- php - 创建 Laravel 应用程序即使在锁定屏幕上也能正常工作
- mongodb - mgo.v2 不在结构中解组嵌套映射,而是设置 bson.M
- node.js - React 和 Node 简单的非对称加密
- python - 在 tkinter 中弹出视频流窗口
- c# - 如何从文本框获取字符串到我的视图模型
- json - JSON数据-在Python中如何仅打印Key =可用的结果