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”
給定遠端主機和文件
$ 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