35. error handling
- name: this command prints FAILED when it fails
command: /usr/bin/example-command -x -y -z
register: command_result
failed_when: "'FAILED' in command_result.stderr
!
#切り戻しに使えそうです
39. module yum
- name: install the latest version of Apache #最新版をいれろー
yum: name=httpd state=latest
!
- name: remove the Apache package #けせー
yum: name=httpd state=absent
!
- name: install the latest version of Apache from the testing repo #testingリポジトリからいれろー?
yum: name=httpd enablerepo=testing state=present
!
- name: upgrade all packages #全アップデート
yum: name=* state=latest
!
- name: install the nginx rpm from a remote repo #nginxのリポジトリ追加
yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-
centos-6-0.el6.ngx.noarch.rpm state=present
!
- name: install nginx rpm from a local file #ローカルファイルからのインストール
yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present
40. module service
# Example action to start service httpd, if not running #起動
- service: name=httpd state=started
!
# Example action to stop service httpd, if running #停止
- service: name=httpd state=stopped
!
# Example action to restart service httpd, in all cases #再起動
- service: name=httpd state=restarted
!
# Example action to reload service httpd, in all cases #再読み込み
- service: name=httpd state=reloaded
!
# Example action to enable service httpd, and not touch the running state #サービス有効
- service: name=httpd enabled=yes
!
# Example action to start service foo, based on running process /usr/bin/foo #statusコマンドがないやつ
- service: name=foo pattern=/usr/bin/foo state=started #はプロセスを見て確認
!
# Example action to restart network service for interface eth0 #ネトワ再起動
- service: name=network state=restarted args=eth0
41. module command
# Example from Ansible Playbooks.
- command: /sbin/shutdown -t now
!
# Run the command if the specified file does not exist.
- command: /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database
!
# You can also use the 'args' form to provide the options. This command
# will change the working directory to somedir/ and will only run when
# /path/to/database doesn't exist.
- command: /usr/bin/make_database.sh arg1 arg2
args:
chdir: somedir/
creates: /path/to/database
!
# so variables like $HOME and operations like "<", ">", "¦", and "&" will not work
#(use the shell module if you need these features).
42. module shell
# Execute the command in remote shell; stdout goes to the specified
# file on the remote.
- shell: somescript.sh >> somelog.txt
!
# Change the working directory to somedir/ before executing the command.
- shell: somescript.sh >> somelog.txt chdir=somedir/
!
# You can also use the 'args' form to provide the options. This command
# will change the working directory to somedir/ and will only run when
# somedir/somelog.txt doesn't exist.
- shell: somescript.sh >> somelog.txt
args:
chdir: somedir/
creates: somelog.txt
!
#command through a shell (/bin/sh) on the remote node
43. module mysql_db
# Create a new database with name 'bobdata'
- mysql_db: name=bobdata state=present
!
# Copy database dump file to remote host and restore it to database
'my_db'
- copy: src=dump.sql.bz2 dest=/tmp
- mysql_db: name=my_db state=import target=/tmp/dump.sql.bz2
44. module mysql_user
# Create database user with name 'bob' and password '12345' with all database
privileges
- mysql_user: name=bob password=12345 priv=*.*:ALL state=present
!
# Creates database user 'bob' and password '12345' with all database privileges
and 'WITH GRANT OPTION'
- mysql_user: name=bob password=12345 priv=*.*:ALL,GRANT state=present
!
# Ensure no user named 'sally' exists, also passing in the auth credentials.
- mysql_user: login_user=root login_password=123456 name=sally state=absent
!
# Specify grants composed of more than one word
- mysql_user: name=replication password=12345 priv=*.*:"REPLICATION CLIENT"
state=present
45. module mysql_replication
# Stop mysql slave thread
- mysql_replication: mode=stopslave
!
# Get master binlog file name and binlog position
- mysql_replication: mode=getmaster
!
# Change master to master server 192.168.1.1 and use binary log
'mysql-bin.000009' with position 4578
- mysql_replication: mode=changemaster master_host=192.168.1.1
master_log_file=mysql-bin.000009 master_log_pos=4578
46. module copy
# Example from Ansible Playbooks
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo
group=foo mode=0644
!
# Copy a new "ntp.conf file into place, backing up the original if it differs
from the copied version
- copy: src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root
mode=644 backup=yes
!
# Copy a new "sudoers" file into place, after passing validation with
visudo
- copy: src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'
47. module fetch
# Store file into /tmp/fetched/host.example.com/tmp/somefile
- fetch: src=/tmp/somefile dest=/tmp/fetched
!
# Specifying a path directly
- fetch: src=/tmp/somefile dest=/tmp/prefix-{{ ansible_hostname }}
flat=yes
!
# Specifying a destination path
- fetch: src=/tmp/uniquefile dest=/tmp/special/ flat=yes
!
# Storing in a path relative to the playbook
- fetch: src=/tmp/uniquefile dest=special/prefix-{{ ansible_hostname }}
flat=yes
50. module template
# Example from Ansible Playbooks
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin
group=wheel mode=0644
!
# Copy a new "sudoers" file into place, after passing validation with
visudo
- template: src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf
%s
!
#Templates are processed by the Jinja2 templating language