LoginSignup
3
7

More than 5 years have passed since last update.

Ansibleの備忘録(モジュール編)

Last updated at Posted at 2018-10-06

※ 適宜更新していきます

モジュールMemo

fileモジュール

モジュール/オプション 説明
file ファイル・ディレクトリの操作
path パスを指定
mode 権限を付与
owner 所有者を指定
group グループを指定
- name: make dirs
  file:
    path: '{{ item }}'
    state: directory
    mode: 0744
    owner: ubuntu
    group: ubuntu
  with_items:
    - /tmp/hoge1
    - /tmp/hoge2
モジュール/オプション 説明
file ファイル・ディレクトリの操作
state ( link ) シンボリックリンク
- name: symlink nginx conf
  file:
    src: /etc/nginx/sites-available/default
    dest: /etc/nginx/sites-enabled/default
    state: link
  become: true

aptパッケージ

モジュール/オプション 説明
apt aptパッケージを利用
state 最新バージョン
update_cache apt-get update後に処理
autoremove 依存関係に対応して削除
become rootユーザで実行
- name: apt-get install
  apt:
    name: "{{ item }}"
    state: latest
    update_cache: yes
    autoremove: yes
  with_items:
    - nginx
    - git
  become: true

apt_repositoryモジュール

  • aptリポジトリを追加する
- name: add apt repo
  apt_repository:
    repo: "{{ item }}"
  with_items:
    - ppa:nginx/stable
  become: true

systemdモジュール

モジュール/オプション 説明
systemd systemdの設定
state(started) サービスを開始
daemon_reload systemdに変更があった場合は起動前にリロードする
enabled OS起動時に自動起動するか
name .serviceを指定する
- name: ensure nginx start
  systemd:
    state: started
    name: nginx.service
    daemon_reload: yes
  become: true

OSのディストリビューションとバージョンについて

モジュール/オプション 説明
debug 実行中にステートメントを出力
with_items loop処理
failed_when 条件を満たさない場合に中止
ansible_distribution ディストリビューションを示す
ansible_distribution_version ディストリビューションバージョンを示す
- name: check distribution and version
  debug:
    var: '{{ item }}'
  with_items:
    - ansible_distribution
    - ansible_distribution_version
  failed_when:
    - ansible_distribution != 'Ubuntu'
    - ansible_distribution_version != '16.04'

wait_forモジュール

  • wait_for
    • pathの場所にファイルが存在するまでtimeout時間待つ
- name: wait until file exit
  wait_for:
    path: /tmp/hoge
    timeout: 60

gemモジュール

  • gem install でパッケージをインストールする
- name: install gem packages
  gem:
    name: "{{ item }}"
    state: latest
  with_items:
    - bundler
    - piculet
    - roadworker

pip3モジュール

  • pip3 install でパッケージをインストールする
- name: install pip3 packages
  pip: name={{ item }} executable=pip3
  with_items:
    - boto3
    - awscli
  become: true

lineinfileモジュール

  • ubuntuユーザがsudoパスワード無しでnginxをreload/restartできるようにする
モジュール/オプション 説明
lineinfile ファイルの中身を操作する
validate %sでコピーするまえに問題ないか確認する
- name: Allow sudo command with nopassword
  lineinfile:
    path: /etc/sudoers
    line: "{{ item }}"
    validate: 'visudo -cf %s'
  with_items:
    - 'ubuntu ALL=NOPASSWD: /bin/systemctl restart nginx.service'
    - 'ubuntu ALL=NOPASSWD: /bin/systemctl reload nginx.service'
  become: true
  • SELinuxを有効にする
- lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: 'SELINUX=enforcing'

gitモジュール

モジュール/オプション 説明
repo リポジトリを指定する
dest clone先のディレクトリを指定する
version ブランチを指定する
- name: download git-secrets
  git:
    repo: https://github.com/awslabs/git-secrets.git
    dest: ~/git-secrets
    version: master

handlerについて

nginxのtaskを使った例
nginxの設定が変更されたら、サービスを再起動したいときに使う

  • nginx/tasks/main.yml
    • notify: "restart nginx"
- name: symlink nginx conf
  file:
    src: /etc/nginx/sites-available/default
    dest: /etc/nginx/sites-enabled/default
    state: link
  become: true
  notify: "restart nginx"
モジュール/オプション 説明
block 複数のタスクをまとめて制御
check_mode(no) dry-run時でも実行する
changed_when 常にステータスokを返す
  • nginx/handlers/main.yml
---
- name: "restart nginx"
  block:
  - name: check config
    command: nginx -t
    check_mode: no
    changed_when: false
  - name: restart nginx
    systemd:
      name: nginx
      state: restarted
      daemon_reload: yes
    become: true
  • TASK[nginx : symlink nginx conf] のステータスが changed になったら実行される。以下はansibleの実行結果
TASK [nginx : symlink nginx conf] ***************************************************************************************************************
--- before
+++ after
@@ -1,4 +1,4 @@
 {
     "path": "/etc/nginx/sites-enabled/default",
-    "state": "absent"
+    "state": "link"
 }

changed: [localhost]

RUNNING HANDLER [nginx : restart nginx] *********************************************************************************************************
changed: [localhost]
3
7
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
3
7