すでにある環境に対して、Ansibleで構成変更するとき、lineinfile で行を置き換えるのではなく、既存の行の一部を変更したいことがある。
しかし、このようなときに、冪等性を確保するのは意外に難しい。正規表現では、最小一致や否定先読みアサーションを使ったとしても、うまく行を選択できないか、あるいは複数回実行すると、そのたびに変更されてしまったりする。
とりあえず、grepで書き換えるべきかどうかを判定して、必要なときだけ書き換えるようにしてみた。
- hosts: servers
vars:
httpd_conf: /etc/apache2/apache2.conf
tasks:
- name: grep %{Session}C
command: grep '^LogFormat[[:space:]].*%{Session}C.*[[:space:]]combined' {{httpd_conf}}
register: grep
ignore_errors: True
failed_when: False
changed_when: False
- name: change httpd.conf
lineinfile:
backup=yes
backrefs=yes
dest={{httpd_conf}}
state=present
regexp="^(LogFormat\s+\")(.*)(\"\s+combined)$"
line="\1\2 \\\\\"%{Session}C\\\\\" \3"
when: grep.rc == 1
を実行した時のものですが、localhostの実行では日付の出力にロケール設定が効いていません。LANG=ja_JP.UTF-8; export LANG; printenv LANG; date
指定したときは、さらに不思議な結果になります。sh -c 'LANG=ja_JP.UTF-8; export LANG; printenv LANG; date'
Select nodes by name: 名前を列挙する
mynode1 mynode2
This will select both nodes.Filter nodes by attribute value: 属性値で選択する
Use Regular Expressions: 正規表現で選択することができる
- Include:
attribute: value
- Exclude:
!attribute: value
Regex syntax checking:hostname: dev(\d+).test.com
.
attribute: /regex/
Examples:attribute: /regex/
- All nodes すべてのノード
- name: .*
- Nodes tagged "production" 「production」というタグがついたノード
- tags: production
- Unix nodes OSが「unix」のノード
- osFamily: unix
Command - Execute a remote commanderror hander で、ステップがエラー終了したとき、ワークフローを打ち切るか、実行を続けるかを指定できます。また、エラー処理のためのコマンドを実行することもできます。
Script - Execute an inline script
Script file or URL - Execute a local script file or a script from a URL
Job Reference - Execute another Job for each Node
2 Node Step PluginsCopy File - Copy a file to a destination on a remote node.
Local Command - Run a command locally on the server
The option values will be available to scripts in these forms:
Bash:$RD_OPTION_NAME
(ローカル実行のみ。環境変数として渡されます。)Commandline Arguments:${option.name}
Script Content:@option.name@
秒 分 時 日 月 曜 年先頭に秒、末尾に年が増えています。
Format
A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:
Field Name Mandatory Allowed Values Allowed Special Characters Seconds YES 0-59 , - * / Minutes YES 0-59 , - * / Hours YES 0-23 , - * / Day of month YES 1-31 , - * ? / L W Month YES 1-12 or JAN-DEC , - * / Day of week YES 1-7 or SUN-SAT , - * ? / L # Year NO empty, 1970-2099 , - * / So cron expressions can be as simple as this: * * * * ? *or more complex, like this: 0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010Special characters
* ("all values") - used to select all values within a field. For example, "" in the minute field means *"every minute". ? ("no specific value") - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put "10" in the day-of-month field, and "?" in the day-of-week field. See the examples below for clarification. - - used to specify ranges. For example, "10-12" in the hour field means "the hours 10, 11 and 12". , - used to specify additional values. For example, "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday". / - used to specify increments. For example, "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". You can also specify '/' after the '' character - in this case '' is equivalent to having '0' before the '/'. '1/3' in the day-of-month field means "fire every 3 days starting on the first day of the month". L ("last") - has different meaning in each of the two fields in which it is allowed. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results. W ("weekday") - used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days.The 'L' and 'W' characters can also be combined in the day-of-month field to yield 'LW', which translates to *"last weekday of the month"*.
- # - used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means"the third Friday of the month" (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.
The legal characters and the names of months and days of the week are not case sensitive. MON is the same as mon.
% ssh-keygen -t dsa -f rundeck -N ''
Generating public/private dsa key pair.
Your identification has been saved in rundeck.
Your public key has been saved in rundeck.pub.
The key fingerprint is:
39:97:87:08:20:3c:4b:e7:d4:e9:04:c0:32:df:c3:06 komeda@ubuntu
The key's randomart image is:
+--[ DSA 1024]----+
| oo.oo . |
|o E.o.+ |
| = X o. |
| o * .. o o |
| . . S + . |
| o . |
| |
| |
| |
+-----------------+
<?xml version="1.0" encoding="UTF-8"?>
<project>
<node description="Rundeck server node" hostname="localhost" name="localhost" osarch="amd64" osfamily="unix" osname="Linux" osversion="3.13.0-24-generic" tags="" username="rundeck">
</node>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project>
<node description="Rundeck server node" hostname="localhost" name="localhost" osarch="amd64" osfamily="unix" osname="Linux" osversion="3.13.0-24-generic" tags="" username="rundeck">
</node>
<node name="ubuntu-host" description="Rundeck server node" tags="" hostname="ubuntu-host" osArch="amd64" osFamily="unix" osName="Linux" osVersion="3.13.0-24-generic" username="rundeck"/>
</project>
% cd ~rundeck/.ssh
% cat id_rsa.pub >>authorized_keys
% chown rundeck:rundeck authorized_keys
% chmod go-rwx authorized_keys
username="user1"
Specifying SSH Username
The username used to connect via SSH is taken from theusername
Node attribute:
username="user1"
This value can also include a property reference if you want to dynamically change it, for example to the name of the current Rundeck user, or the username submitted as a Job Option value:
${job.username}
- uses the username of the user executing the Rundeck execution.${option.someUsername}
- uses the value of a job option named "someUsername".If theusername
node attribute is not set, then the static value provided via project or framework configuration is used. The username for a node is determined by looking for a value in this order:
- Node level:
username
node attribute. Can contain property references to dynamically set it from Option or Execution values.- Project level:
project.ssh.user
property inproject.properties
file for the project.- Rundeck level:
framework.ssh.user
property inframework.properties
file for the Rundeck installation.
Debian/Ubuntu Install
rundeck-2.4.1-1-GA.debExecute:
dpkg -i rundeck-2.4.1-1-GA.deb
% shasum rundeck-2.4.1-1-GA.deb
5ce117bc994c6911bfbd46cc6a7f58b76e2e228f rundeck-2.4.1-1-GA.deb
# dpkg -i rundeck-2.4.1-1-GA.deb
Selecting previously unselected package rundeck.
(Reading database ... 58039 files and directories currently installed.)
Preparing to unpack rundeck-2.4.1-1-GA.deb ...
Unpacking rundeck (2.4.1) ...
dpkg: dependency problems prevent configuration of rundeck:
rundeck depends on java6-runtime | java6-runtime-headless | java7-runtime | java7-runtime-headless; however:
Package java6-runtime is not installed.
Package java6-runtime-headless is not installed.
Package java7-runtime is not installed.
Package java7-runtime-headless is not installed.
dpkg: error processing package rundeck (--install):
dependency problems - leaving unconfigured
Processing triggers for ureadahead (0.100.0-16) ...
Errors were encountered while processing:
rundeck
# aptitude install openjdk-7-jre
The following NEW packages will be installed:
acl{a} at-spi2-core{a} ca-certificates-java{a} colord{a} cpp{a}
cpp-4.8{a} dbus-x11{a} dconf-gsettings-backend{a} dconf-service{a}
desktop-file-utils{a} fontconfig{a} fontconfig-config{a}
fonts-dejavu-core{a} fonts-dejavu-extra{a} gconf-service{a}
gconf-service-backend{a} gconf2{a} gconf2-common{a} gdisk{a} gvfs{a}
gvfs-common{a} gvfs-daemons{a} gvfs-libs{a} hicolor-icon-theme{a}
java-common{a} libasound2{a} libasound2-data{a} libasyncns0{a}
libatasmart4{a} libatk-bridge2.0-0{a} libatk-wrapper-java{a}
libatk-wrapper-java-jni{a} libatk1.0-0{a} libatk1.0-data{a}
libatspi2.0-0{a} libavahi-client3{a} libavahi-common-data{a}
libavahi-common3{a} libavahi-glib1{a} libbonobo2-0{a}
libbonobo2-common{a} libcairo-gobject2{a} libcairo2{a} libcanberra0{a}
libcloog-isl4{a} libcolord1{a} libcolorhug1{a} libcups2{a} libdatrie1{a}
libdconf1{a} libdrm-intel1{a} libdrm-nouveau2{a} libdrm-radeon1{a}
libexif12{a} libflac8{a} libfontconfig1{a} libfontenc1{a} libgconf-2-4{a}
libgconf2-4{a} libgd3{a} libgdk-pixbuf2.0-0{a} libgdk-pixbuf2.0-common{a}
libgif4{a} libgl1-mesa-dri{a} libgl1-mesa-glx{a} libglapi-mesa{a}
libgmp10{a} libgnome2-0{a} libgnome2-bin{a} libgnome2-common{a}
libgnomevfs2-0{a} libgnomevfs2-common{a} libgphoto2-6{a}
libgphoto2-l10n{a} libgphoto2-port10{a} libgraphite2-3{a} libgtk-3-0{a}
libgtk-3-bin{a} libgtk-3-common{a} libgtk2.0-0{a} libgtk2.0-bin{a}
libgtk2.0-common{a} libgudev-1.0-0{a} libgusb2{a} libharfbuzz0b{a}
libice6{a} libicu52{a} libidl-common{a} libidl0{a} libieee1284-3{a}
libisl10{a} libjasper1{a} libjbig0{a} libjpeg-turbo8{a} libjpeg8{a}
liblcms2-2{a} libllvm3.4{a} libltdl7{a} libmpc3{a} libmpfr4{a}
libnspr4{a} libnss3{a} libnss3-nssdb{a} libogg0{a} liborbit-2-0{a}
liborbit2{a} libpango-1.0-0{a} libpangocairo-1.0-0{a}
libpangoft2-1.0-0{a} libpciaccess0{a} libpixman-1-0{a} libpulse0{a}
libsane{a} libsane-common{a} libsecret-1-0{a} libsecret-common{a}
libsm6{a} libsndfile1{a} libtdb1{a} libthai-data{a} libthai0{a}
libtiff5{a} libtxc-dxtn-s2tc0{a} libudisks2-0{a} libv4l-0{a}
libv4lconvert0{a} libvorbis0a{a} libvorbisenc2{a} libvorbisfile3{a}
libvpx1{a} libwayland-client0{a} libwayland-cursor0{a} libx11-xcb1{a}
libxaw7{a} libxcb-dri2-0{a} libxcb-dri3-0{a} libxcb-glx0{a}
libxcb-present0{a} libxcb-render0{a} libxcb-shape0{a} libxcb-shm0{a}
libxcb-sync1{a} libxcomposite1{a} libxcursor1{a} libxdamage1{a}
libxfixes3{a} libxft2{a} libxi6{a} libxinerama1{a} libxkbcommon0{a}
libxmu6{a} libxpm4{a} libxrandr2{a} libxrender1{a} libxshmfence1{a}
libxt6{a} libxtst6{a} libxv1{a} libxxf86dga1{a} libxxf86vm1{a}
openjdk-7-jre openjdk-7-jre-headless{a} policykit-1-gnome{a}
sound-theme-freedesktop{a} tzdata-java{a} udisks2{a} x11-common{a}
x11-utils{a}
The following packages will be upgraded:
tzdata
The following partially installed packages will be configured:
rundeck
1 packages upgraded, 168 newly installed, 0 to remove and 8 not upgraded.
Need to get 241 kB/83.8 MB of archives. After unpacking 257 MB will be used.
Do you want to continue? [Y/n/?]
(中略)
Current status: 0 broken [-1], 8 updates [-1].
# dpkg -i rundeck-2.4.1-1-GA.deb
(Reading database ... 66206 files and directories currently installed.)
Preparing to unpack rundeck-2.4.1-1-GA.deb ...
Unpacking rundeck (2.4.1) over (2.4.1) ...
Setting up rundeck (2.4.1) ...
usermod: no changes
Processing triggers for ureadahead (0.100.0-16) ...
# service rundeckd start
rundeckd start/running, process 58776
# tail /var/log/rundeck/service.log
2015-01-30 23:23:56.852:INFO:oejs.Server:jetty-7.6.0.v20120127
2015-01-30 23:23:58.750:INFO:oejw.StandardDescriptorProcessor:NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
2015-01-30 23:23:59.925:INFO:/:Initializing Spring root WebApplicationContext
INFO BootStrap: Starting Rundeck 2.4.1-1...
INFO BootStrap: using rdeck.base config property: /var/lib/rundeck
INFO BootStrap: loaded configuration: /etc/rundeck/framework.properties
INFO BootStrap: RSS feeds disabled
2015-01-30 23:24:20.216:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/var/lib/rundeck/exp/webapp/},/var/lib/rundeck/exp/webapp
2015-01-30 23:24:20.271:INFO:/:Initializing Spring FrameworkServlet 'grails'
2015-01-30 23:24:20.354:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:4440
chown rundeck:rundeck /tmp/rundeckchmod 750 /tmp/rundeck
http://localhost:4440になっています。ソケット自体はIN6ADDR_ANY_INIT でバインドしているため、どこからでもアクセスできるのですが、ログイン認証の後このURLにリダイレクトされてしまうため操作が継続できないと思います。
grails.serverURL=http://localhost:4440
grails.serverURL=http://ホスト名orIPアドレス:4440
あるいはLANG=en_US.UTF-8 export LANG
LANG=ja_JP.UTF-8 export LANG
<username>:<password>[,<rolename> ...]
# java -cp /var/lib/rundeck/bootstrap/jetty-all-7.6.0.v20120127.jar org.eclipse.jetty.util.security.Password username password
https://wiki.eclipse.org/Jetty/Howto/Secure_Passwords
printf "MD5:%s" $(read -sp "Password: " passwd; echo "$passwd") | md5sum
# java -cp /var/lib/rundeck/bootstrap/jetty-all-7.6.0.v20120127.jar org.eclipse.jetty.util.security.Password admin admin
admin
OBF:1u2a1toa1w8v1tok1u30
MD5:21232f297a57a5a743894a0e4a801fc3
CRYPT:adpexzg3FUZAk
admin:MD5:21232f297a57a5a743894a0e4a801fc3,user,admin,architect,deploy,build
# mkdir /var/lib/rundeck/.ssh
# chown rundeck:rundeck /var/lib/rundeck/.ssh
# chmod go-rwx /var/lib/rundeck/.ssh
# ssh-keygen -t rsa -f /var/lib/rundeck/.ssh/id_rsa -N '' -C Rundeck
Generating public/private rsa key pair.
Your identification has been saved in /var/lib/rundeck/.ssh/id_rsa.
Your public key has been saved in /var/lib/rundeck/.ssh/id_rsa.pub.
The key fingerprint is:
83:06:12:c0:bc:29:96:b3:1b:ae:c7:f8:71:b9:53:37 Rundeck
The key's randomart image is:
+--[ RSA 2048]----+
|+.. |
| o . |
| = . |
|.* . . . |
|o o o S |
| o o. E. |
|.o+ o. . . |
|.ooo.. |
|oo. .. |
+-----------------+
# chown rundeck:rundeck /var/lib/rundeck/.ssh/id_rsa*