Ansible find模块未按预期工作。
我有三个实例,一个是测试节点,第二个是控制器节点,第三个是我运行ansible攻略的地方
我尝试在test_nodes上生成ssh密钥,然后从这些节点获取公钥。这工作得很好。
然后,我尝试将这些公钥附加到另一个主机(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"}发布于 2019-09-11 14:34:00
好的,我得到了问题所在,我在这个游戏中使用hosts: controller,但是文件在我的测试VM实例上。
但我不确定如何仍能解决我的问题。我希望在本地使用公共密钥,然后将其附加到控制器服务器
- 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变量。我怎么才能让这件事起作用。提前感谢
发布于 2019-09-10 18:42:11
Q:"msg":“/home/abhilasha/ANSIBLE/ssh-key/被跳过,因为它似乎不是有效的目录或无法访问\n”
答:查看ssh-keys/ at控制器目录并检查其内容。而不是
paths: /home/abhilasha/ANSIBLE/ssh-keys/在同一路径中找到它
path: ssh-keys/它已被提取到
dest: ssh-keys/发布于 2019-09-10 21:03:14
你能按如下所示更改路径并尝试
- name: fetch public key from all nodes
fetch:
src: ~/.ssh/id_ssh_rsa.pub
dest: /tmp/ssh-keys/- name: Find list of public key files
find:
paths: /tmp/ssh-keys/
file_type: file
recurse: yes
patterns: ".*pub"
use_regex: yes
register: files_matchedhttps://stackoverflow.com/questions/57868717
复制相似问题