LoginSignup
20
21

More than 5 years have passed since last update.

AnsibleのroleやTips

Last updated at Posted at 2014-09-06

参考

sshのポートが22でない場合

hosts
[web_all]
wwwsrv1 ansible_ssh_port=12345

モジュール一覧表示

ansible-doc -l

気になるモジュール

  • locale_gen
  • postgresql_db
  • postgresql_privs
  • postgresql_user

serverの状態をチェック

書き方
- assert: { that: "ansible_os_family != 'RedHat'" }
書き方2
- assert: {
  that:
    "ansible_os_family != 'RedHat'"
  }

ansibleを実行すると、結果を$?で取得できる。
(正常終了: 0、異常終了: 1以上)

role入門してみた

  • run.sh
  • main.yml
  • vim/tasks/main.yml

というファイル構成の場合、

main.yml
---
- hosts: localhost
  sudo: yes
  roles:
    - role: vim
vim/tasks/main.yml
---
- name: install vim
  yum: name=vim state=latest
run.sh
test -z $(which ansible-playbook) && sudo yum install -y epel-release && sudo yum install -y ansible
ansible-playbook --diff -vv main.yml

role入門2

  • tasks,vars,handlersを使う
php/tasks/main.yml
---
- name: find {{ php_log }}
  stat: path={{ php_log }}
  register: result

- name: create {{ php_log }}
  file: path={{ php_log }} owner=apache group=apache mode=0644 state=touch
  when: not result.stat.exists

- name: install php5.6
  yum: pkg={{ item }} state=installed enablerepo=remi
  with_items:
    - php56
    - php56-php-devel
    - php56-php-mysqlnd
    - php56-php-mbstring
    - php56-php-gd
    - php56-php-cli
  notify:
    - restart httpd
php/handlers/main.yml
---
- name: restart httpd
  service: name=httpd state=restarted
php/vars/main.yml
---
php_log: /var/log/php.log

role入門3 filesを使う

  • php/files/php.ini を用意。
php/tasks/main.yml
- copy: >
    src=php.ini
    dest=/etc/php.ini

こういうふうに書ける。

行の追加(lineinfile)

os/tasks/main.yml
---
- name: CentOS6,7の名前解決高速化
  lineinfile: dest=/etc/resolv.conf line='options single-request-reopen'

- name: kernelが更新されてvirtualbox guest addinsが使えなくなることの対策
  lineinfile: dest=/etc/yum.conf line='exclude=kernel-[23]*'
20
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
21