Skip to content

Instantly share code, notes, and snippets.

@evasilev
Forked from mbodo/Ansible - Cheatsheet.md
Last active August 27, 2020 13:51
Show Gist options
  • Save evasilev/230004b931514ad9bbddd3bb39384445 to your computer and use it in GitHub Desktop.
Save evasilev/230004b931514ad9bbddd3bb39384445 to your computer and use it in GitHub Desktop.
Ansible tips

Ansible - Cheatsheet

ansible-galaxy

Playbook template init with ansible-galaxy

ansible-galaxy init roles/myrole

ansible-playbook

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

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"

Chek role

ansible-playbook foo.yml --check

List task for playbook:

ansible-playbook --list-tasks <role>.yml

e.g
ansible-playbook --list-tasks test.yml

Output:

playbook: test.yml

  play #1 (default): Test playbook      TAGS: []
    tasks:
      debug     TAGS: [subset]
      testrole1/subset2 : Subset2 role task1    TAGS: [subset2]
      testrole1/subset2 : Subset2 role task2    TAGS: [subset2]
      debug     TAGS: [subset3]

List hosts for playbook:

ansible-playbook -i <inventory> --list-hosts <playbook>.yml

e.g
ansible-playbook -i ../development.ini --list-hosts nginx.yml

Output:

playbook: oracle.yml

  play #1 (oracle): Install and configure nginx server        TAGS: []
    pattern: [u'oracle']
    hosts (2):
      vm2
      vm1

ansible

Ad-hoc commands:

ansible -i <inventory> <host/hostgroup> -a <command>

e.g
ansible -i development.ini vm1 -a "date"

Output:

192.XXX.XXX.XXX | SUCCESS | rc=0 >>
Sat Apr  X XX:46:06 BST 20XX

Gather facts from a host:

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

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

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"
}

other

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

Reference links:

Official:

Others:

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