如何测试 bashrc 是否包含字符串
How to test if a bashrc contains a string
基本上我想向 bash_profile 添加一个字符串,但前提是它目前不存在。必须是这样的,但还不确定如何实现
"{% if aem_cms_runmode == 'author' and ~/.bash_profile contains "kobold" %}
echo 'export PATH=/opt/day/libs/kobold/kobold-latest:$PATH' >>~/.bash_profile
{% endif %}"
我做了一些研究,但找不到在 jinja 中测试它的合适方法。我只看到检查变量是否具有某些值。
为什么不使用lineinfile?它正是这样做的:
This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression. This is primarily useful when you want to change a single line in a file only.
- name: Update PATH in ~/.bash_profile
lineinfile:
dest: "~/.bash_profile"
line: "export PATH=/opt/day/libs/kobold/kobold-latest:$PATH"
这是幂等的。
如果文件中没有 export PATH
,任务将按预期进行。但是,如果具有不同值的 export PATH
已经存在,结果将在文件中重复 export PATH
例如
给定文件
shell> cat bash_profile
export PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin
下面的任务
- lineinfile:
dest: bash_profile
line: "export PATH=/opt/day/libs/kobold/kobold-latest:$PATH"
将添加重复的 export PATH
行
TASK [lineinfile] ************************************************************
--- before: bash_profile (content)
+++ after: bash_profile (content)
@@ -1 +1,2 @@
export PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin
+export PATH=/opt/day/libs/kobold/kobold-latest:$PATH
如果要替换这样的一行,如果存在的话,需要regexp。引用
"... When modifying a line the regexp should typically match both the initial state of the line as well as its state after replacement by line to ensure idempotence."
例如下面的任务
- lineinfile:
dest: bash_profile
regex: "^export PATH=.*$"
line: "export PATH=/opt/day/libs/kobold/kobold-latest:$PATH"
将替换这样的行,如果它存在的话
TASK [lineinfile] ********************************************************
--- before: bash_profile (content)
+++ after: bash_profile (content)
@@ -1 +1 @@
-export PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin
+export PATH=/opt/day/libs/kobold/kobold-latest:$PATH
包含regexp
的任务是幂等的,即文件改变后不会发生变化
shell> cat bash_profile
export PATH=/opt/day/libs/kobold/kobold-latest:$PATH
如果您只想在没有其他 export PATH
存在时添加您的 export PATH
,请测试它,例如仅当文件中不存在 export PATH
时,才会执行 lineinfile
任务。在这种情况下,不需要 regex
。使图案符合您的需要
- command: "grep '{{ pattern }}' bash_profile"
register: result
ignore_errors: true
changed_when: false
vars:
pattern: "export PATH"
- lineinfile:
dest: bash_profile
line: "export PATH=/opt/day/libs/kobold/kobold-latest:$PATH"
when: result.rc|int != 0
基本上我想向 bash_profile 添加一个字符串,但前提是它目前不存在。必须是这样的,但还不确定如何实现
"{% if aem_cms_runmode == 'author' and ~/.bash_profile contains "kobold" %}
echo 'export PATH=/opt/day/libs/kobold/kobold-latest:$PATH' >>~/.bash_profile
{% endif %}"
我做了一些研究,但找不到在 jinja 中测试它的合适方法。我只看到检查变量是否具有某些值。
为什么不使用lineinfile?它正是这样做的:
This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression. This is primarily useful when you want to change a single line in a file only.
- name: Update PATH in ~/.bash_profile
lineinfile:
dest: "~/.bash_profile"
line: "export PATH=/opt/day/libs/kobold/kobold-latest:$PATH"
这是幂等的。
如果文件中没有 export PATH
,任务将按预期进行。但是,如果具有不同值的 export PATH
已经存在,结果将在文件中重复 export PATH
例如
给定文件
shell> cat bash_profile
export PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin
下面的任务
- lineinfile:
dest: bash_profile
line: "export PATH=/opt/day/libs/kobold/kobold-latest:$PATH"
将添加重复的 export PATH
行
TASK [lineinfile] ************************************************************
--- before: bash_profile (content)
+++ after: bash_profile (content)
@@ -1 +1,2 @@
export PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin
+export PATH=/opt/day/libs/kobold/kobold-latest:$PATH
如果要替换这样的一行,如果存在的话,需要regexp。引用
"... When modifying a line the regexp should typically match both the initial state of the line as well as its state after replacement by line to ensure idempotence."
例如下面的任务
- lineinfile:
dest: bash_profile
regex: "^export PATH=.*$"
line: "export PATH=/opt/day/libs/kobold/kobold-latest:$PATH"
将替换这样的行,如果它存在的话
TASK [lineinfile] ********************************************************
--- before: bash_profile (content)
+++ after: bash_profile (content)
@@ -1 +1 @@
-export PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin
+export PATH=/opt/day/libs/kobold/kobold-latest:$PATH
包含regexp
的任务是幂等的,即文件改变后不会发生变化
shell> cat bash_profile
export PATH=/opt/day/libs/kobold/kobold-latest:$PATH
如果您只想在没有其他 export PATH
存在时添加您的 export PATH
,请测试它,例如仅当文件中不存在 export PATH
时,才会执行 lineinfile
任务。在这种情况下,不需要 regex
。使图案符合您的需要
- command: "grep '{{ pattern }}' bash_profile"
register: result
ignore_errors: true
changed_when: false
vars:
pattern: "export PATH"
- lineinfile:
dest: bash_profile
line: "export PATH=/opt/day/libs/kobold/kobold-latest:$PATH"
when: result.rc|int != 0