I noticed this in RH294 lab as I was experimenting with authorized_key module. Common way to copy user's public key to a remote host would be like this:
ansible.posix.authorized_key: user: charlie key: "{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"
However, I would always get an error message that the file can't be found. At first I thought that it's a permissions problem, but after a while I realized that lookup('file' can't open any file I ask it to. Could others confirm it's the same for them, this looks like a bug, but I'm still not 100% sure I'm not doing something wrong.
Here is what I get using your code:
I don't have navigator installed to fully mimic your approach. But I believe this proofs than lookup is functional and is working as expected.
Please note that 10.11.8.111 is the IP of the "control" node. So the playbook is running locally. Basically It is the same as having "hosts: localhost" in the playbook.
To run it with navigator, if the file locatation is outside of folder where ansible-navigator started, the location of the file needs to be accessible from the navigator container.
There are different ways to do it:
1: add a parameter:
ansible-navigator run test.yml -m stdout --eev /home/student/.ssh:/home/student/.ssh:Z
2: add volume-mounts to ansible-navigator config:
# volume-mounts:
# - src: "home/student/.ssh"
# dest: "home/student/.ssh"
# options: "Z"
3: But IMHO the simplest solution would be moving the file inside the folder where ansible-navigator is started.
@morbius Lookups work on the local machine and not on the remote target hosts : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_lookup.html
use slurp : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/slurp_module.html
I know that, I was talking about local file content copying, it doesn't work. It always says it can't find the file. I need others to try to confirm it's the same for them.
@morbius please elaborate more on the playbook, the path and the error screenshot so that one can check more on this.
Does this look helpful to you :
Were you able to replicate the issue in any course chapter / section - DO374 ,DO467 or RH294 ?
I just tested your code in my lab (All hosts are RHEL9) and it works as expected (I mean the key is added to the authorized_keys).
I would suggest to check if the user "charlie" exists on the target system and if the public key file "/home/charlie/.ssh/id_rsa.pub" exists on the control node.
Also check if the "ansible.posix" collection is installed on the control node where you are trying to run it.
If all the conditions above are satisfied try to run the playbook with -v (or -vv ) to increace the output verbosity.
Good luck!
To simplify the example to the very bones, I wrote this playbook:
- name: Trying lookup
hosts: servera
tasks:
- name: Read a file
ansible.builtin.debug:
msg: "This should print bar {{ lookup('ansible.builtin.file', '/home/student/foo') }}"
So, I tried to read a file that student user has created and has access to. The result is below. It's the same if I change the host to workstation. Trying to increase verbosity tells me nothing useful. This is the default RH294 v9 environment, without trying to start some excercise. I do not understand why it's different for you.
Pardon, I do actually get more info when I crank up verbosity to -vvvvv. I didn't even know it goes that far, I thought it stops at four vs.
Here is what I get using your code:
I don't have navigator installed to fully mimic your approach. But I believe this proofs than lookup is functional and is working as expected.
Please note that 10.11.8.111 is the IP of the "control" node. So the playbook is running locally. Basically It is the same as having "hosts: localhost" in the playbook.
To run it with navigator, if the file locatation is outside of folder where ansible-navigator started, the location of the file needs to be accessible from the navigator container.
There are different ways to do it:
1: add a parameter:
ansible-navigator run test.yml -m stdout --eev /home/student/.ssh:/home/student/.ssh:Z
2: add volume-mounts to ansible-navigator config:
# volume-mounts:
# - src: "home/student/.ssh"
# dest: "home/student/.ssh"
# options: "Z"
3: But IMHO the simplest solution would be moving the file inside the folder where ansible-navigator is started.
Yes, you are right! I did not expect this behavior from ansible-navigator and it was not mentioned in the RH294 training material. When I move file I want to read to the project's directory, lookup works like a charm.
This leaves me a bit worried what other unexpected behavior I might run into, hopefully not at the exam. I also hope that grading would not be affected if I have to copy files to the working directory like that.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.