Ansible command loop amend string value and push original value into new array

Ansible command loop amend string value and push original value into new array



Hi I'm using ansible to create some elastic search indexes using laravel's artisan commands. The issue I have is that when I have to create an index I have to use the php class name which the base name is snake cased from camel case e.g. UserConfigurator = user_configurator. In my vars I have the following;


elastic_search:
version: 6.3.2
indexes:
- "App\ElasticSearch\Configurators\UserConfigurator"
- "App\ElasticSearch\Configurators\ClientConfigurator"
models:
- "App\Models\User"
- "App\Models\Client"



and in my playbook I have the following;


- name: check if indexes exist
uri: url='http://localhost:9200/ index_name '
method=HEAD
status_code=200,404

- name: create indexes
command: php artisan elastic:create-index " item "
args:
chdir: "site_root"
with_items: "elastic_search.indexes"



The playbook isn't sufficient enough to do what I want to do due to lack of experience. Any ideas how I may loop over each elastic_search.indexes and convert the class basename to a snake case and check to see if the index exists or not and push them into two separate arrays so then I can use the one of the new variables to create the index and the other variable to update the index?





So what is your expected result?
– techraf
Aug 25 at 18:17




1 Answer
1



Any ideas how I may loop over each elastic_search.indexes and convert the class basename to a snake case



The algorithm I use for snake-casing something is to break the word at an upper-to-lower boundary, lowercase the entire thing, then reassemble the parts, being mindful that the first upper-to-lower transition is special and should not be separated by _.


_



We can leverage the behavior that when set_fact sees a block of text that looks like JSON, it will coerce that block of text into a dict (you'll see that behavior on the command line with ansible -e '"hello": "world"' too), along with the infinitely handy regex_replace jinja2 filter provided by ansible:


set_fact


dict


ansible -e '"hello": "world"'


regex_replace


- hosts: all
gather_facts: no
vars:
class_names:
- 'AppElasticSearchConfiguratorsUserConfigurator'
- 'AppElasticSearchConfiguratorsClientConfigurator'
tasks:
- set_fact:
index_names: >-
[
% for cn in class_names -%
'' if loop.first else ','

regex_replace("([A-Z])([a-z])", "x0012")
% endfor %
]
with_items: ' class_names '
- debug: var=index_names verbosity=0
- debug: var=item verbosity=0
with_items: ' index_names '



which produces the correct output:


TASK [debug] *******************************************************************
ok: [localhost] => (item=user_configurator) =>
"item": "user_configurator"

ok: [localhost] => (item=client_configurator) =>
"item": "client_configurator"



and now you can use those indices in a command of your choosing.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế