I'm trying to insert a line at the end of my .profile using ansible. It should add to the end if the line is absent. I'm trying the following, but it does'nt do anything:
- name: update profile
lineinfile:
dest: ~/.profile
regexp: 'PATH=$PATH:$HOME/.local/bin'
state: absent
insertafter: EOF
line: 'PATH=$PATH:$HOME/.local/bin'
Thanks in advance for any suggestions
Use state: present if you want line to exist, and not sure why you want to use regexp in this case.
Also EOF is the default value for insertafter, so no need to define it.
- name: update profile
lineinfile:
dest: ~/.profile
state: present
line: 'PATH=$PATH:$HOME/.local/bin'
Related
I'm trying to edit the value of existing line without overriding it, when I run the playbook it adds the value in a new line
Line: server=foo
Value I'm trying to add: boo
Exception:
Line: server=foo,boo
Result:
Line: server=foo,
boo
Code:
- name: Update
lineinfile:
path: "{{ file_path }}"
backrefs: yes
regexp: '(server=.*)'
line: '\1,boo'
Resolved, since I run on WSL I had to do dos2unix and after edits unix2dos
and no extra new line added
The code:
- name: Mark ending
delegate_to: localhost
lineinfile:
create: yes
insertafter: 'EOF'
path: '{{ output_file }}'
line: '=================='
Ansible version 2.8.6
This line is only added the first time the command is executed. I would expect it to add it every time.
The documentation recommends insertafter: 'EOF' for inserting lines at the end of a file, so that's what I'm using.
But now it seems to ignore every insert of this after the first.
More is inserted in between. I'm just using this command to indicate a particular thing has passed.
Setting validate: '' Changes nothing. How do I fix this?
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
I'm using lineinfile as follows:
lineinfile dest=./hosts_exp insertafter='\[hosts1\]' line="xxxxxxxxx" state=present
My hosts_exp is as follows:
[local]
localhost
[hosts1]
[hosts2]
[hosts3]
lineinfile inserts the text after [hosts3] instead of inserting it after [hosts1].
use:
lineinfile:
dest: "./hosts_exp"
line: "xxxxxxxxx"
insertafter: '^\[hosts1\]'
state: present
It appears redundant, but you need to specify the regex too:
lineinfile:
dest: ./hosts_exp
insertafter: '\[hosts1\]'
regexp: '\[hosts1\]'
line: "xxxxxxxxx"
state=present
Why? The regexp says "look for this line". The insertafter says "inject the line here".
I tested this; here's the commit. There are a few minor changes in my commit from the line above, use as necessary.
example:
- name: "blah"
lineinfile:
dest: "/test.sh"
insertafter: 'test text'
line: "text add"
state: present
Use backrefs: yes
lineinfile:
backrefs: yes
dest: ./hosts_exp
insertafter: '\[hosts1\]'
regexp: '\[hosts1\]'
line: "xxxxxxxxx"
state=present
This flag changes the operation of the module slightly; insertbefore and insertafter will be ignored, and if the regexp doesn't match anywhere in the file, the file will be left unchanged. If the regexp does match, the last matching line will be replaced by the expanded line parameter.
I would expect this to be pretty simple. I'm using the lineinfile module like so:
- name: Update bashrc for PythonBrew for foo user
lineinfile:
dest=/home/foo/.bashrc
backup=yes
line="[[ -s ${pythonbrew.bashrc_path} ]] && source ${pythonbrew.bashrc_path}"
owner=foo
regexp='^'
state=present
insertafter=EOF
create=True
The problem I'm having is that it's replacing the last line in the file (which is fi) with my new line rather than appending the line. This produces a syntax error.
Do I have the parameters correct? I've tried setting regexp to both '^' and '' (blank). Is there another way to go about this?
I'm using Ansible 1.3.3.
The Ansible discussion group helped get me sorted out on this. The problem is the regexp parameter.
Since I only want the line appended to the file once, I need the regexp to match the line as closely as possible. This is complicated in my case by the fact that my line includes variables. But, assuming the line started [[ -s $HOME/.pythonbrew, I found the following to be sufficient:
- name: Update bashrc for PythonBrew for foo user
lineinfile:
dest: /home/foo/.bashrc
line: "[[ -s ${pythonbrew.bashrc_path} ]] && source ${pythonbrew.bashrc_path}"
regexp: "^\[\[ -s \\$HOME/\.pythonbrew"
owner: foo
state: present
insertafter: EOF
create: True
Apparently ansible has matured and now (version >2.4.0) according to the documentation, The defaults when only the line is specified will append a given line to the destination file:
- name: Update bashrc for PythonBrew for foo user
lineinfile:
dest: /home/foo/.bashrc
line: "[[ -s ${pythonbrew.bashrc_path} ]] && source {pythonbrew.bashrc_path}"
owner: foo