InfraLab
ANSIBLE40 cmd

Ansible チート集

Ansible の ad-hoc、playbook、roles/collections、inventory、group_vars、vault、Jinja2、check/diff、Molecule テストを実務運用向けに整理したチート集。

更新日
2026-05-29
参照範囲
公式ドキュメント / man page / 主要ベンダーCLI
対象実装
主要 Linux / BSD / ネットワーク機器 CLI の一般的な実装
免責
OS とバージョン差分は実環境で確認してください。

このチートシートの使いどころ

Ansible チート集は、Ansible の ad-hoc、playbook、roles/collections、inventory、group_vars、vault、Jinja2、check/diff、Molecule テストを実務運用向けに整理したチート集。 対象は公式ドキュメント、man page、主要ベンダー CLI で確認できる現行の一般的な実装です。カテゴリはad-hoc、playbook、roles-collections、vault、inventoryを中心に、40件のコマンドや値を用途別に引けます。障害調査、設定変更前の確認、作業メモ作成で、Ansible、playbook、inventory、ansible-vault、ansible-galaxyに関連する操作を短時間で探すために使います。

40 / 40

ansible all -m ping

全ホストへ疎通確認する。

Example
ansible all -i inventory.ini -m ping
ansible -m shell

shell でコマンドを実行する。

Example
ansible web -i inventory.ini -m shell -a "uptime"
ansible -m copy

ファイルを配布する。

Example
ansible web -m copy -a "src=nginx.conf dest=/etc/nginx/nginx.conf mode=0644" -b
ansible -m service

サービスを操作する。

Example
ansible web -m service -a "name=nginx state=reloaded" -b
ansible -m apt

Debian 系でパッケージを入れる。

Example
ansible debian -m apt -a "name=nginx state=present update_cache=yes" -b
ansible -m yum

RHEL 系でパッケージを入れる。

Example
ansible rhel -m yum -a "name=httpd state=present" -b
ansible -m dnf

新しめの RHEL 系で dnf を使う。

Example
ansible fedora -m dnf -a "name=haproxy state=latest" -b
playbook hosts tasks

playbook の最小構造。

Example
- hosts: web become: true tasks: - name: install nginx ansible.builtin.apt: name: nginx state: present
handlers notify

変更時だけ handler を呼ぶ。

Example
tasks: - template: { src: nginx.conf.j2, dest: /etc/nginx/nginx.conf } notify: reload nginx handlers: - name: reload nginx service: { name: nginx, state: reloaded }
vars

playbook 変数を定義する。

Example
- hosts: web vars: app_port: 3000 tasks: - debug: var=app_port
pre_tasks post_tasks

前処理・後処理を定義する。

Example
pre_tasks: - name: drain from lb shell: /usr/local/bin/drain {{ inventory_hostname }} post_tasks: - name: enable lb shell: /usr/local/bin/enable {{ inventory_hostname }}
when

条件付きで task を実行する。

Example
- name: restart nginx on Debian service: { name: nginx, state: restarted } when: ansible_facts.os_family == "Debian"
loop

複数値をループする。

Example
- user: name: "{{ item }}" state: present loop: [alice, bob]
with_items → loop

Ansible 2.5 で追加された推奨ループ記法。with_items は旧来記法として残るが、新規 playbook は loop を使う。

Example
- package: name: "{{ item }}" state: present loop: - curl - jq
jinja default

未定義時の既定値を使う。

Example
listen_port: "{{ app_port | default(3000) }}"
jinja to_yaml

変数を YAML 化して出力する。

Example
{{ app_config | to_yaml }}
jinja regex_replace

正規表現で値を置換する。

Example
{{ inventory_hostname | regex_replace("\.example\.com$", "") }}
jinja selectattr

属性で list を絞る。

Example
{{ users | selectattr("enabled", "equalto", true) | list }}
ansible-galaxy role install

role をインストールする。

Example
ansible-galaxy role install geerlingguy.nginx
ansible-galaxy collection install

collection をインストールする。

Example
ansible-galaxy collection install community.general
requirements.yml

role/collection 依存をファイル管理する。

Example
collections: - name: community.general version: ">=9.0.0" roles: - name: geerlingguy.nginx
role skeleton

role の雛形を作る。

Example
ansible-galaxy role init roles/webapp
inventory INI

INI inventory の基本。

Example
[web] web1 ansible_host=10.0.1.10 web2 ansible_host=10.0.1.11
inventory YAML

YAML inventory の基本。

Example
all: children: web: hosts: web1: ansible_host: 10.0.1.10
group_vars

グループ変数を配置する。

Example
group_vars/web.yml: app_port: 3000 nginx_worker_processes: auto
host_vars

ホスト固有変数を配置する。

Example
host_vars/web1.yml: ansible_host: 10.0.1.10 drain_weight: 10
aws_ec2 inventory

AWS dynamic inventory を使う。

Example
plugin: amazon.aws.aws_ec2 regions: [ap-northeast-1] keyed_groups: - key: tags.Role prefix: role
gcp_compute inventory

GCP dynamic inventory を使う。

Example
plugin: google.cloud.gcp_compute projects: [prod-project] auth_kind: serviceaccount service_account_file: ./sa.json
ansible-vault create

暗号化ファイルを作成する。

Example
ansible-vault create group_vars/prod/vault.yml
ansible-vault edit

暗号化ファイルを編集する。

Example
ansible-vault edit group_vars/prod/vault.yml
ansible-vault encrypt

既存ファイルを暗号化する。

Example
ansible-vault encrypt secrets.yml
ansible-vault decrypt

暗号化ファイルを復号する。

Example
ansible-vault decrypt secrets.yml
ansible-vault rekey

vault パスワードを変更する。

Example
ansible-vault rekey group_vars/prod/vault.yml
ansible-playbook --check --diff

dry-run と差分確認を行う。

Example
ansible-playbook -i inventory.ini site.yml --check --diff
-vvv

詳細ログを出す。

Example
ansible-playbook -i inventory.ini site.yml -vvv
--tags

タグを指定して一部だけ実行する。

Example
ansible-playbook site.yml --tags nginx,deploy
--start-at-task

途中の task から再開する。

Example
ansible-playbook site.yml --start-at-task "deploy app"
ansible-inventory --graph

inventory 展開結果を確認する。

Example
ansible-inventory -i inventory.aws_ec2.yml --graph
molecule test

role を Molecule でテストする。

Example
molecule test
ansible-lint

playbook の静的検査を行う。

Example
ansible-lint site.yml
Related