Ansible PlayBook - Task

Card Puncher Data Processing


A task is a call to an ansible module with arguments located in a play list. Variables can be used in arguments to modules.

Tasks are executed top to bottom one at a time, against

all machines

matched by the host pattern before moving on to the next task.

Task should be idempotent in order to be able to re-run the playbook safely. (ie check whether the desired final state has already been achieved, and if that's the case exit without performing any actions).


module: options
# or
action: module options

ie example

    src: templates/foo.j2
    dest: /etc/foo.conf
# or
action: template src=templates/foo.j2 dest=/etc/foo.conf


Omit parameters

- name: touch files with an optional mode
  file: dest={{ item.path }} state=touch mode={{ item.mode | default(omit) }}
    - path: /tmp/foo
    - path: /tmp/bar
    - path: /tmp/baz
      mode: "0444"


See Ansible - Task Status

Import vs Include

See What's the difference between Import vs Include in Ansible?


- command: /opt/application/
  run_once: true


  • run-once - only run a task one time for a batch of hosts
  • delegate_to specify an individual host to execute on


- hosts: webservers 
  remote_user: root 
    - name: First task -  A command execution
      command: /sbin/setenforce 1
      remote_user: yourname
      become: yes
      become_method: sudo # or su
      ignore_errors: True # Ignore the exit code
      vars: # To define system variables
         ansible_become: yes
         ansible_become_method: runas
         ansible_become_user: DOMAIN\user
         ansible_become_pass: Password01
         ansible_become_flags: logon_type=new_credentials logon_flags=netcredentials_only
    - name: template configuration file with var  {{ myVar }} that restart services only if the file change
        src: template.j2
        dest: /etc/httpd/conf.d/{{ myVar }}
        - restart memcached
        - restart apache


Discover More
Card Puncher Data Processing
Ansible - Module

Module are the component that are called via a task modules_by_category With ansible, example running a module...
Card Puncher Data Processing
Ansible - Ansible command line

Ansible define and run a single playbook task against a set of hosts. Ansible “raw” module is a command line for executing commands in a quick and dirty way. The script module don’t even need...
Card Puncher Data Processing
Ansible - Ansible-vault

ansible-vault is a command line utility that permits to add/get sensitive data (file or property value) into an encrypted format called a vault Example of sensitive data: password private keys ...
Card Puncher Data Processing
Ansible - Credential

credential in Ansible Providing credentials can be done through environment variables used for a CI/CD tool such as Ansible Tower or Jenkins file within your home directory, for local development...
Card Puncher Data Processing
Ansible - Function

function in Ansible are tasks that includes other playbook and can pass variable. Include_tasks is done at runtime while import_tasks is done at start time If you have a conditional (such as...
Card Puncher Data Processing
Ansible - Group (Host Properties)

A host can have one or more group (tag). A group may have also have a group. See Groups don’t really survive outside of inventory and host matching because variables are defined to a specific host...
Card Puncher Data Processing
Ansible - Installation - Version 2.7 on Linux

How to install Ansible on Linux. using the OS package manager for Red Hat Enterprise Linux (TM), CentOS, Fedora, Debian, or Ubuntu, we recommend using Pip Ansible’s “raw” module don’t...
Card Puncher Data Processing
Ansible - Playbook

Playbook is the language of Ansible. Ansible modules are the function Playbooks are declarative instruction written in Yaml that run module functions A playbook is a list of play. Playbooks are:...
Card Puncher Data Processing
Ansible - Role

Roles is the reusable unit of Ansible. You may think of it as a function. It: helps make playbook content self-contained works well with things like git submodules for sharing content with others....
Card Puncher Data Processing
Ansible - Tag

A tag permits to filter the execution of tasks special keywords: tagged, - run only tagged untagged - run only untagged all - run all tasks respectively. never - will prevent a task from...

Share this page:
Follow us:
Task Runner