Ansible 模块用于查找新主机名并更新配置文件

Ansible 模块用于查找新主机名并更新配置文件

你能帮忙修复这个问题吗?我正在尝试将主机名写入文件。

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

给定远程主机和文件

$ hostname -f
test_01.example.org

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

剧本完成了任务

- 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"

给出

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

正则表达式的解释
'^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

相关内容