Difference between revisions of "Ansible Conditions"

From rbachwiki
Jump to navigation Jump to search
Line 21: Line 21:
</pre>
</pre>


<pre>
---
- name: Enable service on firewalld
  hosts: localhost
  tasks:
- name: Open port for http
  firewalld:
  service: http
  permanent: true
  state: enabled
  notify:
  - Reload firewalld
- name: Ensure firewalld is running
    service:
    name: firewalld
    state: started
handlers:
  - name: Reload firewalld
    service:
    name: firewalld
    state: reloaded
</pre>
<code>ansible_os_family</code> is a built in variable
<code>ansible_os_family</code> is a built in variable
''' you can get a list of all ansible built in variables by running'''
''' you can get a list of all ansible built in variables by running'''

Revision as of 18:48, 14 July 2022

Conditions

When

---
- name: Install Apache WebServer
  hosts: localhost
  
  tasks:
    - name: Install Apache on Ubuntu Server
      apt-get:
        name: apache2
        state: present
      when: ansible_os_family == “Ubuntu“

- name: Install Apache on CentOS  Server
  yum:
    name: httpd
    state: present
  when: ansible_os_family == "RedHat"
---
- name: Enable service on firewalld
  hosts: localhost
  tasks:

- name: Open port for http
  firewalld:
   service: http
   permanent: true
   state: enabled
  notify:
  - Reload firewalld
 
- name: Ensure firewalld is running
    service:
     name: firewalld
     state: started

handlers:
  - name: Reload firewalld
    service:
     name: firewalld
     state: reloaded

ansible_os_family is a built in variable you can get a list of all ansible built in variables by running

ansible localhost -m setup

Ansible Menu