Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - using with_items and when conditional to

Tags:

ansible

I have a bunch of servers that have four physical drives on them (/dev/sda, sdb, sdc, and sdd). sda has the OS installed on it.

I need to format each drive except sda. I need to check if each drive has data on it. If it does, then I shouldn't format it.

# This will get all physical disks (sda, sdb, sdc, etc) and assign them to disk_var - name: Get disks   set_fact: disk_var="{{hostvars[inventory_hostname]["ansible_devices"].keys()|list}}"  - name: Check if the disk is partitioned and also ignore sda   stat: path=/dev/{{item}}1   with_items: disk_var   when: item != 'sda'   register: base_secondary_partition_{{item}}  - name: Create GPT partition table   command: /sbin/parted -s /dev/{{item}} mklabel gpt   with_items: disk_var   when: item != 'sda' and base_secondary_partition_{{item}}.stat.exists == false 

There's clearly more steps involved into formatting these drives but it fails at the last task when creating the GPT partition table.

Here's what it looks like when it runs. You'll see that it fails at the last task:

TASK: [role | Get disks] ****************************************************** ok: [server1.com]  TASK: [role | Check if the disk is partitioned] ******************************* skipping: [server1.com] => (item=sda) ok: [server1.com] => (item=sdd) ok: [server1.com] => (item=sdb) ok: [server1.com] => (item=sdc)  TASK: [role | Create GPT partition table] ************************************* fatal: [server1.com] => error while evaluating conditional: base_secondary_partition_sdd.stat.exists == false  FATAL: all hosts have already failed -- aborting 

Any idea how I can check the conditional base_secondary_partition_{{item}}.stat.exists? I need to make sure that if there's data on the drive, it will not format it.

like image 479
hfranco Avatar asked Aug 25 '15 21:08

hfranco


People also ask

Can we define multiple conditions in Ansible?

Defining multiple when conditions in Ansible I want to reboot Debian or Ubuntu Linux system after kernel update, and the inventory hostname must be aws-proxy-server . If both conditions are true, then issue the reboot command using the Ansible reboot module. Otherwise, skip the reboot option.

What is the difference between loop and With_items in Ansible?

The with_ keywords rely on Lookup Plugins - even items is a lookup. The loop keyword is equivalent to with_list, and is the best choice for simple loops. The loop keyword will not accept a string as input, see Ensuring list input for loop: query vs. lookup.

Does Ansible do until loop?

To use this loop in task you essentially need to add 3 arguments to your task arguments: until - condition that must be met for loop to stop. That is Ansible will continue executing the task until expression used here evaluates to true. retry - specifies how many times we want to run the task before Ansible gives up.

How to use conditional statements in Ansible?

You can also use conditional statements in Ansible playbooks where there is a mix of different variables and each representing different entities. Ansible has many useful parameters that can be used in tasks to specify a condition or pre-requisite to execute this task.

What is the use of list in Ansible with_items?

Ansible with_items is a keyword which you will use in playbook and provide a list of items under it. These are some scenarios when you have a simple list, an item is list is also a list, each item in list is a combination of few variables. 1. A simple list will be like below and used in a task as follows. 2.

How to define Ansible when conditions in the Ansible playbook with multiple tasks?

Let’s begin this tutorial by defining Ansible when conditions in the Ansible playbook with multiple tasks. 1. Open a terminal on the Ansible controller host. 2. Run the commands below to create a directory and name it anything you prefer in your home directory, and navigate to that directory.

How do I create a YAML file for Ansible when conditions?

In your preferred code editor, create a YAML file in the ~/ansible_when_condition_demo directory. In this example, the file is called my_playbook.yml. Copy and paste the YAML playbook contents below to the my_playbook.yml file. In both tasks below ( Task-1 and Task-2 ), the when conditions check which operating system every remote host is on.


1 Answers

You do not need to register your result with the item salt. When you register the result of a loop (e.g. with_items) the registered value will contain a key results which holds a list of all results of the loop. (See docs)

Instead of looping over your original device list, you can loop over the registered results of the first task then:

- name: Check if the disk is partitioned and also ignore sda   stat: path=/dev/{{item}}1   with_items: disk_var   when: item != 'sda'   register: device_stat  - name: Create GPT partition table   command: /sbin/parted -s /dev/{{ item.item }} mklabel gpt   with_items: "{{ device_stat.results }}"   when:     - not item | skipped     - item.stat.exists == false 

The condition not item | skipped takes care of that elements which have been filtered in the original loop (sda) will not be processed.

While that might be a solution to your problem, your question is very interesting. There seems to be no eval feature in Jinja2. While you can concatenate strings you can not use that string as a variable name to get to its value...

like image 123
udondan Avatar answered Oct 06 '22 14:10

udondan