1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ansibleでよく使うモジュールとその使いかた

Last updated at Posted at 2019-07-02

shell

・chdirでshellコマンドを実行するdirに移動
・sudo: noでsudoをつけないで実行

install.yml
- shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/
    creates: somelog.txt
    sudo: no

・lock_timeoutで時間を指定し、「yum lockfile is held by another process」を解消する

install.yml
- name: configure / Import mysql Repository
  yum:
    lock_timeout: 60
    name: http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
    update_cache: yes

・urlを指定してインストール

install.yml
- name: configure / Import EPEL Repository
  yum:
    name: https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    state: latest
    update_cache: yes

git

・gitからレポジトリを取得
・destでインストール先を取得

install.yml
- name: install / Download phalcon-devtools
  git:
    repo: https://github.com/phalcon/phalcon-devtools.git
    dest: /var/www/phalcon-devtools

systemctl

起動、再起動など

install.yml
- name: configure / reStart nginx service
  systemd:
    name: nginx
    state: reloaded
    enabled: yes

テンプレートをコピー

install.yml
- name: configure / Setup configure file
  template:
    src: server.cnf.j2
    dest: /etc/my.cnf.d/server.cnf
    owner: root
    group: root
    mode: 0644
    backup: yes

yum

アンインストール

install.yml
- name: check_install / Remove current packages
  yum:
    name: mariadb-libs
    state: absent

firewallの開閉

install.yml
- name: check_install / Allow ports for firewalld
  firewalld:
    port: "{{ item }}"
    permanent: yes
    immediate: yes
    state: enabled
  with_items: "{{ nginx_firewalld_port }}"

ディレクトリの作成

install.yml

- name: mkdir /var/www
  file: path=/var/www
        state=directory
        owner=root
        group=root
        mode=0755

ホストのファイルをターゲットへコピー

install.yml
- name: configure / make index.php
  copy:
    src: ../templates/index.php
    dest: /var/www
    mode: 0755

debug

ディレクトリやファイルの中身確認

install.yml

- name: debug dir exists
  debug:
    msg: "{{ dir }}"

stat

ディレクトリやファイルの有無で処理を実行するか分岐

install.yml
# registerで名前をつけ、「名前.stat.exists」で分岐
- name: check dir rbenv
  stat:
    path: "path/to/.rbenv"
  register: rbenv

# register: rbenvで登録したものがない場合だけ実行
# ある時だけ実行は  when: rbenv.stat.exists == True
- name: install rbenv
  git:
    repo: https://github.com/rbenv/rbenv.git
    dest: "{{ home_dir }}/.rbenv"
  become: no
  when: rbenv.stat.exists == False

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?