# dynamically set info in hosts file

**URL:** <https://forum.ansible.com/t/dynamically-set-info-in-hosts-file/38573>\
**Category:** Ansible Project\
**Created:** [November 7, 2023, 4:15pm UTC](https://forum.ansible.com/t/dynamically-set-info-in-hosts-file/38573 "2023-11-07T16:15:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [November 7, 2023, 4:15pm UTC](https://forum.ansible.com/t/dynamically-set-info-in-hosts-file/38573/1 "2023-11-07T16:15:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 7, 2023, 4:45pm UTC](https://forum.ansible.com/t/dynamically-set-info-in-hosts-file/38573/2 "2023-11-07T16:45:30Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [November 7, 2023, 5:45pm UTC](https://forum.ansible.com/t/dynamically-set-info-in-hosts-file/38573/3 "2023-11-07T17:45:30Z")

</div>

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!
