Last active
August 29, 2015 14:12
-
-
Save cognitom/40a574752df8c33fdb96 to your computer and use it in GitHub Desktop.
Ansibleでファイルの行を書き換える3つの方法 ref: http://qiita.com/cognitom/items/57de72b739642041dcd5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: "設定の修正(1)" | |
lineinfile: >- | |
dest='/path/to/file/' | |
state=present | |
backrefs=yes | |
regexp='^#?\s*ServerTokens' | |
line='ServerTokens Prod' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: "設定の修正(2)" | |
lineinfile: >- | |
dest='/path/to/file' | |
state=present | |
backrefs=yes | |
regexp='{{ item.regexp }}' | |
line='{{ item.line }}' | |
with_items: | |
- regexp: '^#?\s*ServerTokens' | |
line: 'ServerTokens Prod' | |
- regexp: '^#?\s*ServerSignature' | |
line: 'ServerSignature Off' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: "設定の修正(3)" | |
replace: >- | |
dest='/path/to/file' | |
regexp='AllowOverride None' | |
replace='AllowOverride All' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<Directory /> | |
Options FollowSymLinks | |
AllowOverride None | |
</Directory> | |
<Directory "/var/www/html"> | |
Options FollowSymLinks | |
AllowOverride None | |
</Directory> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: "設定の修正(4)" | |
shell: >- | |
c='/path/to/file' && | |
k='<Directory "\/var\/www\/html">' && | |
s='AllowOverride None' && | |
r='AllowOverride All' && | |
mv $c $c.backup && | |
awk "/$k/{f=1} f==1&&/$s/{sub(/.+/,\"$r\"); f=0} 1" $c.backup > $c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
awk " | |
/$k/{f=1} | |
f==1&&/$s/{sub(/.+/,\"$r\"); f=0} | |
1 | |
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ awk '/search/gsub(/search/replace)' target.txt > target.txt | |
$ sed -i -e 's/search/replace/g' target.txt | |
$ perl -p -i -e 's/search/replace/g' target.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: "webservers" | |
sudo: yes | |
vars: | |
document_root: "/var/www/html" | |
httpd_conf: "/etc/httpd/conf/httpd.conf" | |
tasks: | |
- name: "yumのアップデート" | |
command: "yum -y update" | |
- name: "パッケージのインストール" | |
yum: >- | |
name={{ item }} | |
state=present | |
with_items: | |
- "httpd" | |
- "libmcrypt" | |
- "libselinux-python" | |
- name: "httpd.confの修正 (1)" | |
lineinfile: >- | |
dest='{{ httpd_conf }}' | |
state=present | |
backrefs=yes | |
regexp='{{ item.regexp }}' | |
line='{{ item.line }}' | |
with_items: | |
- regexp: '^#?\s*ServerTokens' | |
line: 'ServerTokens Prod' | |
- regexp: '^#?\s*ServerSignature' | |
line: 'ServerSignature Off' | |
- name: "httpd.confの修正 (2)" | |
shell: >- | |
c='{{ httpd_conf }}' && | |
k='<Directory "{{ document_root|replace("/","\/") }}">' && | |
t='AllowOverride None' && | |
s='/None$/' && | |
r='All' && | |
awk "/$k/{f=1} f==1&&/$t/{sub($s,\"$r\"); f=0} 1" $c > $c | |
- name: "サービスを起動: httpd" | |
service: >- | |
name= httpd | |
state=started | |
enabled=yes | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ perl -p0777 -i -e 's/search/replace/gs' target.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: "webservers" | |
sudo: yes | |
vars: | |
document_root: "/var/www/html" | |
httpd_conf: "/etc/httpd/conf/httpd.conf" | |
tasks: | |
- name: "yumのアップデート" | |
command: "yum -y update" | |
- name: "パッケージのインストール" | |
yum: >- | |
name={{ item }} | |
state=present | |
with_items: | |
- "httpd" | |
- "libmcrypt" | |
- "libselinux-python" | |
- name: "httpd.confの修正 (1)" | |
lineinfile: >- | |
dest='{{ httpd_conf }}' | |
state=present | |
backrefs=yes | |
regexp='{{ item.regexp }}' | |
line='{{ item.line }}' | |
with_items: | |
- regexp: '^#?\s*ServerTokens' | |
line: 'ServerTokens Prod' | |
- regexp: '^#?\s*ServerSignature' | |
line: 'ServerSignature Off' | |
- name: "httpd.confの修正 (2)" | |
shell: >- | |
c='{{ httpd_conf }}' && | |
k='<Directory "{{ document_root|replace("/","\/") }}">' && | |
t='AllowOverride None' && | |
s='/None$/' && | |
r='All' && | |
awk "/$k/{f=1} f==1&&/$t/{sub($s,\"$r\"); f=0} 1" $c > $c | |
- name: "サービスを起動: httpd" | |
service: >- | |
name= httpd | |
state=started | |
enabled=yes | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment