Skip to content

Instantly share code, notes, and snippets.

@cognitom
Last active August 29, 2015 14:12
Show Gist options
  • Save cognitom/40a574752df8c33fdb96 to your computer and use it in GitHub Desktop.
Save cognitom/40a574752df8c33fdb96 to your computer and use it in GitHub Desktop.
Ansibleでファイルの行を書き換える3つの方法 ref: http://qiita.com/cognitom/items/57de72b739642041dcd5
- name: "設定の修正(1)"
lineinfile: >-
dest='/path/to/file/'
state=present
backrefs=yes
regexp='^#?\s*ServerTokens'
line='ServerTokens Prod'
- 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'
- name: "設定の修正(3)"
replace: >-
dest='/path/to/file'
regexp='AllowOverride None'
replace='AllowOverride All'
...
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/html">
Options FollowSymLinks
AllowOverride None
</Directory>
...
- 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
awk "
/$k/{f=1}
f==1&&/$s/{sub(/.+/,\"$r\"); f=0}
1
"
$ 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
---
- 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
$ perl -p0777 -i -e 's/search/replace/gs' target.txt
---
- 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