dynamically set info in hosts file

I am trying to dynamically set my target host info, which I can only get to through a Bastion host. Here is what I am doing:

  • name: Set up host
    lineinfile:
    path: “{{ inventory_file }}”
    regexp: ‘^ansible_ssh_common_args=-o ProxyCommand=“ssh -p BASTION_port -W %h:%p -q BASTION_user@BASTION_IP”’
    line: ‘^ansible_ssh_common_args=-o ProxyCommand=“ssh -p {{ new_ssh_port }} -W %h:%p -q {{ ansible_user }}@{{ ansible_default_ipv4.address }}”’

I get this error:
/work/hosts does not exists.

The file does exist, in that directory. I’ve tried chmod’ing /work/hosts to 777 which did not help. Can anyone see what I am doing wrong? Why is it not finding a file that is there?

You have 3 hosts involved, but you do not clarify which one you are
updating: controller, bastion or target?

I'm guessing you are using the play pointing at the target, but you
probably need to delegate the task to the controller (aka localhost).

Yes. I have my ansible code on the controller. First I set up the bastion host, then I want the variables from the bastion host file copied into the hosts file. Then I’ll set up the target host.

You’re right. I can’t believe I forgot that . Thank you!