3. Tasks

3.1. Modules

  • Cloud modules

  • Clustering modules

  • Commands modules

  • Crypto modules

  • Database modules

  • Files modules

  • Identity modules

  • Inventory modules

  • Messaging modules

  • Monitoring modules

  • Net Tools modules

  • Network modules

  • Notification modules

  • Packaging modules

  • Remote Management modules

  • Source Control modules

  • Storage modules

  • System modules

  • Utilities modules

  • Web Infrastructure modules

  • Windows modules

3.2. Install Packages

3.2.1. Single Package

- name: install ntpdate
  package:
    name: ntpdate
    state: present

3.2.2. Install Multiple Packages

- name: install the latest version of Apache and MariaDB
  package:
    name:
      - httpd
      - mariadb-server
    state: latest

3.2.3. Install HTTPd server

  • In some distributions apache is named httpd

Listing 116. This uses a variable as this changes per distribution
- name: remove the apache package
  package:
    name: "{{ apache }}"
    state: absent

3.3. Running Commands

3.3.1. Copy

- name: Copy ansible inventory file to client
  copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts owner=root group=root mode=0644
- name: Copy ansible inventory file to client
  copy:
    src: /etc/ansible/hosts
    dest: /etc/ansible/hosts
    owner: root
    group: root
    mode: 0644
Listing 117. Variables can be used in action lines. Suppose you defined a variable called vhost in the vars section
- name: create a virtual host file for {{ vhost }}
  template:
    src: myfile.j2
    dest: /etc/httpd/conf.d/{{ vhost }}
    owner: root
    group: root
    mode: 0644

3.3.2. Shell

- name: show date
  shell: /bin/date
- name: run this command and ignore the result
  shell: /usr/bin/somecommand || /bin/true
- name: run this command and ignore the result
  shell: /usr/bin/somecommand
  ignore_errors: True

3.3.3. Iptables

- name: allow access from 10.0.0.1
  iptables:
    chain: INPUT
    jump: ACCEPT
    source: 10.0.0.1

3.4. Order

  • inventory - The default. The order is ‘as provided’ by the inventory

  • reverse_inventory - As the name implies, this reverses the order ‘as provided’ by the inventory

  • sorted - Hosts are alphabetically sorted by name

  • reverse_sorted - Hosts are sorted by name in reverse alphabetical order

  • shuffle - Hosts are randomly ordered each run

- hosts: all
  order: sorted
  gather_facts: False
  tasks:
    - debug:
        var: inventory_hostname