Ansible Commands - keshavbaweja-git/guides GitHub Wiki

Pre requisities

  • ansible.cfg
[defaults]
inventory = hosts
remote_user = <remote-username>
private_key_file = <path-to-private-key>
  • hosts
  • site.yml
    • A Playbook is an ordered list of plays.
    • A Play is an ordered list of tasks associated with an unordered set of hosts.
    • A task is defined in terms of a single module.
Description Command
Ping servers ansible <host-group-name> -m ping
Check server uptime ansible <host-group-name> -m command -a uptime
Pass command line args to playbook ansible-playbook <pb.yml> --extra-vars "arg1=value1 arg2=value2"
Pass command line args to playbook ansible-playbook <pb.yml> -e "arg1=value1 arg2=value2"
Pass command line args from a file to playbook ansible-playbook <pb.yml> -e @vars.yml
Run tasks associated with a tag ansible-playbook <pb.yml> --tags "tag1"
Run tasks not associated with a tag ansible-playbook <pb.yml> --skip-tags "tag1"
View values of a variable - debug: var=<var_name>
Host specification - union of two groups hosts: group1:group2
Host specification - intersection of two groups hosts: group1:&group2
Host specification - exclusion hosts: group1:!group2
Host specification - command line ansible-playbook -l 'group1:&group2' pb.yml
Viewing all facts ansible <server> -m setup
Viewing a subset of facts ansible <server/host_group> -m setup -a 'filter=ansible_eth*'

Associating facts with a host

Place /etc/ansible/facts.d on the host. Access these facts as variable ansible_local in playbook.

Built in variables

hostvars

A dictionary that contains all of the variables defined on all the hosts, keyed by the hostname as known to ansible.

inventory_hostname

Hostname of the current host as known to ansible.

groups

Capture output of a command

- name: capture output
  command: whoami
  register: result
- debug: var=result

Value of a registered variable is always a dictionary.

Using command output in a task

- name: capture output of id command
  command: id -un
  register: login
- debug: "Logged in as users {{ login.stdout }}"

Unarchive

  - name: Copy and unarchive installation
    unarchive:
      src: "{{ installation_file }}"
      dest: "{{ destination_folder}}"

To avoid overwrites on reruns, add a parameter "creates" with value set to path of one of the file created by unarchive action. If that file exists, unarchive won't overwrite.

Create directory

    file:
      path: "{{ destination_folder }}"
      state: "directory"

Create symbolic link

  - name: Create symbolic link
    file:
      src: "<path_to>"
      path: "<symbolic_link>"
      state: link

Display current host name

    debug:
      msg: "{{ansible_fqdn}}"

Run a task against a specific host group

  - name: Stop spark slave
    when: inventory_hostname in groups['spark-slave']
    command: "{{ spark_home }}/sbin/stop-slave.sh"
    tags:
    - stop-slave

Execute a task conditionally

  - name: Create "current" symbolic link
    file:
      src: "{{ base_folder }}/{{ version }}"
      path: "{{ base_folder }}/current"
      state: link
    when: do_not_update_current is undefined

If the above is executed as ansible-playbook <pb.yml> -e "do_not_update_current=''", the task above will not be executed

Delete folders older than 30 days

  tasks:
  - name: Display current host name
    debug:
      msg: "{{ ansible_fqdn }}"

  - name: Folders older than 30 days
    find:
      paths: "/a/b/c"
      age: 30d
      file_type: directory
    register: "folders_to_delete"

  - name: Display folders
    debug:
      var: "folders_to_delete"

  - name: Remove folders
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ folders_to_delete.files }}"

Running Strategies

  • Linear (default) - Ansible completes execution of a task on all hosts before it proceeds with execution of next task on any host.
  • Free - Ansible initiates execution of next task on a host as soon as the previous task completes on that host.
⚠️ **GitHub.com Fallback** ⚠️