LoginSignup
0
5

More than 5 years have passed since last update.

Ansible ノウハウ

Last updated at Posted at 2018-03-15

概要

・ansible で始めるインフラ自動化
https://www.slideshare.net/dcubeio/ansible-72056386

設定方法

1:ansibleクライアント、サーバ共に下記実施。
$ sudo apt-get update
$ sudo apt-get install -y python-dev python-pip
$ sudo pip install ansible

2:ansibleサーバ側で、ssh用のコンフィグ設定。クライアント側は、AzureでVM作る時に、公開鍵設定済。これで、ssh IPアドレス でログイン可能。

.ssh/config
Host プライベートIPアドレス
    HostName プライベートIPアドレス
    User ログインユーザ名
    IdentityFile 秘密鍵のpath

3:ansibleサーバ側のanbible.cfgに、秘密鍵のpathを設定(private_key_file)
3をやるなら、2は不要。

4:ansibleサーバ側の、/etc/hostsにvncとxrdpのIPアドレス設定

5:inventoryにhostsの設定を反映
inventoryには、hostsの名前を設定すること。IPアドレスは、inventoryには書いてはいけない。

gather factなし
become user

qiita書き方

https://qiita.com/hiroyuki_hon/items/f2a779bb295fd12646ab
https://qiita.com/Qiita/items/c686397e4a0f4f11683d

よくまとまってるからあとでみる

モジュール

tags

でかいplaybookがある場合に、部分的に実行するために、tagを指定しておく。tag指定しておけば、こんな感じでいける。
http://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html?highlight=include_tasks

$ ansible-playbook example.yml --tags "configuration,packages"

sample.yml
tasks:

    - yum:
        name: "{{ item }}"
        state: installed
      loop:
         - httpd
         - memcached
      tags:
         - packages

    - template:
        src: templates/src.j2
        dest: /etc/foo.conf
      tags:
         - configuration

when

条件を作って、実行制御するためのパラメータ。
http://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement

sample.yml
- name: send channel setting for restapi
  become: false
  synchronize:
    src: channel/
    dest: ~/fabric-performance-verification.git/fabricRestTool/artifacts/channel/
    delete: yes
  when: "'web1' in inventory_hostname"

command

リモートホストでコマンドを実行する。
http://docs.ansible.com/ansible/latest/modules/command_module.html?highlight=command

sample.yml
- name: install n
  command: make -C /tmp/n

shell

リモートホストにシェルを実行させる。commandとの違いがわからない。どっちでも同じことができるのは確かだと思う。becomeも含めてこれはモジュール?という疑問があるが、とりあえず下記のように指定すれば動くのでは、詳細はこれ以上進めて詰まったらやるで良い。chdirは実行ディレクトリ。

sample.yml
- name: build cryptogen/configtxgen script
  become: true
  shell: ./bootstrap-1.0.5.sh
  args:
    chdir: "{{ ansible_env.PWD }}/fabric/scripts"

ignore_errors

playbookは失敗すると止まるが、失敗しても止めたくないときに指定するパラメータ?
http://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html?highlight=ignore_errors

ruby.sample.yml
- name: mode old data
  shell: mv /root/sar.data.{{ inventory_hostname }} /root/sar.data.{{ inventory_hostname }}.`date '+%Y%m%d-%H%M%S'`
  ignore_errors: true

file

ファイルやディレクトリやシンボリックリンクのパラメータをセットする。copyとかでもできるっぽい。

sample.yml
- name: change owner/group ~/fabric/scripts/bin
  become: true
  file:
    path: "{{ ansible_env.PWD }}/fabric/scripts/bin"
    state: directory
    owner: "{{ ansible_env.USER }}"
    group: "{{ ansible_env.USER }}"

copy

リモートホストにファイルを転送する。copyするファイルは、「files」ディレクトリの中に配置しておく。
http://docs.ansible.com/ansible/latest/modules/copy_module.html?highlight=copy

sample.yml
    - copy:
        src: /etc/hosts
        dest: /etc/hosts
        owner: root
        group: root
        mode: 0644

varialbes

http://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variables
Variables Defined in a Playbook In a playbook, it’s possible to define variables directly inline like so

sample.yml
- hosts: webservers
  vars:
    http_port: 80

変数の指定。

template

テンプレートファイル(jinja2)を展開したファイルをリモートホストへ転送する。ホストのtemplatesディレクトリにあるsrcのファイルをテンプレートとして、リモートのdestにファイルを生成する
http://docs.ansible.com/ansible/latest/modules/template_module.html?highlight=templat

sample.yml
- name: send docker-compose.yml
  template:
    src: docker-compose.yml
    dest: ~/docker-compose.yml

synchronize

rsyncのラッパー。rsyncほど有能なコマンドではないが、ある程度使える。copyとの違いがいまいちわからない。後続の処理に影響している?
http://docs.ansible.com/ansible/latest/modules/synchronize_module.html?highlight=synchronize

ruby.yml
- name: send channel settings
  synchronize:
    src: channel/
    dest: /root/channel/
    delete: yes

include_tasks

現在のplaybookで実行されるタスクのリスト。
http://docs.ansible.com/ansible/latest/modules/include_tasks_module.html?highlight=include_tasks

main.yml
- include_tasks: install.yml
  tags:
    - install

service

リモートホストのサービスを管理する。
http://docs.ansible.com/ansible/latest/modules/service_module.html?highlight=service

sample.yml
- name: restart docker
  service:
    name: docker
    state: restarted

systemd

リモートホストのsystemdサービスを管理する。serviceでも出来るような気がするが。。。
http://docs.ansible.com/ansible/latest/modules/systemd_module.html?highlight=systemd

ruby.install.yml
- name: service disabled
  systemd:
    name: sysstat
    state: stopped
    enabled: false

apt

APTパッケージのマネジメントを実施する。
http://docs.ansible.com/ansible/latest/modules/apt_module.html?highlight=apt

sample.yml
- name: Install apt docker
  apt:
    name: "{{ item }}"
    update_cache: yes
    force: yes
  with_items:
    - docker-engine 
    - python-setuptools
    - apt-utils
    - python-pip

with itemsは、 "{{ item }}"の部分に代入されてaptで繰り返しインストールしてくれる。

apt_repository

UbuntuとDebianのAPT(Advanced Package Tool)のレポジトリを追加、削除する。
http://docs.ansible.com/ansible/latest/modules/apt_repository_module.html?highlight=apt_repository

sample.yml
- name: Add apt-repogitory for docker
  apt_repository:
    repo: "deb https://apt.dockerproject.org/repo ubuntu-xenial main"
    update_cache: yes

pip

依存関係にあるPythonのライブラリの管理。
http://docs.ansible.com/ansible/latest/modules/pip_module.html?highlight=pip

sample.yml
- name: pip install
  pip:
    name: "{{ item }}"
  with_items:
    - docker-compose
    - docker
    - dockerpty
    - PyYAML
  ignore_errors: yes

git

gitのレポジトリの管理。destのディレクトリの中身に、repoで指定した箇所のファイルが入る。
http://docs.ansible.com/ansible/latest/modules/git_module.html?highlight=git

sample.yml
- name: download n
  git: 
    repo: "{{ n_repo }}"
    dest: /tmp/n

docker image

Dockerのimagesを、Build, load or pullする。
http://docs.ansible.com/ansible/latest/modules/docker_image_module.html?highlight=docker_image

sample.yml
- name: pull an image
  docker_image:
    name: hyperledger/fabric-ca:x86_64-1.0.5

NameはたぶんDockerHubにあるイメージの名前をそのまま指定する。
Tagでつけるバージョンについても、dockerhubのhyperledger/fabric-caのページにある、tagsのページに記載がある。

ホストにある、compose-ymlのテンプレートのファイル名とディレクトリの場所を指定して、コントロールされる側に送る。

docker_service

docker composeファイルを使った、startしたりshutdownしたり、servicesをscaleしたりする。とりあえず、ディレクトリ指定して、docker-compose.ymlを実行するものだという理解でよい。
http://docs.ansible.com/ansible/latest/modules/docker_service_module.html?highlight=docker_service

sample.yml
- name: Deploy the CA using docker compose
  docker_service:
    project_src: ~/
    files: docker-compose.yml
0
5
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
0
5