ansible lineinfile 根据输入数组替换多个文件中包含默认端口的模式的行开始
ansible lineinfile replace a line begin containing a pattern with default ports in multiple files based on input array
---
-
gather_facts: true
hosts: abc,pqr,xyz
name: "playbook"
serial: 1
tasks:
-
block:
-
shell: "ls /etc/maria/" # could be n number of ports like 123, 456, 789
register: ports
-
lineinfile:
line: PORT={{ new_ports }}
path: "/etc/instance_{{ item }}/mariadb.conf"
regexp: '^PORT='
with_items: "{{ ports.stdout_lines }}"
new_ports: # length of this would be based on number of ports, should create an array based on array size of `ports`
- 8880
- 8881
- 8882
举例来说,我有一些来自 ls /etc/maria/
123
456
789
的 3 个端口
和3个对应的文件
/etc/instance_123/mariadb.conf
,
/etc/instance_456/mariadb.conf
、
/etc/instance_789/mariadb.conf
。
可能是 n 个端口和文件。
我想将 /etc/instance_123/mariadb.conf
中以 ^PORT= 开头的行替换为 PORT=8880 等等,直到数组端口的长度。
使用建议代码获得的结果:
之前
/etc/instance_1234/mariadb.conf | grep port
port=1234
剧本
- hosts: abc
gather_facts: false
vars:
ansible_python_interpreter: /usr/bin/python3
serial: 1
tasks:
- shell: ls /etc/maria/
register: ports
- debug:
msg: "{{ ports.stdout_lines|length }}"
- lineinfile:
path: "/etc/instance_{{ item }}/mariadb.conf"
regexp: "^port=.*$"
line: "port={{ new_ports[ansible_loop.index0] }}"
loop: "{{ ports.stdout_lines }}"
loop_control:
extended: true
vars:
new_ports: "{{ range(8880, 8880 + ports.stdout_lines|length) }}"
之后
/etc/instance_1234/mariadb.conf | grep port
port=x
例如,给定文件
shell> ssh admin@test_11 ls /tmp/mongo
123
456
shell> ssh admin@test_12 ls /tmp/mongo
123
456
789
剧本
- hosts: test_11,test_12
gather_facts: false
serial: 1
tasks:
- command: ls /tmp/mongo
register: ports
- lineinfile:
path: "/tmp/etc/instance_{{ item }}/mongodb.conf"
regexp: "^PORT=.*$"
line: "PORT={{ new_ports[ansible_loop.index0] }}"
create: true
loop: "{{ ports.stdout_lines }}"
loop_control:
extended: true
vars:
new_ports: "{{ range(8880, 8880 + ports.stdout_lines|length) }}"
创建或更新配置文件
shell> ssh admin@test_11 ls /tmp/etc/
instance_123
instance_456
shell> ssh admin@test_11 cat /tmp/etc/instance_123/mongodb.conf
PORT=8880
shell> ssh admin@test_11 cat /tmp/etc/instance_456/mongodb.conf
PORT=8881
shell> ssh admin@test_12 ls /tmp/etc/
instance_123
instance_456
instance_789
shell> ssh admin@test_12 cat /tmp/etc/instance_123/mongodb.conf
PORT=8880
shell> ssh admin@test_12 cat /tmp/etc/instance_456/mongodb.conf
PORT=8881
shell> ssh admin@test_12 cat /tmp/etc/instance_789/mongodb.conf
PORT=8882
---
-
gather_facts: true
hosts: abc,pqr,xyz
name: "playbook"
serial: 1
tasks:
-
block:
-
shell: "ls /etc/maria/" # could be n number of ports like 123, 456, 789
register: ports
-
lineinfile:
line: PORT={{ new_ports }}
path: "/etc/instance_{{ item }}/mariadb.conf"
regexp: '^PORT='
with_items: "{{ ports.stdout_lines }}"
new_ports: # length of this would be based on number of ports, should create an array based on array size of `ports`
- 8880
- 8881
- 8882
举例来说,我有一些来自 ls /etc/maria/
123
456
789
的 3 个端口
和3个对应的文件/etc/instance_123/mariadb.conf
,
/etc/instance_456/mariadb.conf
、
/etc/instance_789/mariadb.conf
。
可能是 n 个端口和文件。
我想将 /etc/instance_123/mariadb.conf
中以 ^PORT= 开头的行替换为 PORT=8880 等等,直到数组端口的长度。
使用建议代码获得的结果:
之前
/etc/instance_1234/mariadb.conf | grep port
port=1234
剧本
- hosts: abc
gather_facts: false
vars:
ansible_python_interpreter: /usr/bin/python3
serial: 1
tasks:
- shell: ls /etc/maria/
register: ports
- debug:
msg: "{{ ports.stdout_lines|length }}"
- lineinfile:
path: "/etc/instance_{{ item }}/mariadb.conf"
regexp: "^port=.*$"
line: "port={{ new_ports[ansible_loop.index0] }}"
loop: "{{ ports.stdout_lines }}"
loop_control:
extended: true
vars:
new_ports: "{{ range(8880, 8880 + ports.stdout_lines|length) }}"
之后
/etc/instance_1234/mariadb.conf | grep port
port=x
例如,给定文件
shell> ssh admin@test_11 ls /tmp/mongo
123
456
shell> ssh admin@test_12 ls /tmp/mongo
123
456
789
剧本
- hosts: test_11,test_12
gather_facts: false
serial: 1
tasks:
- command: ls /tmp/mongo
register: ports
- lineinfile:
path: "/tmp/etc/instance_{{ item }}/mongodb.conf"
regexp: "^PORT=.*$"
line: "PORT={{ new_ports[ansible_loop.index0] }}"
create: true
loop: "{{ ports.stdout_lines }}"
loop_control:
extended: true
vars:
new_ports: "{{ range(8880, 8880 + ports.stdout_lines|length) }}"
创建或更新配置文件
shell> ssh admin@test_11 ls /tmp/etc/
instance_123
instance_456
shell> ssh admin@test_11 cat /tmp/etc/instance_123/mongodb.conf
PORT=8880
shell> ssh admin@test_11 cat /tmp/etc/instance_456/mongodb.conf
PORT=8881
shell> ssh admin@test_12 ls /tmp/etc/
instance_123
instance_456
instance_789
shell> ssh admin@test_12 cat /tmp/etc/instance_123/mongodb.conf
PORT=8880
shell> ssh admin@test_12 cat /tmp/etc/instance_456/mongodb.conf
PORT=8881
shell> ssh admin@test_12 cat /tmp/etc/instance_789/mongodb.conf
PORT=8882