Ansible Cheatsheet - BETA: Still draft

Ansible Ad-Hoc Comamands

Simple Ad-Hoc Command

ansible all -m ping

Specify specfic host, note ,

ansible all 192.9.200.1, -m ping

Specify inventory file

ansible all -i inventory -m ping

Pass arguments to module

ansible web -m yum -a "name=httpd state=present

Common options

  • -m module name, e.g. yum

  • -i inventory source

    • -i hosts read inventory from file hosts

    • -i inventory/ec2.py execute dynamic inventory script

Ansible Playbook Comamands

Simple A`ansible-playbook` Command

ansible-playbook main.yml

Simple A`ansible-playbook` Command

ansible-playbook main.yml

ansible all 192.9.200.1, -m ping

Specify inventory file

ansible all -i inventory -m ping

Pass arguments to module

home

ansible.cfg files

  • Precedence

    Sample ansible.cfg with callbacks enabled

    [defaults]
    inventory               = inventory
    stdout_callback         = default
    bin_ansible_callbacks   = True
    callback_whitelist      = yaml, timer, profile_tasks
    
    [ssh_connection]
    ssh_args                = -F ./ssh.cfg (1)
    1. All ssh configuration encapsulated in ssh.cfg file

Inventory

ansible-inventory --graph --vars -i my-inventory

ansible-inventory --host appdb1.da4a.internal --vars -i fix-inventory

In-Memory Inventory Examples

A common pattern in cloud, VM, or Container creation is to create the objects (e.g. using say ec2 module) then

  1. Get instance facts

  2. Call add-host with instance facts register

    name: Fetch Instance Info          (1)
      os_server_facts:
       cloud: ospcloud
       region_name: RegionOne
      register: result
    
    - name: Add host                   (2)
      add_host:
        name: "{{ item.public_v4 }}"
        group: "{{ item.metadata.group }}"
      loop: "{{ result.ansible_facts.openstack_servers }}"
Tony Kay avatar
About Tony Kay
Team lead for Automation and Management in Red Hat's GPTE DevOps and Automation team. Working globally, I focus primarily on Hybrid Cloud Automation with Ansible. About 2/3rds of my time is automating production cloud deployments and developing both code and training content. I also deliver Automation training globally to both Red Hat and Partner Consultants and Architects on cloud infrastructure using Ansible and on OpenShift/Kubernetes.
comments powered by Disqus