I’ve the following yaml file, in which I want to substitute %IMG_NAME% with a shell variable which I have already defined, e.g. $IMG_NAME.
This is the input file
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
spec:
replicas: 1
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: %IMG_NAME%. //this the value to update
imagePullPolicy: Always
ports:
- containerPort: 5000
resources:
limits:
memory: 50Mi
requests:
memory: 25Mi
I try to do the following which doesn’t work, any idea ?
sed -e ’s,myimage,’$IMG_NAME',g' < deployment.yaml
I have no clue why you write update-dep: in your attemp. What is that?
Anyway, maybe you just want to do this:
sed 's/%IMG_NAME%/'"$IMG_NAME"'/g' deployment.yaml
where
double quoting $IMG_NAME is just a good habit,
-e is not necessary,
using , instead of / is unnecessary too, as pointed out on a comment
Not specific to sed, it is very important to know the following:
the symbol ' is a single quote, not the right single quotation mark that you put in your question (’).
if you have an actual file you want to pass to a unix program (e.g. sed, awk, and many others) you don't need to pass it like this, program < file, becuase many of these programs accept a file name as a command line argument, so you can do program file. Doing so, the program is aware of the file name (it isn't when you do program < file) and it is also aware it is a file in the first place (after < you could put something which is not a file). If the program is not aware it's working on a file (or it's not aware of what the filename is), options like sed's -i option cannot be used, because the program has no clue where it should overwrite.
Related
I'm new to shell script.
I have a YAML file that consists of placeholder . I need to replace all the occurrences of placeholder <test-name> with a value sweta
script.sh
Name="sweta"
Below is the example of YAML file
metadata:
name: <test-name>-svc
namespace: abc
spec:
selector:
app: <test-name>
ports:
- protocol: TCP
name: http
port: 80
targetPort: 8080
My expected output is:
metadata:
name: sweta-svc
namespace: abc
spec:
selector:
app: sweta
ports:
- protocol: TCP
name: http
port: 80
targetPort: 8080
Can someone help me?
Appreciate all your help. Thanks in advance!
There are two easy ways to accomplish your search and replace <test-name> string with sweta
Note: Assuming your yaml file name is config.yaml
Option-1: Using stream editor command 'sed' as below:
To overwrite the original file
sed -i 's/<test-name>/sweta/g' config.yaml
To write the output to a different file
sed 's/<test-name>/sweta/g' config.yaml > updated_config.yaml
The syntax of sed command is sed -i 's/original/new/g' file.txt
where
sed is the command name
-i stands for in-place edit. This option is used to overwrite the original file. If you don't use this, you have to redirect output to a different file as done in second command above.
s stands for substitute
original is the search string
new is the replace string
g is for global (replace all occurrences). Omitting this will just replace only the first occurence
file.txt is the text file (Linux doesn't use extensions like .txt but people often name files with such extensions as a convention to denote file type)
Option-2: Use 'awk' command:
awk is a very powerful text processing command with its own language syntax, but this search and replace is quite easy:
awk '{gsub("<test-name>", "sweta"); print $0}' config.yaml > updated_config.yaml
I want to uncomment a line in file sshd_config by using Ansible and I have the following working configuration:
- name: Uncomment line from /etc/ssh/sshd_config
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '^#AuthorizedKeysFile'
line: 'AuthorizedKeysFile .ssh/authorized_keys'
However this config only works if the line starts by #AuthorizedKeysFile, but it won't work if the line starts by # AuthorizedKeysFile or # AuthorizedKeysFile (spaces between # and the words).
How can I configure the regexp so it won't take into account any number of spaces after '#'?
I've tried to add another lineinfile option with a space after '#', but this is not a good solution:
- name: Uncomment line from /etc/ssh/sshd_config
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '# AuthorizedKeysFile'
line: 'AuthorizedKeysFile .ssh/authorized_keys'
If you need zero or more white spaces after the '#' character, the following should suffice:
- name: Uncomment line from /etc/ssh/sshd_config
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '^#\s*AuthorizedKeysFile.*$'
line: 'AuthorizedKeysFile .ssh/authorized_keys'
The modification to your original code is the addition of the \s* and the .*$ in the regex.
Explanation:
\s - matches whitespace (spaces, tabs, line breaks and form feeds)
* - specifies that the expression to it's left (\s) can have zero or more instances in a match
.* - matches zero or more of any character
$ - matches the end of the line
Firstly, you are using the wrong language. With Ansible, you don't tell it what to do, but define the desired state. So it shouldn't be Uncomment line form /etc/ssh/sshd_config, but Ensure AuthorizedKeysFile is set to .ssh/authorized_keys.
Secondly, it doesn't matter what the initial state is (if the line is commented, or not). You must specify a single, unique string that identifies the line.
With sshd_config this is possible as the AuthorizedKeysFile directive occurs only once in the file. With other configuration files this might be more difficult.
- name: Ensure AuthorizedKeysFile is set to .ssh/authorized_keys
lineinfile:
dest: /etc/ssh/sshd_config
regexp: AuthorizedKeysFile
line: 'AuthorizedKeysFile .ssh/authorized_keys'
It will match any line containing AuthorizedKeysFile string (no matter if it's commented or not, or how many spaces are there) and ensure the full line is:
AuthorizedKeysFile .ssh/authorized_keys
If the line were different, Ansible will report "changed" state.
On the second run, Ansible will find the AuthorizedKeysFile again and discover the line is already in the desired state, so it will end the task with "ok" state.
One caveat with the above task is that if any of the lines contains a comment such as a real, intentional comment (for example an explanation in English containing the string AuthorizedKeysFile), Ansible will replace that line with the value specified in line.
I should caveat this with #techraf's point that 99% of the time a full template of a configuration file is almost always better.
Times I have done lineinfile include weird and wonderful configuration files that are managed by some other process, or laziness for config I don't fully understand yet and may vary by distro/version and I don't want to maintain all the variants... yet.
Go forth and learn more Ansible... it is great because you can keep iterating on it from raw bash shell commands right up to best practice.
lineinfile module
Still good to see how best to configuration manage one or two settings just a little better with this:
tasks:
- name: Apply sshd_config settings
lineinfile:
path: /etc/ssh/sshd_config
# might be commented out, whitespace between key and value
regexp: '^#?\s*{{ item.key }}\s'
line: "{{ item.key }} {{ item.value }}"
validate: '/usr/sbin/sshd -T -f %s'
with_items:
- key: MaxSessions
value: 30
- key: AuthorizedKeysFile
value: .ssh/authorized_keys
notify: restart sshd
handlers:
- name: restart sshd
service:
name: sshd
state: restarted
validate don't make the change if the change is invalid
notify/handlers the correct way to restart once only at the end
with_items (soon to become loop) if you have multiple settings
^#? the setting might be commented out - see the other answer
\s*{{ item.key }}\s will not match other settings (i.e. SettingA cannot match NotSettingA or SettingAThisIsNot)
Still might clobber a comment like # AuthorizedKeysFile - is a setting which we have to live with because there could be a setting like AuthorizedKeysFile /some/path # is a setting... re-read the caveat.
template module
- name: Configure sshd
template:
src: sshd_config.j2
dest: /etc/ssh/sshd_config
owner: root
group: root
mode: "0644"
validate: '/usr/sbin/sshd -T -f %s'
notify: restart sshd
handlers:
- name: restart sshd
service:
name: sshd
state: restarted
multiple distro support
And if you are not being lazy about supporting all your distros see this tip
- name: configure ssh
template: src={{ item }} dest={{ SSH_CONFIG }} backup=yes
with_first_found:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.sshd_config.j2"
- "{{ ansible_distribution }}.sshd_config.j2"
https://ansible-tips-and-tricks.readthedocs.io/en/latest/modifying-files/modifying-files/
(needs to be updated to a loop using the first_found lookup)
Is it possible to achieve the same goal with replace module.
https://docs.ansible.com/ansible/latest/modules/replace_module.html
- name: Uncomment line from /etc/ssh/sshd_config
replace:
path: /etc/ssh/sshd_config
regexp: '^\s*#+AuthorizedKeysFile.*$'
replace: 'AuthorizedKeysFile .ssh/authorized_keys'
If you want to simply uncomment a line without setting the value, you can use replace with backreferences, eg (with a handy loop):
- name: Enable sshd AuthorizedKeysFile
replace:
path: /etc/ssh/sshd_config
# Remove comment and first space from matching lines
regexp: '^#\s?(\s*){{ item }}(.+)$'
replace: '\1{{ item }}\2'
loop:
- 'AuthorizedKeysFile'
This will only remove the first space after the #, and so retain any original indenting. It will also retain anything after the key (eg the default setting, and any following comments)
Thanks to the other helpful answers that provided a solid starting point.
I'm trying to remove a block from yml file using the following command,
awk '$1 == "tool:"{t=1}
t==1 && $1 == "ports:"{t++; next}
t==2 && /:[[:blank:]]*$/{t=0}
t != 2' file.yml
for the following yml,
tool:
image: tool.xxx.com/platform/app:dev
log_driver: syslog
restart: always
ports:
- "54325:80"
- "543325:80"
volume:
- "a:b"
tool1:
image: tool1.xxx.com/platform/app:dev
log_driver: syslog
restart: always
ports:
- "54325:80"
- "543325:80"
volume:
- "a:b"
The goal is to remove the ports sub-block from the tool main block.
This answer from https://stackoverflow.com/a/37256824/698072 seems to be perfectly valid when tried here. But it is removing everything right from the ports until the end of yml file in my Ubuntu 14.04 (tried both in sh and zsh in multiple systems of 14.04).
Expected:
tool:
image: tool.xxx.com/platform/app:dev
log_driver: syslog
restart: always
volume:
- "a:b"
tool1:
image: tool1.xxx.com/platform/app:dev
log_driver: syslog
restart: always
ports:
- "54325:80"
- "543325:80"
volume:
- "a:b"
Output:
tool:
image: tool.xxx.com/platform/app:dev
log_driver: syslog
restart: always
Any help on this peculiar case would be really great.
I've made few changes in my earlier awk command to take care of possibility that your yml has DOS line endings.
awk '{sub(/\r$/, "")}
$1 == "tool:"{t=1}
t==1 && $1 == "ports:"{t++; next}
t==2 && /:\s*$/{t=0}
t != 2' file.yml
Looks like Ubuntu awk doesn't support POSIX classes [[:blank:]] or [[:space:]] so we'll need to use \s to match white-spaces.
If the file was created on windows and therefore has windows line endings (CR-LF), then the pattern /:[[:blank:]]*$/ will fail
[[:blank:]] only matches tabs and spaces. To match any whitespace character, including \r (carriage return), use [[:space:]].
If that turns out to be the problem, consider using dos2unix or similar to remove the incorrect line endings, as they will cause you other problems with other utilities.
I tried this in my task, but doesn't seem to work
- name: Fix line endings from CRLF to LF
local_action: replace dest={{my_dir}}/conf/{{item}} regexp='\r\n' replace='\n'
I usually do this using sed as follows and it works
sed -i 's/\r//g' file
I want to avoid using shell module to do this replacement as it throws a warning in ansible
You can remove the CRLF line endings with the -replace command. Your playbook might look like:
---
- hosts: all
tasks:
- local_action: replace dest={{my_dir}}/conf/{{item}} regexp="\r"
By not specifying the replace parameter in the - replace command, it will just remove all carriage returns. See http://docs.ansible.com/ansible/replace_module.html.
I tested this with a local file I created and it worked when testing on localhost. It also worked when I added localhost to the /etc/ansible/hosts file and had the following playbook instead:
---
- hosts: all
tasks:
- replace: dest={{my_dir}}/conf/{{item}} regexp="\r"
Just be sure to use the absolute filepath.
You can do something like this:
set_fact:
my_content: "{{ lookup('file', "{{my_dir}}/conf/{{item}}" ) | replace('\r\n', '\n')}}"
After this you can use the content or save in the disk.
The following converts line endings using the Jinja2 template engine. A line-ending directive is inserted at the beginning of the source file on the ansible machine (delegate_to: localhost). Sending the file to the downstream server can then be done by applying template or win_template to the file.
It handles source files with any line-ending, which could be useful if you're working through a list of files from more than one origin.
- name: prepare to add line endings
lineinfile:
insertbefore: BOF
dest: '{{ src_file }}'
line: '#jinja2: newline_sequence:"\n"'
#for Linux to Windows: #line: '#jinja2: newline_sequence:"\r\n"'
delegate_to: localhost
- name: copy changed file with correct line-endings
template: # win_template for Linux to Windows
src: '{{ src_file }}'
dest: '{{ dest_file }}'
In the lineinfile module, it replaces the full line.
If the line is long I have to repeat the whole line again.
Let us suppose I want to replace the single word in the file:
#abc.conf
This is my horse
this is the playbook:
- lineinfile: dest=abc.conf
state=present
regexp='horse'
line='This is my dog'
backup=yes
is there any way to achieve someting like sed 's/horse/dog/g' ?
New module replace available since 1.6 version:
- replace:
dest=abc.conf
regexp='horse'
replace='dog'
backup=yes
You can use backreferences to retrieve other parts(that should not be changed) of the line:
- lineinfile: dest=abc.conf
state=present
regexp='^(.*)horse(.*)$'
line='\1dog\2'
backup=yes
backrefs=yes
If you need to do more replace operations in one block and you have the file locally, you might want to consider using template, which substitutes variables in the template file and copies the file to the remote:
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf
In the local file you can write a variable with ansible sintax like
{{variable}}
and it will be substituted if it is in the scope of the script. Here the docs.