1

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 {{ '}}' }}"
  tasks:
    - name: Update hostname config file
      block:
        - 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:
https://serv1.app/key/auth/mvn/vars/lenz/svc/chk”
Gabby Seyi
  • 15
  • 4
  • I need the server hostname to replace the hostname in the config.tml file. Here is what the file looks like: uri: https://server.*old_hostname*/ask/params/class I want the new hostname to replace *old_hostname* – Gabby Seyi Oct 15 '20 at 18:40
  • See [mcve](https://stackoverflow.com/help/minimal-reproducible-example) and post complete minimal details to reproduce the problem. – Vladimir Botka Oct 15 '20 at 20:45

1 Answers1

0

Given the remote host and the file

$ hostname -f
test_01.example.org

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

The playbook does the job

- hosts: test_01
  tasks:
    - 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"

gives

$ 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
Vladimir Botka
  • 3,791
  • 6
  • 17
  • You can skip the `command` too, as the FQDN is already in the ansible fact `ansible_fqdn`. – Michael Hampton Oct 15 '20 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 '20 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. – Vladimir Botka Oct 15 '20 at 20:43
  • Thank you very much Vladimir, I really appreciate your help. – Gabby Seyi Oct 15 '20 at 20:51
  • HI Vladimir, what it the line is uri: http://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 '20 at 21:33
  • Thanks for the explanation. I appreciate it – Gabby Seyi Oct 15 '20 at 21:42
  • It's Python regex. Use [pythex](https://pythex.org/), create and test your regex. The module *lineinfile* is idempotent if the *regexp* matches both the origin and changed lines. – Vladimir Botka Oct 15 '20 at 21:42