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”

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