Can you help fix this? I am trying to write the hostname into a file.

- name: Update host.
  hosts: all
  connection: local
    # Default Value
    domain: '{{ default_domain }}'
    hostname: “serv1.{{ '{{' }} domain {{ '}}' }}"
    - name: Update hostname config file
        - lineinfile:
            path: /home/test/conf.yml
            state: present
            regexp: 'authorization-uri:(.*)$'
            line: "authorization-uri: https://{{ serv1.{{ '{{' }} domain {{ '}}' }}/key/auth/mvn/vars/lenz/svc/chk”

Domain = serv1
Hostname = app2
Output should be:
  • I need the server hostname to replace the hostname in the config.tml file. Here is what the file looks like: uri: server.*old_hostname*/ask/params/class I want the new hostname to replace old_hostname
    – Gabby Seyi
    Oct 15, 2020 at 18:40
  • See mcve and post complete minimal details to reproduce the problem. Oct 15, 2020 at 20:45

1 Answer 1


Given the remote host and the file

$ hostname -f

$ cat /tmp/config.yml
# authorization-uri
uri: server.old_hostname/ask/params/class

The playbook does the job

- hosts: test_01
    - command: hostname -f
      register: result
    - lineinfile:
        dest: /tmp/config.yml
        insertafter: '^# authorization-uri(.*)$'
        regexp: '^uri: server\.(.*)/ask/params/class$'
        line: "uri: server.{{ result.stdout }}/ask/params/class"


$ cat /tmp/config.yml
# authorization-uri
uri: server.test_01.example.org/ask/params/class

Explanation of the regular expression
'^uri: server\.(.*)/ask/params/class$'
^ ................. the beginning of the line
uri: server\. ..... text; the dot must be escaped
(.*) .............. any sequence
/ask/params/class   text
$ ................. end of the line
  • You can skip the command too, as the FQDN is already in the ansible fact ansible_fqdn. Oct 15, 2020 at 19:57
  • Thank you very much Vladimir, It works, but its adding a new line. I wanted to edit that same line with the new parameter. I ran it, I get 2 lines instead of one. Could you please help look into that?
    – Gabby Seyi
    Oct 15, 2020 at 20:40
  • The example I posted is idempotent. There must be a difference in the file you use if you see a new line added instead of the existing one replaced. Oct 15, 2020 at 20:43
  • Thank you very much Vladimir, I really appreciate your help.
    – Gabby Seyi
    Oct 15, 2020 at 20:51
  • HI Vladimir, what it the line is uri: hostname/lock/auth/real/realms/ip/lookup/auth instead of uri: server.old_hostname/ask/params/class. That is where the problem is coming from. thanks
    – Gabby Seyi
    Oct 15, 2020 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.