1
2

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 3 years have passed since last update.

Ansible 逆引き

Last updated at Posted at 2021-08-14

環境・前提条件

対象 バージョン
Host OS Windows10
Client OS Centos-7.8
Ansible (ansible_local) Version 2.9.24

ファイル・ディレクトリ操作

ダウンロード get_url
- name: Download Something
  get_url:
    url: [URL_FOR_DOWNLOAD]
    dest: [PATH_OF_DEST]

# Example
- name: Download Oracle preinstall rpm package
  get_url:
    url: https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm
    dest: /tmp
インストール yum
- name: Install Something
  yum:
    name: [PATH_OF_INSTALL_PACKAGE]
    state: present

# Example
- name: Install Oracle Preinstallation
  yum:
    name: /tmp/oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm
    state: present
ファイルコピー(ホスト to クライアント) copy
- name: Copy Something
  copy:
    src: [PATH_OF_SOURCE_ON_HOST]
    dest: [PATH_OF_DEST_ON_CLIENT]
    mode: [PERMISSION_MODE]

# Example
- name: Copy file for Oracle installation
  copy:
    src: ./installer/oracle-database-ee-19c-1.0-1.x86_64.rpm
    dest: /tmp/oracle-database-ee-19c-1.0-1.x86_64.rpm
    mode: 0644
ディレクトリ作成 file (state: directory)
- name: Make directory
  file:
    path: [PATH_OF_DIRECTORY]
    state: directory
    owner: [OWNER]
    group: [GROPU]
    mode: [PERMISSION_MODE]

# Example
- name: Make ansible setup status directory
  file:
    path: "/ansible_setup_status"
    state: directory
    owner: "root"
    group: "root"
    mode: 0755
空ファイル作成 file (state: touch)
- name: Create empty file
  file: 
    path: [PATH_OF_FILE]
    state: touch

# Example
- name: Put file to indicate completed configure Oracle DB
  file: 
    path: "/ansible_setup_status/configured_oracle_db"
    state: touch
ファイル存在確認
- name: Check the file if exist
  stat:
    path: [PATH_OF_FILE]
  register: [REGISTER_VARIABLE]

# Example
- name: "check 'configured_oracle_db' if exist"
  stat:
    path: "/ansible_setup_status/configured_oracle_db"
  register: configured_oracle_db_exist

ファイル編集

ファイル末尾に複数行を追記 lineinfile
- name: Add lines to end of file
  ansible.builtin.lineinfile:
    path: [PATH_OF_FILE]
    state: present
    line: "{{ item }}"
  with_items:
  - 'line1'
  - 'line2'
  - 'line3'

# Example
- name: bash_profile of oracle user
  ansible.builtin.lineinfile:
    path: /home/oracle/.bash_profile
    state: present
    line: "{{ item }}"
  with_items:
  - 'export ORACLE_SID=ORCLCDB'
  - 'export ORACLE_BASE=/opt/oracle'
  - 'export ORACLE_HOME=$ORACLE_BASE/product/19c/dbhome_1'
  - 'export PATH=$PATH:$ORACLE_HOME/bin'
ファイル内文字列置換 replace
- name: Replace line in file
  replace:
    path: [PATH_OF_FILE]
    regexp: [TARGET_CHARCTER]
    replace: [REPLACE_CHARCTER]

# Example
- name: Replace a localhost ip address
  replace:
    path: /etc/hosts
    regexp: '^127.0.0.1'
    replace: '192.168.60.50'

Shell 実行

実行 cmd
- name: Execute command
  shell:
    cmd: [COMMAND]

# Example
- name: Configure Oracle DB
  shell:
    cmd: /etc/init.d/oracledb_ORCLCDB-19c configure
環境変数を宣言しつつ実行 environment
- name: Execute command with environment variable
  shell:
    cmd: [COMMAND]
  environment:
    [ENV1]: [VALUE1]
    [ENV2]: [VALUE2]
    [ENV3]: [VALUE3]

# Example
- name: Configure Oracle DB
  shell:
    cmd: /etc/init.d/oracledb_ORCLCDB-19c configure
  environment:
    ORACLE_BASE: /opt/oracle
    ORACLE_HOME: /opt/oracle/product/19c/dbhome_1
    ORACLE_SID: ORCLCDB
ユーザを指定して実行 become
- name: Execute with specified user
  shell:
    cmd: [COMMAND]
  become: yes
  become_user: [USER]

# Example
- name: Install Golden Gate
  shell:
    cmd: /home/oracle/work/gg_inst/fbo_ggs_Linux_x64_shiphome/Disk1/runInstaller -silent -nowait -responseFile /home/oracle/work/gg_inst/fbo_ggs_Linux_x64_shiphome/Disk1/response/oggcore.rsp
  become: yes
  become_user: oracle
失敗を無視する実行 ignore_errors
- name: Execute command and ignore errors
  shell:
    cmd: [COMMAND]
  ignore_errors: yes

# Example
- name: Configure Oracle DB
  shell:
    cmd: /etc/init.d/oracledb_ORCLCDB-19c configure
  ignore_errors: yes
条件に基づき実行 when
- name: Execute depends on condition
  shell:
    cmd: [COMMAND]
  when: [CONDITION]

# Example(ファイルの存在を確認してから実行)
- name: "check 'configured_oracle_db' if exist"
  stat:
    path: "/ansible_setup_status/configured_oracle_db"
  register: configured_oracle_db_exist

- name: Configure Oracle DB
  shell:
    cmd: /etc/init.d/oracledb_ORCLCDB-19c configure
  when: not configured_oracle_db_exist.stat.exists
warning を出さない warn: false
- name: Not show warning
  shell:
    cmd: [COMMAND]
  args:
    warn: false

# Example
- name: Unzip Oracle Golden Gate installation file
  shell:
    cmd: unzip -u /home/oracle/work/gg_inst/191004_fbo_ggs_Linux_x64_shiphome.zip -d /home/oracle/work/gg_inst/
  args:
    warn: false

タスク調整・デバッグ

特定の時間待つ pause

例) 5秒待つ場合

- name: pause
  pause:
    seconds: 5
register に登録された内容を確認 debug
- name: pause
  pause:
    seconds: 5
  register: result
- name: show result
  debug:
    var: result
1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?