Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Created October 1, 2020 11:31
Show Gist options
  • Save zeitounator/184acbbfca8e2b22bfc5b4b680b6b5c9 to your computer and use it in GitHub Desktop.
Save zeitounator/184acbbfca8e2b22bfc5b4b680b6b5c9 to your computer and use it in GitHub Desktop.
---
- hosts: localhost
gather_facts: false
vars:
test_file_path: /tmp/test_config_file.txt
original_file_content: |-
hite-config {
enable = false
host-name = "host1"
port = 201
batch-size = 100
patterns = ["example"]
type = LINE
prefix = "stats."${test}".service"
}
some-config1 {
enable = false
send-enable = false
name = "lector"
host-name = ${test}
connection-timeout = 120000
read-timeout = 60000
}
lorem-ipsum-config {
enable = false
send-enable = false
name = "lector"
host-name = ${test}
connection-timeout = 120000
read-timeout = 60000
}
dolor-sit-amet-config {
enable = false
send-enable = false
name = "lector"
host-name = ${test}
connection-timeout = 120000
read-timeout = 60000
}
tasks:
- name: Create the test file with original content
copy:
dest: "{{ test_file_path }}"
content: "{{ original_file_content }}"
- name: Show the file content before replacement
debug:
msg: "{{ lookup('file', test_file_path).split('\n') }}"
- name: Replace needed content
replace:
path: "{{ test_file_path }}"
after: 'some-config {'
before: '}'
regexp: '^(\s*enable = )false$'
replace: '\g<1>true'
- name: Show file content after replacement
debug:
msg: "{{ lookup('file', test_file_path).split('\n') }}"
PLAY [localhost] ****************************************************************************************************************************************************************************************************************************
TASK [Create the test file with original content] *******************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Show the file content before replacement] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
"hite-config {",
" enable = false",
" host-name = \"host1\"",
" port = 201",
" batch-size = 100",
" patterns = [\"example\"]",
" type = LINE",
" prefix = \"stats.\"${test}\".service\"",
"}",
"",
"some-config1 {",
" enable = false",
" send-enable = false",
" name = \"lector\"",
" host-name = ${test}",
" connection-timeout = 120000",
" read-timeout = 60000",
"}",
"",
"some-config2 {",
" enable = false",
" send-enable = false",
" name = \"lector\"",
" host-name = ${test}",
" connection-timeout = 120000",
" read-timeout = 60000",
"}",
"",
"lorem-ipsum-config {",
" enable = false",
" send-enable = false",
" name = \"lector\"",
" host-name = ${test}",
" connection-timeout = 120000",
" read-timeout = 60000",
"}",
"",
"dolor-sit-amet-config {",
" enable = false",
" send-enable = false",
" name = \"lector\"",
" host-name = ${test}",
" connection-timeout = 120000",
" read-timeout = 60000",
"}"
]
}
TASK [Replace needed content] ***************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Show file content after replacement] **************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
"hite-config {",
" enable = false",
" host-name = \"host1\"",
" port = 201",
" batch-size = 100",
" patterns = [\"example\"]",
" type = LINE",
" prefix = \"stats.\"${test}\".service\"",
"}",
"",
"some-config1 {",
" enable = false",
" send-enable = false",
" name = \"lector\"",
" host-name = ${test}",
" connection-timeout = 120000",
" read-timeout = 60000",
"}",
"",
"some-config2 {",
" enable = true",
" send-enable = false",
" name = \"lector\"",
" host-name = ${test}",
" connection-timeout = 120000",
" read-timeout = 60000",
"}",
"",
"lorem-ipsum-config {",
" enable = false",
" send-enable = false",
" name = \"lector\"",
" host-name = ${test}",
" connection-timeout = 120000",
" read-timeout = 60000",
"}",
"",
"dolor-sit-amet-config {",
" enable = false",
" send-enable = false",
" name = \"lector\"",
" host-name = ${test}",
" connection-timeout = 120000",
" read-timeout = 60000",
"}"
]
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment