ラベル ansible の投稿を表示しています。 すべての投稿を表示
ラベル ansible の投稿を表示しています。 すべての投稿を表示

2014/12/19

INI-style

構造の単純なテキストファイルであり、設定ファイルの標準です。
このファイルは主にWindowsで使用するが、他のプラットフォームでも使われる。
INIファイルという名前はこのファイルの一般的な拡張子「.INI」から来ている。INIとはinitializationである。
その他に「.CFG」や「.conf」、あるいは「.TXT」も使われる。


パラメータ name=value
セクション  [section]
コメント ; comment text


★ansible lineinfile
This module will search a file for a line, and ensure that it is present or absent.
 This is primarily useful when you want to change a single line in a file only.
 For other cases, see the copy or template modules.

 オプション
 ・backrefs->insertbefore and insertafter will be ignored, and if the regexp doesn't match anywhere in the file,
 the file will be left unchanged.
 If the regexp does match, the last matching line will be replaced by the expanded line parameter.
 ・insertafterーー> the line will be inserted after the specified regular expression

2014/07/17

ansible 特殊 オプション

対象ホスト:
    hosts:
    [test-sever]
    test-test-[1:2][1:6]

テストオプション
★--list-hosts 対象ホスト名をリストするだけ、ほかに何もしない
    ansible test-server -m ** -a "...." -i hosts --list-hosts
    =>
    test-test-11..test-test-16
    test-test-21..test-test-26

★-l されにホストのfilterである!
  ansible test-server -m ** -a "...." -i hosts -l test-test-11 ==>test-test-11だけ対象になる
  ansible test-server -m ** -a "...." -i hosts -l test-test-1* ==>test-test-11~16 対象になる

★-f 一回実行する数
★-C テストモード

2014/06/27

ansible-playbook option

--list-tasks==>タスクの一覧
--check===>オプションを指定することで変更は行わないが、実際に実行するとこうなるという出力がされます
--step ==>one step at a time
--private-key  ==>鍵ファイルを指定して、そしてpasswordの指定がなくなる
--list-hosts
--skip-tags
--syntax-check ==->chechk the fiels and do nothing other
-t tags 指定するタグだけを実行する

2014/05/19

ansible parallelism pull

★
-f==>using a parallelism level of 10
ansible-playbook playbook.yml -f 10

★Ansible-Pull
==>checkout a repo of configuration instructions from git, and then run ansible-playbook against that content.
・it allows you to automatically deploy the latest code in your repository, even if the servers are different
・I find the fact that you don’t have to run a deploy step, other than just committing, completely magical.
==>これはいいの?
・

2014/05/14

linux ansible install err No such file or directory

sudo yum install ansible==>

ansible を実行して、エラーになる。。

  File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory


=====>
Sorry for the confusion, I found this duplicate thread.
Indeed, openssh-clients was missing. Now it looks much better.
Thanks for pointing me to the right direction!
===>openssh-clients!!!

2014/04/03

ansible ループ 条件分岐

・単純ループ:
  name: add several users
 user: name={{ item }} state=present groups=wheel
 with_items:
    - testuser1
    - testuser2


・with_items + 辞書によるループ
 name: add several users
 user: name={{ item.name }} state=present groups={{ item.groups }}
 with_items:
   - { name: 'testuser1', groups: 'wheel' }
   - { name: 'testuser2', groups: 'root' }


・コマンド実行結果を with_items で回すーー>かなり動的に
- name: retrieve the list of home directories
   command: ls /home
   register: home_dirs

 - name: add home dirs to the backup spooler
   file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link
   with_items: home_dirs.stdout_lines
   # same as with_items: home_dirs.stdout.split()
  
  
・with_sequence による整数範囲ループ(Pythonの range のようなもの)
  file: dest=/var/stuff/{{ item }} state=directory
  with_sequence: start=4 end=16 stride=2
  
  
・do-until(+sleep)ループ
 action: shell /usr/bin/foo
 register: result
 until: result.stdout.find("all systems go") != -1
 retries: 5
 delay: 10
 ★★action: shell /usr/bin/foo 
 ==>適切に変換してくれるみたいな。。


・ファイルglobでループ
 copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
 with_fileglob:
   - /playbooks/files/fooapp/*
==>with_fileglob matches all files in a single directory, non-recursively, that match a pattern


・単純な変数による分岐
 - name: "shutdown Debian flavored systems"
   command: /sbin/shutdown -t now
   when: ansible_os_family == "Debian"
  
・コマンド実行結果による分岐
 tasks:
   # まずはコマンドを実行してresultに格納
   - shell: /usr/bin/foo
     register: result
     ignore_errors: True

   # タスクが失敗した場合
   - debug: msg="it failed"
     when: result|failed

   # タスクにより更新された場合
   - debug: msg="it changed"
     when: result|changed

   # タスクが成功した場合
   - debug: msg="it succeeded"
     when: result|success

   # タスクがskipされた場合
   - debug: msg="it was skipped"
     when: result|skipped

   # タスクの標準出力によって分岐
   - shell: echo "hi does not found"
     when: result.stdout.find('hi') != -1

・複数ターゲットが関連するタスクを実行する(Rolling Update)
 - hosts: webservers
   serial: 5

   tasks:
     - name: take out of load balancer pool
       local_action: command /usr/bin/take_out_of_pool {{ inventory_hostname }}

     - name: actual steps would go here
       yum: name=acme-web-stack state=latest

     - name: add back to load balancer pool
       local_action: command: /usr/bin/add_back_to_pool {{ inventory_hostname }}

local_action モジュールはplaybookを実行しているホストで実行するコマンドを定義する。上記の例だとWEBサーバを5並列でLB切り離し→更新→LB組み込みを実行する。

2014/03/24

ansible config

ansible-playbookの方の-i のデフォルト値は???
あれ、設定ファイルは?ハマった。

If installing ansible from a package manager, the latest ansible.cfg should be present in /etc/ansible,
possibly as a ”.rpmnew” file (or other) as appropriate in the case of updates.
If you have installed from pip or from source, however, you may want to create this file in order to override default settings in Ansible.
==>ないので、自分で作るのね。


優先順位:
* ANSIBLE_CONFIG (an environment variable)
* ansible.cfg (in the current directory)
* .ansible.cfg (in the home directory)
* /etc/ansible/ansible.cfg


ansible.cfg:https://raw.github.com/ansible/ansible/devel/examples/ansible.cfg

内容的には
[defaults]
# some basic default values...

hostfile       = /etc/ansible/hosts
==>This is the default location of the inventory file, script,
 or directory that Ansible will use to determine what hosts it has available to talk to:
hostfile = /etc/ansible/hosts
library        = /usr/share/ansible
remote_tmp     = $HOME/.ansible/tmp
pattern        = *

forks          = 5 
=ー>This is the default number of parallel processes to spawn when communicating with remote hosts.
Since Ansible 1.3, the fork number is automatically limited to the number of possible hosts,
so this is really a limit of how much network and CPU load you think you can handle.

poll_interval  = 15
sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
transport      = smart
remote_port    = 22
module_lang    = C

# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
#remote_user = root

2014/02/04

ansible

かなり使い方安い。。。

python製

ーーーーーーーーーーーーーー
例:ansibleのインストール:

# yum install python-devel python-setuptools
# easy_install pip
# pip install ansible
ーーーーーーーーーーーーーー

★設定ファイル
/etc/ansible/ansible.cfg
/etc/ansible/hosts

★環境変数に追加する
$ echo "source /usr/local/src/ansible/hacking/env-setup" >> ~/.bash_profile

★ansible --version
 ansible --v デバッグ
 ansible-doc yum モジュールの説明
 ansible --help

★ansible all -i hosts -a "cat /etc/redhat-release" -k
allーー>すべてのホスト
-i 対象ホストファイル
-m モジュール名, --module-name
-k, --ask-pass
-a 直接コマンドを実行する

★インストール
ansible all -i hosts -m yum -a "name=httpd state=latest" -k

★削除
$ ansible all -m file -a "dest=/home/ansible/foo/ state=absent" -i hosts -k

★mkdir -p と同じで深い階層のディレクトリも一発で作成できました。
$ ansible all -m file -a "dest=/home/ansible/foo/bar/baz state=directory" -i hosts -k

★state に「link」を指定し、src にリンク元ファイルのパスを指定します。
$ ansible all -m file -a "src=/etc/httpd/conf/httpd.conf dest=/home/ansible/httpd.conf state=link" -i hosts -u root -k

★コピー
$ ansible all -m file -a "dest=/home/ansible/test.conf mode=600" -i hosts -k
★他
ansible all -s -i hosts -m command -a "chmod -R 775 /someone_sv"
ansible all -s -i hosts -m command -a "chown -R someone:someone /someone_sv"
ansible all -s -i hosts -m copy -a "src=./log4j.xml dest=/somewhere/log4j.xml"
ansible all -s -i hosts -m cron -a "minute=20 job=/some.sh name='test'"
ansible all -s -i hosts -m copy -a "src=./some.sh dest=/somewhere/some.sh owner=root group=root mode=755"

★Playbook
mysql.yml
 - hosts: all
    user: root
    vars:
        mysql_port: 3306
    handlers:
    - name: restart iptables
      service: name=iptables state=restarted
    tasks:
     - name: install mysql
        yum: name=mysql state=installed
        yum: name=$item state=installed
        with_items:
        - mysql
        - mysql-server
        - mysql-devel
     - name: insert iptables rule
      lineinfile: dest=/etc/sysconfig/iptables state=present regexp="{{ mysql_port }}"
                  insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport {{ mysql_port }} -j ACCEPT"
   
   
    notify: restart iptables
    - include: redis.yml
      vars:
        a: a
        b: b
        c: c
        d: d
       
    ・redis.yml
     - name: install redis packages
       action: yum name=$item state=installed
       with_items:
        - ${libunwind}
        - ${libunwind_devel}
        - ${gperftools_libs}
       - ${redis}

    - name: start redis
    action: service name=redis state=started enabled=yes
                 
変数の参照
$var
${var}
{{ var }}
                 
実行:ansible-playbook mysql.yml -i hosts -k

★handlers
ーー>task の実行により何か変更が発生した場合のみ実行したい処理を定義するために用意されているのが handler です。
例えば、設定ファイルの変更が発生した場合のみ、サービスを再起動したい、といった事を実現するための仕組みです。
handler が実行されるタイミングは対象の task 実行後ではなく、 Playbook の最後になります。
また、複数の task から同じ handler が notify されていた場合でも一度だけしか実行されません。

★Role
include を更に便利にしたような仕組みとして Role というものが用意されています。
Role ではサーバの役割毎にディレクトリを分け、更にその下にセクション毎にディレクトリを分けた構造で Playbook を管理します
要するに、推奨されている規約に従ってディレクトリ構造を作成しておけば自動的にファイルが include される仕組みです。


mysql
├─hosts
├─site.yml
└─roles
    └─mysql
        ├─handlers
        │  └─main.yml
        ├─tasks
        │  └─main.yml
        ├─templates
        │  └─my.cnf.j2
        └─vars
            └─main.yml
roles/x/tasks/main.yml が存在すれば自動的に読み込まれる
roles/x/handlers/main.yml が存在すれば自動的に読み込まれる
roles/x/vars/main.yml が存在すれば自動的に読み込まれる
copy タスクで roles/x/files/ 以下のファイルはパスを指定せずに参照可能
script タスクで roles/x/files/ 以下のファイルはパスを指定せずに参照可能
template タスクで roles/x/templates/ 以下のファイルはパスを指定せずに参照可能


■The command module takes the command name followed by a list of space-delimited arguments.
The given command will be executed on all selected nodes.
It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work
(use the shell module if you need these features).