Difference between revisions of "Ansible Conditions"

From rbachwiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 47: Line 47:


</pre>
</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'''
Line 54: Line 56:
==Loops==
==Loops==


<pre.
<pre>
---
---
- name: Install packages thru loop
- name: Install packages thru loop
Line 70: Line 72:
</pre>
</pre>


*




<pre>
<pre>
---
- name: Install packages thru loop
  hosts: localhost
  vars:
  packages: [ftp,telnet,htop) 
  tasks:
- name: Install packages
  yum
    name: ‘{{packages}}’
    state: present
</pre>
---
---
- name: Create users thru loop
- name: Create users thru loop
Line 95: Line 84:
- name: Create users
- name: Create users
   user:
   user:
     name: {{ item }}””
     name: ""{{ item }}""
   loop:
   loop:
       - jerry
       - jerry
       - kramer
       - kramer
       - eliane
       - eliane
</pre>
<pre>
<pre>
</pre>
---
---
- name: Create users thru loop
- name: Create users thru loop
Line 112: Line 102:
- name: Create users
- name: Create users
   user:
   user:
     name: {{item}}
     name: '{{item}}'
   with_items: ‘{{users}}’
   with_items: ‘{{users}}’


<pre>


</pre>
</pre>
<pre>
</pre>


=[[Ansible| Ansible Menu]]=
=[[Ansible| Ansible Menu]]=
[[Category:Ansible]]
[[Category:Ansible]]

Latest revision as of 20:23, 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

Loops

---
- name: Install packages thru loop
  hosts: localhost
  vars:
   packages: [ftp,telnet,htop)  

  tasks:
- name: Install package
  yum
    name: ‘{{items}}’
    state: present
  with_items: ‘{{packages}}’



---
- name: Create users thru loop
  hosts: localhost

  tasks:
- name: Create users
  user:
    name: ""{{ item }}""
  loop:
      - jerry
      - kramer
      - eliane


---
- name: Create users thru loop
  hosts: localhost
  vars:
   users: [jerry,kramer,eliane]  

  tasks:
- name: Create users
  user:
    name: '{{item}}'
  with_items: ‘{{users}}’


Ansible Menu