[Ansible] – How to create conditionals? How to just run a task when and/or if?

Use the attribute when to define that some task can just be executed when some conditional be attended. Following an example.

- name: Run tasks when userid exist
  include: tasks.yml 
  vars: 
    services: 
      - firwalld 
      - httpd
  when: userid is defined
- name: Copy file when the host is a webservice
  copy:
    src: files/index.html
    dest: /var/www/html/index.html
  when: inventory_hostname in groups["webservice"]

Check here the operators and see more about it in the Ansible docs.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s