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
    name: ntpdate
    state: present

3.2.2. Install Multiple Packages

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

3.2.3. Install HTTPd server

  • In some distributions apache is named httpd

Code 3.16. This uses a variable as this changes per distribution
- name: remove the apache 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
    src: /etc/ansible/hosts
    dest: /etc/ansible/hosts
    owner: root
    group: root
    mode: 0644
Code 3.17. 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 }}
    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
    chain: INPUT
    jump: ACCEPT

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
    - debug:
        var: inventory_hostname