ANSIBLE40 cmd
Ansible チート集
Ansible の ad-hoc、playbook、roles/collections、inventory、group_vars、vault、Jinja2、check/diff、Molecule テストを実務運用向けに整理したチート集。
- 更新日
- 2026-05-14
- 参照範囲
- 公式ドキュメント / man page / 主要ベンダーCLI
- 対象実装
- 主要 Linux / BSD / ネットワーク機器 CLI の一般的な実装
- 免責
- OS とバージョン差分は実環境で確認してください。
40 / 40
| Command | Description | Example | Copy |
|---|---|---|---|
ansible all -m ping | 全ホストへ疎通確認する。 | ansible all -i inventory.ini -m ping | |
ansible -m shell | shell でコマンドを実行する。 | ansible web -i inventory.ini -m shell -a "uptime" | |
ansible -m copy | ファイルを配布する。 | ansible web -m copy -a "src=nginx.conf dest=/etc/nginx/nginx.conf mode=0644" -b | |
ansible -m service | サービスを操作する。 | ansible web -m service -a "name=nginx state=reloaded" -b | |
ansible -m apt | Debian 系でパッケージを入れる。 | ansible debian -m apt -a "name=nginx state=present update_cache=yes" -b | |
ansible -m yum | RHEL 系でパッケージを入れる。 | ansible rhel -m yum -a "name=httpd state=present" -b | |
ansible -m dnf | 新しめの RHEL 系で dnf を使う。 | ansible fedora -m dnf -a "name=haproxy state=latest" -b | |
playbook hosts tasks | playbook の最小構造。 | - hosts: web
become: true
tasks:
- name: install nginx
ansible.builtin.apt:
name: nginx
state: present | |
handlers notify | 変更時だけ handler を呼ぶ。 | 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 変数を定義する。 | - hosts: web
vars:
app_port: 3000
tasks:
- debug: var=app_port | |
pre_tasks post_tasks | 前処理・後処理を定義する。 | 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 を実行する。 | - name: restart nginx on Debian
service: { name: nginx, state: restarted }
when: ansible_facts.os_family == "Debian" | |
loop | 複数値をループする。 | - user:
name: "{{ item }}"
state: present
loop: [alice, bob] | |
with_items → loop | Ansible 2.5 で追加された推奨ループ記法。with_items は旧来記法として残るが、新規 playbook は loop を使う。 | - package:
name: "{{ item }}"
state: present
loop:
- curl
- jq | |
jinja default | 未定義時の既定値を使う。 | listen_port: "{{ app_port | default(3000) }}" | |
jinja to_yaml | 変数を YAML 化して出力する。 | {{ app_config | to_yaml }} | |
jinja regex_replace | 正規表現で値を置換する。 | {{ inventory_hostname | regex_replace("\.example\.com$", "") }} | |
jinja selectattr | 属性で list を絞る。 | {{ users | selectattr("enabled", "equalto", true) | list }} | |
ansible-galaxy role install | role をインストールする。 | ansible-galaxy role install geerlingguy.nginx | |
ansible-galaxy collection install | collection をインストールする。 | ansible-galaxy collection install community.general | |
requirements.yml | role/collection 依存をファイル管理する。 | collections:
- name: community.general
version: ">=9.0.0"
roles:
- name: geerlingguy.nginx | |
role skeleton | role の雛形を作る。 | ansible-galaxy role init roles/webapp | |
inventory INI | INI inventory の基本。 | [web]
web1 ansible_host=10.0.1.10
web2 ansible_host=10.0.1.11 | |
inventory YAML | YAML inventory の基本。 | all:
children:
web:
hosts:
web1:
ansible_host: 10.0.1.10 | |
group_vars | グループ変数を配置する。 | group_vars/web.yml:
app_port: 3000
nginx_worker_processes: auto | |
host_vars | ホスト固有変数を配置する。 | host_vars/web1.yml:
ansible_host: 10.0.1.10
drain_weight: 10 | |
aws_ec2 inventory | AWS dynamic inventory を使う。 | plugin: amazon.aws.aws_ec2
regions: [ap-northeast-1]
keyed_groups:
- key: tags.Role
prefix: role | |
gcp_compute inventory | GCP dynamic inventory を使う。 | plugin: google.cloud.gcp_compute
projects: [prod-project]
auth_kind: serviceaccount
service_account_file: ./sa.json | |
ansible-vault create | 暗号化ファイルを作成する。 | ansible-vault create group_vars/prod/vault.yml | |
ansible-vault edit | 暗号化ファイルを編集する。 | ansible-vault edit group_vars/prod/vault.yml | |
ansible-vault encrypt | 既存ファイルを暗号化する。 | ansible-vault encrypt secrets.yml | |
ansible-vault decrypt | 暗号化ファイルを復号する。 | ansible-vault decrypt secrets.yml | |
ansible-vault rekey | vault パスワードを変更する。 | ansible-vault rekey group_vars/prod/vault.yml | |
ansible-playbook --check --diff | dry-run と差分確認を行う。 | ansible-playbook -i inventory.ini site.yml --check --diff | |
-vvv | 詳細ログを出す。 | ansible-playbook -i inventory.ini site.yml -vvv | |
--tags | タグを指定して一部だけ実行する。 | ansible-playbook site.yml --tags nginx,deploy | |
--start-at-task | 途中の task から再開する。 | ansible-playbook site.yml --start-at-task "deploy app" | |
ansible-inventory --graph | inventory 展開結果を確認する。 | ansible-inventory -i inventory.aws_ec2.yml --graph | |
molecule test | role を Molecule でテストする。 | molecule test | |
ansible-lint | playbook の静的検査を行う。 | ansible-lint site.yml |