cancel
Showing results for 
Search instead for 
Did you mean: 
morbius
Flight Engineer
Flight Engineer
  • 1,522 Views

Lookup can't lookup anything

Jump to solution

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.

Labels (1)
2 Solutions

Accepted Solutions
ConstantinB
Mission Specialist
Mission Specialist
  • 1,385 Views

@morbius 

Here is what I get using your code:

Screenshot_2024-01-03_16-05-43.png

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.

 

View solution in original post

ConstantinB
Mission Specialist
Mission Specialist
  • 1,305 Views

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.  

View solution in original post

9 Replies
morbius
Flight Engineer
Flight Engineer
  • 1,500 Views

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.

Chetan_Tiwary_
Community Manager
Community Manager
  • 1,424 Views

@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 :

Chetan_Tiwary__0-1704294484176.pngChetan_Tiwary__1-1704294502174.png

Chetan_Tiwary__2-1704294525242.png

Were you able to replicate the issue in any course chapter / section - DO374 ,DO467 or RH294 ?

0 Kudos
ConstantinB
Mission Specialist
Mission Specialist
  • 1,414 Views

@morbius 

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!

0 Kudos
morbius
Flight Engineer
Flight Engineer
  • 1,395 Views

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.

Screenshot from 2024-01-03 22-23-10.png

0 Kudos
morbius
Flight Engineer
Flight Engineer
  • 1,395 Views

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.

Screenshot from 2024-01-03 22-33-50.png

0 Kudos
ConstantinB
Mission Specialist
Mission Specialist
  • 1,386 Views

@morbius 

Here is what I get using your code:

Screenshot_2024-01-03_16-05-43.png

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.

 

ConstantinB
Mission Specialist
Mission Specialist
  • 1,306 Views

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
Flight Engineer
Flight Engineer
  • 1,255 Views

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.

0 Kudos
Join the discussion
You must log in to join this conversation.