InfraLab
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

CommandDescriptionExampleCopy
ansible all -m ping全ホストへ疎通確認する。ansible all -i inventory.ini -m ping
ansible -m shellshell でコマンドを実行する。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 aptDebian 系でパッケージを入れる。ansible debian -m apt -a "name=nginx state=present update_cache=yes" -b
ansible -m yumRHEL 系でパッケージを入れる。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 tasksplaybook の最小構造。- 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 }
varsplaybook 変数を定義する。- 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 → loopAnsible 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 installrole をインストールする。ansible-galaxy role install geerlingguy.nginx
ansible-galaxy collection installcollection をインストールする。ansible-galaxy collection install community.general
requirements.ymlrole/collection 依存をファイル管理する。collections: - name: community.general version: ">=9.0.0" roles: - name: geerlingguy.nginx
role skeletonrole の雛形を作る。ansible-galaxy role init roles/webapp
inventory INIINI inventory の基本。[web] web1 ansible_host=10.0.1.10 web2 ansible_host=10.0.1.11
inventory YAMLYAML 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 inventoryAWS dynamic inventory を使う。plugin: amazon.aws.aws_ec2 regions: [ap-northeast-1] keyed_groups: - key: tags.Role prefix: role
gcp_compute inventoryGCP 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 rekeyvault パスワードを変更する。ansible-vault rekey group_vars/prod/vault.yml
ansible-playbook --check --diffdry-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 --graphinventory 展開結果を確認する。ansible-inventory -i inventory.aws_ec2.yml --graph
molecule testrole を Molecule でテストする。molecule test
ansible-lintplaybook の静的検査を行う。ansible-lint site.yml
Related