首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible find模块给出错误“似乎不是有效的目录或无法访问”绝对路径

Ansible find模块给出错误“似乎不是有效的目录或无法访问”绝对路径
EN

Stack Overflow用户
提问于 2019-09-10 18:20:25
回答 3查看 1.7K关注 0票数 1

Ansible find模块未按预期工作。

我有三个实例,一个是测试节点,第二个是控制器节点,第三个是我运行ansible攻略的地方

我尝试在test_nodes上生成ssh密钥,然后从这些节点获取公钥。这工作得很好。

然后,我尝试将这些公钥附加到另一个主机(Controller_node)的authorized_keys文件中。为此,我使用find模块来获取文件列表,然后在authorized_key模块中循环遍历这些文件。

我使用的是:

代码语言:javascript
复制
- 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来获取文件列表,然后遍历它们。

代码语言:javascript
复制
- 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"}
EN

回答 3

Stack Overflow用户

发布于 2019-09-11 14:34:00

好的,我得到了问题所在,我在这个游戏中使用hosts: controller,但是文件在我的测试VM实例上。

但我不确定如何仍能解决我的问题。我希望在本地使用公共密钥,然后将其附加到控制器服务器

代码语言:javascript
复制
- 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变量。我怎么才能让这件事起作用。提前感谢

票数 1
EN

Stack Overflow用户

发布于 2019-09-10 18:42:11

Q:"msg":“/home/abhilasha/ANSIBLE/ssh-key/被跳过,因为它似乎不是有效的目录或无法访问\n”

答:查看ssh-keys/ at控制器目录并检查其内容。而不是

代码语言:javascript
复制
paths: /home/abhilasha/ANSIBLE/ssh-keys/

在同一路径中找到它

代码语言:javascript
复制
path:  ssh-keys/

它已被提取到

代码语言:javascript
复制
dest:  ssh-keys/
票数 0
EN

Stack Overflow用户

发布于 2019-09-10 21:03:14

你能按如下所示更改路径并尝试

代码语言:javascript
复制
- name: fetch public key from all nodes
  fetch:
     src: ~/.ssh/id_ssh_rsa.pub
     dest:  /tmp/ssh-keys/
代码语言:javascript
复制
- name: Find list of public key files
  find:
     paths: /tmp/ssh-keys/
     file_type: file
     recurse: yes
     patterns: ".*pub"
     use_regex: yes
  register: files_matched
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57868717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档