Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active December 7, 2024 08:50
Show Gist options
  • Save knbknb/f3555b3d88e256b2b25d3a9b907d0def to your computer and use it in GitHub Desktop.
Save knbknb/f3555b3d88e256b2b25d3a9b907d0def to your computer and use it in GitHub Desktop.
ansible-adhoc-commands
# Visualize the gathering phase of Ansible, on a target host.
# Returns a JSON structure with all the facts about the target host
# More than ping, less than a full playbook
ansible -m setup 192.168.178.29
# Filter the output of the setup module to only show facts related to the distribution
ansible -m setup 192.168.178.29 -a "filter=ansible_distribution*"
# a LOT of facts are gathered and put into variables
# this is how to get them back as pure json
ANSIBLE_STDOUT_CALLBACK=json ansible -m setup localhost
# var are available with both underscore notation AND dot.notation
#use Variables in playbooks as
#
- debug:
msg: "SELinux status is {{ ansible_selinux.status }}"
# Shell commands preparing for docker connection plugin
## Not an ad-hoc command,
## when working with docker-based containers managed by Ansible,
## watch images downloaded and containers started on the control node
watch -tn0.5 "docker container ps -a | head -3"
# attach to Docker container containing just the Python interpreter,
# cleanup image after exit
docker container run --name py1 -it --rm python
# run in detached mode, still Ansible can manage the container
docker container run --name py1 -it --detach python
# optional: "bring to foreground" by attaching to the container
# docker container attach py1
# ctrl-p + ctrl-q to detach from the container again
# list inventory given in static.ini
ansible-inventory --list --yaml -i static.ini
# when using the community.docker.docker connection plugin, the filename must end with docker.yml
ansible-inventory --list --yaml -i dynamic-docker.yml --limit "c*"
ansible -m ping all # does not include localhost
# run an additional command on all containers we specified in the inventory file
ansible -m command -a "hostname" all -i static.ini
# List hosts without ansible-inventory
ansible --list-hosts all -i static.ini
# List all inventory plugins
ansible-doc -t inventory community.docker.docker_containers
ansible-doc -t inventory community.general.virtualbox
# show all callback plugins (output formatters)
ansible-doc -t callback --list
# fewer processes spawned, but slower execution
ansible-playbook -i rolling.yml --forks 2
# various ways to check the syntax of a playbook
ansible-lint playbook.yml
ansible-playbook playbook.yml --syntax-check
ansible-playbook playbook.yml --check
ansible-playbook playbook.yml --check --diff --verbose
ansible-playbook -C -v playbook.yml
# generate list of objections from ansible-lint
ansible-lint --generate-ignore
# ... or put a noqa: rule on respective line in playbook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment