Ansible closer look task control



Simple loops :-

this will iterate from a list of items for a task.

backend concept:-

It uses  with_items as loop starter and data is stored in  item  


Demo 1:  


-bash-4.3$ cat loops/simple.yml
---
 - hosts: localhost
   become: true
   tasks:
    - name: starting service
      service:
       name: "{{ item }}"
       state: restarted
       enabled: yes
      with_items:
       - httpd
       - sshd


Using internal variables :


 Demo 2: 


-bash-4.3$ cat  loops/simple1.yml ---
 - hosts: localhost
   become: true
   vars:
    x:
     - httpd
     - sshd
   tasks:
    - name: starting service
      service:
       name: "{{ item }}"
       state: restarted
       enabled: yes
      with_items: "{{ x }}"
 


Using  external variables : 


demo 3: 


-bash-4.3$ cat loops/simple2.yml ---
 - hosts: localhost
   become: true
   tasks:
    - name: including variable
      include_vars: vars/var.yml
    - name: starting service
      service:
       name: "{{ item }}"
       state: restarted
       enabled: yes
      with_items: "{{ x }}"
 

-bash-4.3$ cat  loops/vars/var.yml x:
 - httpd
 - sshd


Hash oriented or Key value based simple loop:


Demo :

-bash-4.3$ cat  loops/keyval.yml
---
 - hosts: localhost
   become: true
   tasks:
    - name: adding users and assigining them in a group
      user:
       name: "{{ item.name }}"
       state: present
       groups:  "{{ item.groups }}"
      with_items:
       - {name: 'raj', groups: 'root'}
       - {name: 'roahn', groups: 'wheel'}
       - {name: 'rajesh', groups: 'bin'}
 


Time for nested loops 

-bash-4.3$ cat  loops/nested.yml ---
 - hosts: localhost
   become: true
   vars:
    x:
     - key1
     - key2
     - key3

   tasks:
    - name: adding in line
      lineinfile: dest=/tmp/{{ item[0] }}/a.txt line={{ item[1] }} state=present
      with_nested:
       - ['user1','user2','user3']
       - "{{ x }}"
 


Here : 
 all 3 keys are added to each user file 


Demo 2:

-bash-4.3$ cat loops/nestedbug.yml ---
- name: Demo of with_nested
  hosts: localhost
  become: true

  vars:
    list1: [1, 2, 3]
    list2: [a, b, c, d]

  tasks:
    - name: Printing message
      debug: msg="Values are {{item[0]}} and {{item[1]}}"
      with_nested:
        - list1
        - list2
 

ANSIble tasks with condition :

 As in normal programming language we are using we are using when it comes to project with ansible it also check the same thing :

Demo: 

-bash-4.3$ cat condition/when1.yml ---
 - hosts: localhost
   become: true
   vars:
    x: true

   tasks:
    - name: pring message
      debug: msg="hello world this is ansible"
      when: x

 

Comments

  1. Hey thanks for this amazing post! Thank you so much for sharing the good post, I appreciate your hard work.Keep blogging.
    DevOps Training in Electronic City

    ReplyDelete
  2. This information really helped me a lot. It was very informative.
    Devops Services

    ReplyDelete
  3. Thanks for sharing such a great post. It is very useful and informative. Valuable information you have shared. Also, check out
    Devops Services

    ReplyDelete
  4. Very well written!

    Azure Data Engineer

    Build a successful cloud career with Microsoft Azure Data Engineer Training at Quality Thought.
    Learn Azure Data Factory, Synapse, Databricks, SQL, Spark and more with projects, placement
    support.
    Course URL: https://qualitythought.in/azure-data-engineer-training/

    Contact: 9121188426

    Address: 3rd Floor, Metro Station Ameerpet, ADITYA ENCLAVE, 303, Behind Ameerpet,
    Ameerpet, Hyderabad, Telangana 500016.

    ReplyDelete
  5. Insightful article!
    Quantum Computing Program

    Quality Thought's Quantum Computing Training in Hyderabad prepares learners for emerging
    careers with comprehensive coverage of quantum concepts, programming, algorithms and
    real-world projects. Includes flexible training, certification and placement assistance.

    Course URL:
    https://qualitythought.in/quantum-computing-training/

    Contact: 08106961963.

    Training locations:
    3rd Floor, Metro Station Ameerpet, ADITYA ENCLAVE, 303, Behind Ameerpet, Hyderabad 500016
    and Nilgiri Block, ADITYA ENCLAVE, 303B, Ameerpet, Hyderabad 500016.

    ReplyDelete

Post a Comment