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. 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
  5. Excellent content!

    Data Science Course in Hyderabad

    Quality Thought offers a comprehensive Data Scientist Course in Hyderabad designed to equip learners with the skills required for today's data-driven industries. The curriculum includes Python programming, Statistics, Probability, SQL, Data Analysis, Data Visualization, Machine Learning, Deep Learning, Artificial Intelligence, Natural Language Processing (NLP), Feature Engineering, Model Evaluation, and predictive analytics.

    Students gain practical experience by working on real-time datasets, case studies, and industry-oriented projects using popular tools and libraries such as Pandas, NumPy, Matplotlib, Scikit-learn, TensorFlow, and Jupyter Notebook. The course emphasizes hands-on learning, enabling learners to build, evaluate, and deploy machine learning models while developing analytical and problem-solving skills.

    Whether you are a beginner, graduate, software professional, or career changer, this training helps you prepare for Data Science roles through expert guidance, project-based learning, resume building, mock interviews, certification guidance, and placement assistance.

    Quality Thought offers flexible classroom and online training with weekday and weekend batches. Learn more about the course syllabus, fees, and upcoming batches by visiting https://qualitythought.in/data-science-training/. For admissions, call 07337344490 or visit 6th Floor, Nilgiri Block, ADITYA ENCLAVE, 601B, Ameerpet, Hyderabad, Telangana 500016.

    ReplyDelete

Post a Comment