Run an Ansible task only when the variable contains a specific string

Active3 hr before
Viewed126 times

7 Answers


means - failed only when the previously registered variable "promtool_version" doesn't contains string '1.5.2'.,if variable1 is an array or dict instead, in will search for the exact string as one of its items.,In ansible 2.9 this is deprecated in favor of using ~ concatenation for variable replacement:,Thanks for contributing an answer to Stack Overflow!

If variable1 is a string, and you are searching for a substring in it, this should work:

when: '"value" in variable1'
load more v

The string content of a registered variable can be empty. If you want to run another task only on hosts where the stdout of your registered variable is empty, check the registered variable’s string contents for emptiness:,Create a conditional test based on the registered variable.,Conditions based on registered variables,If a fact or variable is a string, and you need to run a mathematical comparison on it, use a filter to ensure that Ansible reads the value as an integer:

   -name: Configure SELinux to start mysql on any port
   name: mysql_connect_any
state: true
persistent: yes
when: ansible_selinux.status == "enabled"
# all variables can be used directly in conditionals without double curly braces
load more v

We can also make a conditional statement based on whether the variable contains a particular string. We can use variable.find for checking the contents. In the following example, the task will only run when the variable test1 contains the string “World”.,Note: We need not use the “{{ }}” in the when statement for the variable. It is by default considered as a variable.,If the variable value is registered from a shell command you may have to use stdout.find to check the content.,Variables are necessary when you need to store the output of a task and need to access it in some other tasks. They are mostly used when dealing with a conditional statement where you can opt to run the task based on the output of another task. Here I will show some examples of using variable substitution in conditionals.

Note: We need not use the “{{ }}” in the when statement for the variable. It is by default considered as a variable.

-hosts: all
   test1: "Hello World"
   -name: Ansible when variable equals example
   msg: "Equals"
when: test1 == "Hello World"
load more v

To match strings against a substring or a regex, use the “match” or “search” filter,Please be sure to answer the question. Provide details and share your research!,Connect and share knowledge within a single location that is structured and easy to search., Check whether a lowercase alphabet-only string has any character repetition

In your case:

when: ec2_tag_Name is match("testhost.*")
load more v

The Playbook to search a string using ansible lineinfile,Search for a String in a file using Ansible lineinfile moduleThe Playbook to search a string using ansible lineinfile,When the string is present the first task will be changed it will eventually trigger the second task, which supposed to run only when the string exists in the file.,Search for a String in a file using the Shell module grep commandThe Playbook

The Playbook to search a string using ansible lineinfile

-- -
-name: Search String with lineinfile
hosts: web

   -name: "Searching for a String"
become: yes
become_user: root
tags: example1
   path: /etc/httpd / conf / httpd.conf
line: "LogLevel debug"
state: present
check_mode: yes
register: presence
failed_when: presence is changed

   name: "sample task in case the String present in the file"
debug: msg = "DEBUG is enabled in Apache Log file"
when: presence is not changed
load more v

The ansible-examples github repository contains many examples of how variables are used in Ansible.,Other useful magic variables refer to the current play or playbook, including:,Examples of where to set a variable,As shown elsewhere in the docs, it is possible for one server to reference variables about another, like so:

   field1: one
field2: two
load more v

Example of playbook task explicitly referencing a VNF instance specific json file and loading the contents as global variables:,Create VNF inventory hosts file with all VMs and OA&M IP addresses, and VM or VNFC names as required for the VNF type, grouped by VM/VNFC type. Add site with all groups as children. Inventory hosts file are required for all VNF instances, to be configured and managed through Ansible. Inventory hosts file example:,VNF inventory hosts file names include the VNF instance name and are now created under base inventory directory to preserve properties of (global) inventory/group_vars files with variables, example, site specific attributes for DNS, NTP, etc.,Virtual IP addresses that can be used by multiple VMs, usually, used by the active VM of an active-standby pair, are placed under a group named after the VNFC (VM) type, plus “vip” string, example of such a group name “oamvip”. An inventory hosts file site also contains a (group) with all groups as children (see last four lines in above example), to load site specific variables like NTP, DNS IP addresses, and other site specific variables, making them global variables to be used by playbooks, namely, configure playbook.

$ pwd
$ ansible-playbook -i ../inventory/vfdb9904vhosts site.yml --extra-vars "vnf_instance=vfdb9904v"

NOTE: To preserve Ansible inventory/group_vars capability, that makes
group_vars contents global variables available to all playbooks, when they
reside in the inventory directory, guidelines were updated to name the
VNF inventory hosts file as (a flat file) <VNFName>hosts, stored in the
   inventory directory, not a subdirectory under inventory.
load more v

Other "undefined-undefined" queries related to "Run an Ansible task only when the variable contains a specific string"