Ansible playbook - regexp | unable to change data in brackets and quotes - ansible

I've been trying to finish up a playbook for deploying a new server. I'm struggling with changing data within brackets containing quotes via lineinfile and a regex:
- name: "Configuring: filebeat agent - configuring output to logstash"
lineinfile:
dest: "/etc/filebeat/filebeat.yml"
regexp: '#hosts: ["localhost:5044"]'
line: 'hosts: ["elk.home:5044"]'
tags: application
After the playbook is executed, the desired line:
#hosts: ["localhost:5044"]
is not updated to reflect:
hosts: ["elk.home:5044"]
What I'm trying to achieve is:
#hosts: ["localhost:5044"] is replaced with hosts: ["elk.home:5044"]
There are no errors generated. I've tried varying " and ' along with escapes \, but I can't get the expression correct. Any suggestions would be greatly appreciated!

Thanks seshadri_c and β.εηοιτ.βε!
I was able to reach a resolution with the following lines:
- name: "Configuring: filebeat agent - enabling logstash output hosts"
lineinfile:
dest: "/etc/filebeat/filebeat.yml"
regexp: '#hosts: \["localhost:5044"\]'
line: 'hosts: ["elk.home:5044"]'
tags:
- configuration
- application
- filebeat
After completing the playbook, I had an issue with whitespace. I added two spaces that correctly modified the line
- name: "Configuring: filebeat agent - enabling logstash output hosts"
lineinfile:
dest: "/etc/filebeat/filebeat.yml"
regexp: '#hosts: \["localhost:5044"\]'
line: ' hosts: ["elk.home:5044"]'
tags:
- configuration
- application
- filebeat

Related

Ansible lineinfile is not changing file as expect

THi, new to this. . . .
Testing out a Ansible play to update the swappiness setting on a test box.
---
- hosts: "{{ target }}"
become: yes
tasks:
- name: swapness
lineinfile:
path: /etc/sysctl.conf
state: present
regexp: vm.swappiness=*
line: vm.swappiness=6
Currently, it is set to vm.swappiness=4 but running the above does not change that.
the way I read the logic is...
look for a line with "vm.swappiness=" and replace it with "vm.swappiness=6"
I would do in this way:
---
- name: Playbook who modify swapiness.
hosts: "{{ target }}"
become: yes
tasks:
# Set vm.swappiness to 6 in /etc/sysctl.conf
- name: Set swapiness.
sysctl:
name: vm.swappiness
value: '6'
state: present
sysctl_set: yes
reload: yes
The reason for that is that If exists any module for it, you should be doing using that module specific instead a generic one.
Ansible has a sysctl module, so it's better to use it for this.
Note: Please, review the code and set 2 white spaces between them if not already.
Given the file
shell> cat sysctl.conf
vm.swappiness=4
The task below works as expected
- lineinfile:
path: sysctl.conf
regexp: vm.swappiness=*
line: vm.swappiness=6
Running the playbook with the options --check --diff gives abridged
TASK [lineinfile] *****************************************************
--- before: sysctl.conf (content)
+++ after: sysctl.conf (content)
## -1 +1 ##
-vm.swappiness=4
+vm.swappiness=6
Notes
The regexp vm.swappiness=* is probably not what you really want. This regexp matches the string vm.swappiness followed by 0 or mode =. For example, below are strings that match this regexp
vm.swappiness
vm.swappiness=
vm.swappiness==========
Instead, you want a regexp that matches the string vm.swappiness= followed by 0 or more characters. For example vm.swappiness=.*

Ansible replace text in file

So ive been tasked with replaceing zabbix server. To do so i have to modify zabbix_agent file in all server and there are many. Tho in this job is the first time i see ansible so i need some help. And i am using ansible-playbook.
In zabbix_agentd.conf file there is the old zabbix conf:
HostMetadata=Linux
PidFile=/var/run/zabbix/zabbx_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=zabbix.company.com
ServerActive=zabbix.company.com
HostnameItem=system.hostname
Include=/etc/zabbix_agentd.d/
Now i need to replace "Server" and "ServerActive" to "zabbix2.company.com"
I have tried various codes from this page to work for my needs but so far it has failed. No clue what im doing wrong
Try this one
- lineinfile:
path: /etc/zabbix_agentd.conf
regexp: '^\s*{{ key }}\s*=(.*)$'
line: '{{ key }}={{ value }}'
notify: reload zabix
loop:
- {key: 'Server', value: 'zabbix2.company.com'}
- {key: 'ServerActive', value: 'zabbix2.company.com'}
Notes
Path is required; probably /etc/zabbix_agentd.conf ?
It is not necessary to search the white-space \s* in regexp. However, it would match and fix potential spaces in the configuration.
Create and notify a handler reload zabix when anything changed. See Handlers: Running Operations On Change.
Take a look at Zabix modules.
I have manged to solve this issue using this code.
---
tasks:
- name: 'replace line'
lineinfile:
dest: /etc/zabbix/zabbix_agentd.conf
regexp: '^(.*)Server=zabbix.company.com(.*)$'
line: 'Server=zabbix2.company.com'
backrefs: yes

Ansible Playbook keeps adding ^M to my files

I have the below ansible playbook where i am identifying a string in a file and replacing it with another
---
- name: Ansible Playbook
hosts: webserver1
remote_user: user45
tasks:
- name: Replacing content with other
lineinfile:
path: /home/user45/run.sh
regexp: '^(.*)DEBUG=(.*)$'
line: 'DEBUG=ON'
the above works, but it adds ^M to the end of every other line in that file and every blank line
From what I have read online this normally occurs when you copy and paste from Windows to Linux but i typed this out manually so i am kind of stumped
Playbook is running on Linux Redhat 7
Please verify your script "/home/user45/run.sh".Looks like carriage-return character exist in there.
For whatever reason lineinfile was adding the ^M .. If I change it to use replace module it does not add the ^M bits
---
- name: Ansible Playbook
hosts: webserver1
remote_user: user45
tasks:
- name: Replacing content with other
replace:
dest: /home/user45/run.sh
regexp: 'DEBUG=.*'
line: 'DEBUG=ON'
This problem has been fixed with this solution on ansible 2.9:
---
- name: Ansible Playbook
hosts: webserver1
remote_user: user45
tasks:
- name: Replacing content with other
replace:
path: /home/user45/run.sh
regexp: 'DEBUG=.*'
replace: 'DEBUG=ON'
Enjoy ;)

Ansible: Limit a task in a playbook to localhost

I want to restrict an Ansible play to a specific host
Here's a cut down version of what I want:
- hosts some_host_group
tasks:
- name: Remove existing server files
hosts: 127.0.0.1
file:
dest: /tmp/test_file
state: present
- name: DO some other stuff
file:
...
I want to (as an early task), remove a local directory (I've created a file in the example as it's a more easily observed test). I was under the impression that I could limit a play to a set of hosts with the "hosts" parameter to the task -
but I get this error:
ERROR! 'hosts' is not a valid attribute for a Task
$ansible --version
ansible 2.3.1.0
Thanks.
PS I could wrap the ansible in a shell fragment, but that's ugly.
You should use delegate_to or local_action and tell Ansible to run the task only once (otherwise it will try to delete the directory as many times as target hosts in your play, although it won't be a problem).
You should also use absent not present if you want to remove directory, as you stated.
- name: Remove existing server files
delegate_to: 127.0.0.1
run_once: true
file:
dest: /tmp/test_file
state: absent
There are syntax errors in your playbook, have a look at Ansible Intro, Local Playbooks and Delegation.
- hosts: some_host_group
tasks:
- name: Remove existing server files
- hosts: localhost
tasks:
- file:
dest: /tmp/test_file
state: present
- name: DO some other stuff
file:

How to add new line in ansible playbook

I am trying to add new line in yaml configuration file through ansible playbook using lineinfile. What I have tried till now.
#attempt-1
- name: "Configure Node River"
lineinfile: "dest=/path/to/config.conf line='node.river: river_name'"
#attempt-2
- name: "Configure Node River"
lineinfile: "dest=/path/to/config.conf state=present regexp='^' line='node.river: river_name'"
Also I have tried as per reference document.
# Add a line to a file if it does not exist, without passing regexp
- lineinfile: dest=/tmp/testfile line="192.168.1.99 foo.lab.net foo"
Can anyone guide me how to add new line which is not present in file. I know I can add End of the File by adding regexp='' insertafter=EOF in my #attempt-1. But I want to add above line in middle of the file.
As I understand you can use insertafter=<regexp>:
--- play.yml
- hosts: localhost
gather_facts: False
tasks:
- name: "Configure Node River"
local_action: 'lineinfile dest=/home/ig/test.conf
line="node.river: river_name" insertafter="first line"'
test.conf before:
first line
second line
test conf after:
first line
node.river: river_name
second line

Resources