Skip to content

Instantly share code, notes, and snippets.

@mbodo
Forked from aleixripoll/ansible-tips.md
Last active March 11, 2019 15:35
Show Gist options
  • Save mbodo/b107bdfd3160c9cfc76db5c33d6f53b2 to your computer and use it in GitHub Desktop.
Save mbodo/b107bdfd3160c9cfc76db5c33d6f53b2 to your computer and use it in GitHub Desktop.
Ansible tips

Ansible - Cheatsheet

Playbook template init with ansible-galaxy

ansible-galaxy init roles/myrole

Playbook useful switches:

ansible-playbook -C -D -i <inventory> [-u <ssh-user>] [-t tag1,tag2] [--list-hosts] [-l host1,host2] play.yml

-C check_mode
-D show file diffs

Ad-hoc commands:

ansible -i <inventory> <host/hostgroup> -a <command>
EXAMPLE: ansible -i inventory/beta tag_Role_api_socket -a "date"

Gather facts from a host:

ansible -m setup -i <inventory> <host>

e.g
ansible -m setup -i development.ini vm1

Output:

 "ansible_selinux": {
            "config_mode": "enforcing",
            "mode": "enforcing",
            "policyvers": 28,
            "status": "enabled",
            "type": "targeted"
        },
        "ansible_service_mgr": "systemd",
        ...

Ping host:

ansible -m ping -i <inventory> <host>

e.g
ansible -m ping -i development.ini vm1

Output:

192.XXX.XXX.XXX | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Search for pattern in all encrypted vault files:

for f in $(grep -rl '$ANSIBLE_VAULT;1.1'); do echo $f:; ansible-vault view $f | grep <pattern>; done

Intersect conditions in host limit option

ansible-playbook -i inventory/ec2.py meta.yml --list-hosts -l "tag_Name_a:&tag_Role_b:&tag_Environment_c"

Reference links:

Official:

Others:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment