Cheat Sheet

Useful Ansible commands.

Hosts

Host from variable

Playbook file:

- name: Your playbook name
  hosts: "{{ variable_host | default('localhost') }}"
  become: yes

To run it:

ansible-playbook playbooks/your-playbook-name.yml \
  --extra-vars "variable_host=PUT-YOUR-HOST-HERE"

Ping

Ping all hosts

ansible all -m ping -u root

Ping localhost

ansible all -m shell -a "ping -c 3 localhost" -u root

Python virtualenv

Make sure python will load all modules installed in your virtualenv.

ansible-playbook -v \
  playbooks/my-playbook.yaml \
  --extra-vars="ansible_python_interpreter=$(which python)"

Set RSA key path

ansible all -m ping -u root --private-key=~/.ssh/id_rsa

Install packages

ansible all -m apt -a "pkg=nginx state=latest update_cache=true" -u root

Decrypt

Decrypt string

Make sure your string variable is in group_vars/all.yml.

cat group_vars/all.yml

For example:

aws_access_key_id: !vault | $ANSIBLE_VAULT;1.1;AES256 39393635326661316162323939656138666562303765666138333737366139306535303432653537 3036323230616163343135393736343462346464646438320a373234383634646130666333316132 65623561393832666131656365646437643139626532623962646439376364333066323934663661 3031326632385365630a643162643562633231653132393139333737353837633931663764373239 64666537386530366564643336346630666530373664633630323730643261613563

Then run:

ansible YOUR-HOST-HERE -m debug -a 'var=aws_access_key_id'

Last updated