[ansible-project] Ansible 2.13 yum module - requires minimum version of target python of 2.7 or 3.5. But I already have that

2022-08-05 Thread Daniel Barros
 

I have an Ansible (2.13) machine targeting a remote centOS 6 with Python 
3.6.

Remote machine has python3.6 installed and variables are set like this:


$ ansible-inventory --host centos-6-vm

{

"ansible_private_key_file": "~/.ssh/id_rsa",

"ansible_python_interpreter": "/usr/bin/python3.6",

"ansible_user": "daniel"

}

Ansible can ping, setup and gather facts from target successfully.

But when trying to run the yum module, I get an error as if my server 
doesn't have Python 3.6

This is my playbook

---

- name: Deploy Services

  hosts: centos-6-vm

  gather_facts: true

 

  tasks:

 

- name: Show python interpreter

  debug:

var: "{{ item }}"

  with_items:

- ansible_python_interpreter

- ansible_python_version

 

- name: Patch

  become: true

  yum:

name: "*"

security: true

state: latest

update_cache: true

The first task is just to confirm that Ansible is recognizing Python 
versions. And the result is success.

ok: [centos-6-vm] => (item=ansible_python_interpreter) => {

"ansible_loop_var": "item",

"ansible_python_interpreter": "/usr/bin/python3.6",

"item": "ansible_python_interpreter"

}

ok: [centos-6-vm] => (item=ansible_python_version) => {

"ansible_loop_var": "item",

"ansible_python_version": "3.6.6",

"item": "ansible_python_version"

}

But when it gets in the module, it gets an error saying no Python3.

TASK [Patch] 
***

fatal: [centos-6-vm]: FAILED! => {"changed": false, "msg": "ansible-core 
requires a minimum of Python2 version 2.7 or Python3 version 3.5. Current 
version: 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red 
Hat 4.4.7-17)]"}

Any idea how can I approach that?

 

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3176afe0-d6fc-4e63-a4f8-94c20d3a7b16n%40googlegroups.com.


Re: [ansible-project] Re: Pull of List in a Variable File as Extra-Vars

2022-08-05 Thread wpgpo...@gmail.com
Hello Vladimir,

It does works successfully. Many thanks and highly appreciated.

This thread is RESOLVED.

Thanks... WP

On Thursday, August 4, 2022 at 7:35:34 PM UTC-5 vbo...@gmail.com wrote:

> > On Wednesday, August 3, 2022 at 5:02:16 PM UTC-4 wpgpo...@gmail.com 
> wrote:
> > > *general_var.yml*
> > > main_dir: "{{ app_name }}-{{ ansible_hostname }}-{{ ver }}"
>
> Use *vars* lookup plugin to indirectly reference the value of the
> variable stored in *app_name*. Then use filter *product* to create
> the combinations. For example, given
>
> * the extra variable '-e app_name=dir_list'
> * the variable *dir_list*
> shell> cat dirname.yml
> dir_list:
> - hello
> - goodbye
> - take-care
> * the inventory
> shell> cat hosts
> host01 ansible_hostname=01.us.com
> * the variable 'ver=123'
>
> The variable *main_dir*
>
> shell> cat general_var.yml
> main_dir: "{{ lookup('vars', app_name)|
> product([ansible_hostname])|
> product([ver])|
> map('flatten')|
> map('join','-')|list }}"
>
> gives the list you want (simplified)
>
> main_dir:
> - hello-01.us.com-123
> - goodbye-01.us.com-123
> - take-care-01.us.com-123
>
> Test the iteration
>
> - debug:
> msg: "copy {{ item.0 }} to {{ item.1 }}"
> with_nested:
> - "{{ patch_lib }}"
> - "{{ main_dir }}"
>
> gives (abridged)
>
> msg: copy lib_1 to hello-01.us.com-123
> msg: copy lib_1 to goodbye-01.us.com-123
> msg: copy lib_1 to take-care-01.us.com-123
> msg: copy lib_2 to hello-01.us.com-123
> msg: copy lib_2 to goodbye-01.us.com-123
> msg: copy lib_2 to take-care-01.us.com-123
>
> Fit the details to your needs.
>
> -- 
> Vladimir Botka
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/798676cd-5c65-4125-bfa8-9e47c6fca688n%40googlegroups.com.


[ansible-project] 'include_vars' doesn't find the vars file

2022-08-05 Thread dulhaver via Ansible Project
I want to include a defaults, or variable file in the /defaults/main.yml of a 
role I am playing
Upon execution I am getting "the defaults/main.yml file for role 
'postgres_disable' must contain a dictionary of variables"

== DETAILED ERROR 

ansible-playbook [core 2.12.2]
  config file = 
/home/gwagner/repos/ansible/open_source/postgres_upgrade/ansible.cfg
  configured module search path = ['/home/gwagner/.ansible/plugins/modules', 
'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  ansible collection location = 
/home/gwagner/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 
(Red Hat 8.5.0-3)]
  jinja version = 2.10.3
  libyaml = True
Using /home/gwagner/repos/ansible/open_source/postgres_upgrade/ansible.cfg as 
config file
host_list declined parsing /etc/ansible/hosts as it did not pass its 
verify_file() method
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() 
method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() 
method
Parsed /etc/ansible/hosts inventory source with ini plugin
[WARNING]: provided hosts list is empty, only localhost is available. Note that 
the implicit localhost does not match 'all'
ERROR! The defaults/main.yml file for role 'postgres_disable' must contain a 
dictionary of variables
==


I tried all 3 variants of addressing the file in the TASK below seprately. Each 
of them throws the same error

= my TASK 

- name: include defaults file
  ansible.builtin.include_vars:
file: "{{ item }}"
  loop:
- ../vars/pg_disable_defaults.yml
- 
/home/gwagner/repos/ansible/open_source/postgres_upgrade/roles/postgres_disable/vars/pg_disable_defaults.yml
- pg_disable_defaults.yml
==

= my FOLDER STRUCTURE 


roles/postgres_disable/
├── defaults
│   └── main.yml
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── main.yml
│   └── postgres_disable.yml
├── tests
│   ├── inventory
│   └── test.yml
└── vars
├── main.yml
└── pg_disable_defaults.yml
==

the pg_disable_defaults.yml works as soon I move it to ~/defaults/main.yml, so 
the file itself does not appear to be the source of bad here.

Seems I am fundamentally getting something wrong here ... what is it?

Maybe the idea of a starting point in 'role_name/defaults/main.yml'?

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/1271492684.42080.1659717388175%40office.mailbox.org.


Re: [ansible-project] Lineinfile - Skip/Update an item inside "with_items" using conditions

2022-08-05 Thread farrukh ahmed
Thanks @utoddl for your time and reply.

when:  item.line is not search(omit)

This solution actually worked for me.

Thanks & regards,

FARRUKH
On Friday, August 5, 2022 at 5:22:03 PM UTC+5 uto...@gmail.com wrote:

> That "when:" should be
>
> when: item.line is not search(omit)
>
> On Friday, August 5, 2022 at 8:03:34 AM UTC-4 Todd Lewis wrote:
>
>> - name: "Update Redis Parameters in {{ redis_config_path }}"
>>   lineinfile:
>> path: "{{ redis_config_path }}"
>> backrefs: yes
>> regexp: "{{ item.regexp }}"
>> line: "{{ item.line }}"
>>   when: item.line is match(omit)
>>   with_items:
>> - { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM | d(omit) 
>> }}mb" }
>> - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE 
>> | d(omit) }}" }
>> - { regexp: '^tcp-backlog',   line: "tcp-backlog {{ TCP_BACKLOG | 
>> d(omit) }}" }
>> - { regexp: '^maxclients',line: "maxclients {{ MAX_CLIENTS | 
>> d(omit) }}" }
>> - { regexp: '^timeout',   line: "timeout {{ TIMEOUT }}" }
>>
>>
>>
>> On 8/5/22 7:31 AM, farrukh ahmed wrote:
>>
>> Thanks for your reply Vladimir Botka.
>>
>> This is my actual case: 
>>
>>  defaults > main.yml
>> ---
>> # defaults file for redis_configs
>> redis_config_path: /etc/redis/redis.conf
>>
>> # Default Params Values
>> MAX_MEM: 3000
>> TCP_KEEPALIVE: 0
>> TCP_BACKLOG: 511
>> MAX_CLIENTS: 15000
>> TIMEOUT: 1500
>>
>>
>> And this is: 
>>
>> Tasks > main.yml
>>
>> ---
>>
>> - name: "Update Redis Parameters in {{ redis_config_path }}"
>>   lineinfile:
>> path: "{{ redis_config_path }}"
>>   backrefs: yes
>>   regexp: "{{ item.regexp }}"
>>   line: "{{ item.line }}"
>>   with_items:
>> - { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM }}mb", when: {{ 
>> MAX_MEM }} is defined } 
>> - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE 
>> }}", when: {{ TCP_KEEPALIVE }} is defined }
>> - { regexp: '^tcp-backlog', line: "tcp-backlog {{ TCP_BACKLOG }}", 
>> when: {{ TCP_BACKLOG }} is defined}
>> - { regexp: '^maxclients', line: "maxclients {{ MAX_CLIENTS }}", 
>> when: {{ MAX_CLIENTS }} is defined}
>> - { regexp: '^timeout', line: "timeout {{ TIMEOUT }}" } 
>>
>>
>> I want this to check if each variable inside an item is defined then make 
>> the changes to the line in the file, otherwise skip only those items which 
>> variables is not defined.
>> For Instance, If I comment out the variable MAX_MEM from the defaults > 
>> main.yml. like below:
>>
>> # Default Params Values
>> ##MAX_MEM: 3000
>> TCP_KEEPALIVE: 0
>> TCP_BACKLOG: 511
>> MAX_CLIENTS: 15000
>> TIMEOUT: 1500  
>>
>> Then the execution should skip changes to the line where MAX_MEM is 
>> undefined, and the rest lines should be changed as they defined.
>>
>> Is it possible?
>>
>>
>> On Friday, August 5, 2022 at 2:18:05 PM UTC+5 vbo...@gmail.com wrote:
>>
>>> On Thu, 4 Aug 2022 23:43:59 -0700 (PDT) 
>>> farrukh ahmed  wrote: 
>>>
>>> > with_items: 
>>> > - { regexp: '^text1', line: "text1 = {{ VAR1 }}", when: VAR1 is 
>>> defined } 
>>> > - { regexp: '^text2', line: "text2 = {{ VAR2 }}" } 
>>> > - { regexp: '^text3', line: "text3 = {{ VAR3 }}" } 
>>>
>>> Set *default* to avoid errors, add attribute *def*, and select lines. 
>>> For example, 
>>>
>>> shell> cat pb.yml 
>>> --- 
>>> - hosts: localhost 
>>> gather_facts: false 
>>> vars: 
>>> lines: 
>>> - regexp: '^text1' 
>>> line: 'text1 = {{ VAR1|d("undef") }}' 
>>> def: '{{ VAR1 is defined }}' 
>>> - regexp: '^text2' 
>>> line: 'text2 = {{ VAR2|d("undef") }}' 
>>> def: '{{ VAR2 is defined }}' 
>>> - regexp: '^text3' 
>>> line: 'text3 = {{ VAR3|d("undef") }}' 
>>> def: '{{ VAR3 is defined }}' 
>>> tasks: 
>>> - debug: 
>>> var: item 
>>> loop: "{{ lines|selectattr('def') }}" 
>>>
>>> gives 
>>>
>>> shell> ansible-playbook pb.yml -e VAR1=test 
>>>
>>> PLAY [localhost] 
>>> * 
>>>
>>> TASK [debug] 
>>> * 
>>> ok: [localhost] => (item={'regexp': '^text1', 'line': 'text1 = test', 
>>> 'def': True}) => ansible_loop_var: item item: 
>>> def: true 
>>> line: text1 = test 
>>> regexp: ^text1 
>>>
>>> -- 
>>> Vladimir Botka 
>>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2cf96c01-f0fc-4ed5-afdb-4329530ae06en%40googlegroups.com.


Re: [ansible-project] Lineinfile - Skip/Update an item inside "with_items" using conditions

2022-08-05 Thread Todd Lewis
That "when:" should be

when: item.line is not search(omit)

On Friday, August 5, 2022 at 8:03:34 AM UTC-4 Todd Lewis wrote:

> - name: "Update Redis Parameters in {{ redis_config_path }}"
>   lineinfile:
> path: "{{ redis_config_path }}"
> backrefs: yes
> regexp: "{{ item.regexp }}"
> line: "{{ item.line }}"
>   when: item.line is match(omit)
>   with_items:
> - { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM | d(omit) 
> }}mb" }
> - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE | 
> d(omit) }}" }
> - { regexp: '^tcp-backlog',   line: "tcp-backlog {{ TCP_BACKLOG | 
> d(omit) }}" }
> - { regexp: '^maxclients',line: "maxclients {{ MAX_CLIENTS | 
> d(omit) }}" }
> - { regexp: '^timeout',   line: "timeout {{ TIMEOUT }}" }
>
>
>
> On 8/5/22 7:31 AM, farrukh ahmed wrote:
>
> Thanks for your reply Vladimir Botka.
>
> This is my actual case: 
>
>  defaults > main.yml
> ---
> # defaults file for redis_configs
> redis_config_path: /etc/redis/redis.conf
>
> # Default Params Values
> MAX_MEM: 3000
> TCP_KEEPALIVE: 0
> TCP_BACKLOG: 511
> MAX_CLIENTS: 15000
> TIMEOUT: 1500
>
>
> And this is: 
>
> Tasks > main.yml
>
> ---
>
> - name: "Update Redis Parameters in {{ redis_config_path }}"
>   lineinfile:
> path: "{{ redis_config_path }}"
>   backrefs: yes
>   regexp: "{{ item.regexp }}"
>   line: "{{ item.line }}"
>   with_items:
> - { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM }}mb", when: {{ 
> MAX_MEM }} is defined } 
> - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE 
> }}", when: {{ TCP_KEEPALIVE }} is defined }
> - { regexp: '^tcp-backlog', line: "tcp-backlog {{ TCP_BACKLOG }}", 
> when: {{ TCP_BACKLOG }} is defined}
> - { regexp: '^maxclients', line: "maxclients {{ MAX_CLIENTS }}", when: 
> {{ MAX_CLIENTS }} is defined}
> - { regexp: '^timeout', line: "timeout {{ TIMEOUT }}" } 
>
>
> I want this to check if each variable inside an item is defined then make 
> the changes to the line in the file, otherwise skip only those items which 
> variables is not defined.
> For Instance, If I comment out the variable MAX_MEM from the defaults > 
> main.yml. like below:
>
> # Default Params Values
> ##MAX_MEM: 3000
> TCP_KEEPALIVE: 0
> TCP_BACKLOG: 511
> MAX_CLIENTS: 15000
> TIMEOUT: 1500  
>
> Then the execution should skip changes to the line where MAX_MEM is 
> undefined, and the rest lines should be changed as they defined.
>
> Is it possible?
>
>
> On Friday, August 5, 2022 at 2:18:05 PM UTC+5 vbo...@gmail.com wrote:
>
>> On Thu, 4 Aug 2022 23:43:59 -0700 (PDT) 
>> farrukh ahmed  wrote: 
>>
>> > with_items: 
>> > - { regexp: '^text1', line: "text1 = {{ VAR1 }}", when: VAR1 is defined 
>> } 
>> > - { regexp: '^text2', line: "text2 = {{ VAR2 }}" } 
>> > - { regexp: '^text3', line: "text3 = {{ VAR3 }}" } 
>>
>> Set *default* to avoid errors, add attribute *def*, and select lines. 
>> For example, 
>>
>> shell> cat pb.yml 
>> --- 
>> - hosts: localhost 
>> gather_facts: false 
>> vars: 
>> lines: 
>> - regexp: '^text1' 
>> line: 'text1 = {{ VAR1|d("undef") }}' 
>> def: '{{ VAR1 is defined }}' 
>> - regexp: '^text2' 
>> line: 'text2 = {{ VAR2|d("undef") }}' 
>> def: '{{ VAR2 is defined }}' 
>> - regexp: '^text3' 
>> line: 'text3 = {{ VAR3|d("undef") }}' 
>> def: '{{ VAR3 is defined }}' 
>> tasks: 
>> - debug: 
>> var: item 
>> loop: "{{ lines|selectattr('def') }}" 
>>
>> gives 
>>
>> shell> ansible-playbook pb.yml -e VAR1=test 
>>
>> PLAY [localhost] 
>> * 
>>
>> TASK [debug] 
>> * 
>> ok: [localhost] => (item={'regexp': '^text1', 'line': 'text1 = test', 
>> 'def': True}) => ansible_loop_var: item item: 
>> def: true 
>> line: text1 = test 
>> regexp: ^text1 
>>
>> -- 
>> Vladimir Botka 
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/456b1d18-766b-4da2-9d1f-c7f0e1b37d36n%40googlegroups.com.


Re: [ansible-project] Lineinfile - Skip/Update an item inside "with_items" using conditions

2022-08-05 Thread Todd Lewis

- name: "Update Redis Parameters in {{ redis_config_path }}"
  lineinfile:
path: "{{ redis_config_path }}"
backrefs: yes
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
  when: item.line is match(omit)
  with_items:
- { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM | d(omit) 
}}mb" }
- { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE | 
d(omit) }}" }
- { regexp: '^tcp-backlog',   line: "tcp-backlog {{ TCP_BACKLOG | d(omit) 
}}" }
- { regexp: '^maxclients',line: "maxclients {{ MAX_CLIENTS | d(omit) 
}}" }
- { regexp: '^timeout',   line: "timeout {{ TIMEOUT }}" }


On 8/5/22 7:31 AM, farrukh ahmed wrote:

Thanks for your reply Vladimir Botka.

This is my actual case:

 defaults > main.yml
---
# defaults file for redis_configs
redis_config_path: /etc/redis/redis.conf

# Default Params Values
MAX_MEM: 3000
TCP_KEEPALIVE: 0
TCP_BACKLOG: 511
MAX_CLIENTS: 15000
TIMEOUT: 1500


And this is:

Tasks > main.yml

---

- name: "Update Redis Parameters in {{ redis_config_path }}"
  lineinfile:
    path: "{{ redis_config_path }}"
  backrefs: yes
  regexp: "{{ item.regexp }}"
  line: "{{ item.line }}"
  with_items:
    - { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM }}mb", when: 
{{ MAX_MEM }} is defined }
    - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ 
TCP_KEEPALIVE }}", when: {{ TCP_KEEPALIVE }} is defined }
    - { regexp: '^tcp-backlog', line: "tcp-backlog {{ TCP_BACKLOG }}", 
when: {{ TCP_BACKLOG }} is defined}
    - { regexp: '^maxclients', line: "maxclients {{ MAX_CLIENTS }}", 
when: {{ MAX_CLIENTS }} is defined}

    - { regexp: '^timeout', line: "timeout {{ TIMEOUT }}" }


I want this to check if each variable inside an item is defined then 
make the changes to the line in the file, otherwise skip only those 
items which variables is not defined.
For Instance, If I comment out the variable MAX_MEM from the defaults 
> main.yml. like below:


# Default Params Values
##MAX_MEM: 3000
TCP_KEEPALIVE: 0
TCP_BACKLOG: 511
MAX_CLIENTS: 15000
TIMEOUT: 1500

Then the execution should skip changes to the line where MAX_MEM is 
undefined, and the rest lines should be changed as they defined.


Is it possible?


On Friday, August 5, 2022 at 2:18:05 PM UTC+5 vbo...@gmail.com wrote:

On Thu, 4 Aug 2022 23:43:59 -0700 (PDT)
farrukh ahmed  wrote:

> with_items:
> - { regexp: '^text1', line: "text1 = {{ VAR1 }}", when: VAR1 is
defined }
> - { regexp: '^text2', line: "text2 = {{ VAR2 }}" }
> - { regexp: '^text3', line: "text3 = {{ VAR3 }}" }

Set *default* to avoid errors, add attribute *def*, and select lines.
For example,

shell> cat pb.yml
---
- hosts: localhost
gather_facts: false
vars:
lines:
- regexp: '^text1'
line: 'text1 = {{ VAR1|d("undef") }}'
def: '{{ VAR1 is defined }}'
- regexp: '^text2'
line: 'text2 = {{ VAR2|d("undef") }}'
def: '{{ VAR2 is defined }}'
- regexp: '^text3'
line: 'text3 = {{ VAR3|d("undef") }}'
def: '{{ VAR3 is defined }}'
tasks:
- debug:
var: item
loop: "{{ lines|selectattr('def') }}"

gives

shell> ansible-playbook pb.yml -e VAR1=test

PLAY [localhost]
*

TASK [debug]
*
ok: [localhost] => (item={'regexp': '^text1', 'line': 'text1 = test',
'def': True}) => ansible_loop_var: item item:
def: true
line: text1 = test
regexp: ^text1

-- 
Vladimir Botka




--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/f361c79f-4ad7-e622-bc7d-bf4843ef4f01%40gmail.com.


Re: [ansible-project] Lineinfile - Skip/Update an item inside "with_items" using conditions

2022-08-05 Thread Stefan Hornburg (Racke)

On 05/08/2022 13:31, farrukh ahmed wrote:

Thanks for your reply Vladimir Botka.

This is my actual case:

 defaults > main.yml
---
# defaults file for redis_configs
redis_config_path: /etc/redis/redis.conf

# Default Params Values
MAX_MEM: 3000
TCP_KEEPALIVE: 0
TCP_BACKLOG: 511
MAX_CLIENTS: 15000
TIMEOUT: 1500


And this is:

Tasks > main.yml

---

- name: "Update Redis Parameters in {{ redis_config_path }}"
  lineinfile:
    path: "{{ redis_config_path }}"
  backrefs: yes
  regexp: "{{ item.regexp }}"
  line: "{{ item.line }}"
  with_items:
    - { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM }}mb", when: {{ 
MAX_MEM }} is defined }
    - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE }}", 
when: {{ TCP_KEEPALIVE }} is defined }
    - { regexp: '^tcp-backlog', line: "tcp-backlog {{ TCP_BACKLOG }}", when: {{ 
TCP_BACKLOG }} is defined}
    - { regexp: '^maxclients', line: "maxclients {{ MAX_CLIENTS }}", when: {{ 
MAX_CLIENTS }} is defined}
    - { regexp: '^timeout', line: "timeout {{ TIMEOUT }}" }


I want this to check if each variable inside an item is defined then make the 
changes to the line in the file, otherwise skip only those items which 
variables is not defined.
For Instance, If I comment out the variable MAX_MEM from the defaults > 
main.yml. like below:

# Default Params Values
##MAX_MEM: 3000
TCP_KEEPALIVE: 0
TCP_BACKLOG: 511
MAX_CLIENTS: 15000
TIMEOUT: 1500

Then the execution should skip changes to the line where MAX_MEM is undefined, 
and the rest lines should be changed as they defined.

Is it possible?


Sorry, but it eludes me why you didn't come up with the actual case in the 
first place.  I see no point in wasting everyone's time.

Regards

    Racke



On Friday, August 5, 2022 at 2:18:05 PM UTC+5 vbo...@gmail.com wrote:

On Thu, 4 Aug 2022 23:43:59 -0700 (PDT)
farrukh ahmed  wrote:

> with_items:
> - { regexp: '^text1', line: "text1 = {{ VAR1 }}", when: VAR1 is defined }
> - { regexp: '^text2', line: "text2 = {{ VAR2 }}" }
> - { regexp: '^text3', line: "text3 = {{ VAR3 }}" }

Set *default* to avoid errors, add attribute *def*, and select lines.
For example,

shell> cat pb.yml
---
- hosts: localhost
gather_facts: false
vars:
lines:
- regexp: '^text1'
line: 'text1 = {{ VAR1|d("undef") }}'
def: '{{ VAR1 is defined }}'
- regexp: '^text2'
line: 'text2 = {{ VAR2|d("undef") }}'
def: '{{ VAR2 is defined }}'
- regexp: '^text3'
line: 'text3 = {{ VAR3|d("undef") }}'
def: '{{ VAR3 is defined }}'
tasks:
- debug:
var: item
loop: "{{ lines|selectattr('def') }}"

gives

shell> ansible-playbook pb.yml -e VAR1=test

PLAY [localhost]
*

TASK [debug]
*
ok: [localhost] => (item={'regexp': '^text1', 'line': 'text1 = test',
'def': True}) => ansible_loop_var: item item:
def: true
line: text1 = test
regexp: ^text1

-- 
Vladimir Botka


--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/0eaa1c36-727c-47a8-bebc-6f38db9f4971n%40googlegroups.com
 
.



--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/4a22e810-d112-6d68-1a57-72661775e675%40linuxia.de.


Re: [ansible-project] Lineinfile - Skip/Update an item inside "with_items" using conditions

2022-08-05 Thread farrukh ahmed
Thanks for your reply Vladimir Botka.

This is my actual case:

 defaults > main.yml
---
# defaults file for redis_configs
redis_config_path: /etc/redis/redis.conf

# Default Params Values
MAX_MEM: 3000
TCP_KEEPALIVE: 0
TCP_BACKLOG: 511
MAX_CLIENTS: 15000
TIMEOUT: 1500


And this is: 

Tasks > main.yml

---

- name: "Update Redis Parameters in {{ redis_config_path }}"
  lineinfile:
path: "{{ redis_config_path }}"
  backrefs: yes
  regexp: "{{ item.regexp }}"
  line: "{{ item.line }}"
  with_items:
- { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM }}mb", when: {{ 
MAX_MEM }} is defined } 
- { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE 
}}", when: {{ TCP_KEEPALIVE }} is defined }
- { regexp: '^tcp-backlog', line: "tcp-backlog {{ TCP_BACKLOG }}", 
when: {{ TCP_BACKLOG }} is defined}
- { regexp: '^maxclients', line: "maxclients {{ MAX_CLIENTS }}", when: 
{{ MAX_CLIENTS }} is defined}
- { regexp: '^timeout', line: "timeout {{ TIMEOUT }}" } 


I want this to check if each variable inside an item is defined then make 
the changes to the line in the file, otherwise skip only those items which 
variables is not defined.
For Instance, If I comment out the variable MAX_MEM from the defaults > 
main.yml. like below:

# Default Params Values
##MAX_MEM: 3000
TCP_KEEPALIVE: 0
TCP_BACKLOG: 511
MAX_CLIENTS: 15000
TIMEOUT: 1500  

Then the execution should skip changes to the line where MAX_MEM is 
undefined, and the rest lines should be changed as they defined.

Is it possible?


On Friday, August 5, 2022 at 2:18:05 PM UTC+5 vbo...@gmail.com wrote:

> On Thu, 4 Aug 2022 23:43:59 -0700 (PDT)
> farrukh ahmed  wrote:
>
> > with_items:
> > - { regexp: '^text1', line: "text1 = {{ VAR1 }}", when: VAR1 is defined }
> > - { regexp: '^text2', line: "text2 = {{ VAR2 }}" }
> > - { regexp: '^text3', line: "text3 = {{ VAR3 }}" }
>
> Set *default* to avoid errors, add attribute *def*, and select lines.
> For example,
>
> shell> cat pb.yml
> ---
> - hosts: localhost
> gather_facts: false
> vars:
> lines:
> - regexp: '^text1'
> line: 'text1 = {{ VAR1|d("undef") }}'
> def: '{{ VAR1 is defined }}'
> - regexp: '^text2'
> line: 'text2 = {{ VAR2|d("undef") }}'
> def: '{{ VAR2 is defined }}'
> - regexp: '^text3'
> line: 'text3 = {{ VAR3|d("undef") }}'
> def: '{{ VAR3 is defined }}'
> tasks:
> - debug:
> var: item
> loop: "{{ lines|selectattr('def') }}"
>
> gives
>
> shell> ansible-playbook pb.yml -e VAR1=test
>
> PLAY [localhost]
> *
>
> TASK [debug]
> *
> ok: [localhost] => (item={'regexp': '^text1', 'line': 'text1 = test',
> 'def': True}) => ansible_loop_var: item item:
> def: true
> line: text1 = test
> regexp: ^text1
>
> -- 
> Vladimir Botka
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/0eaa1c36-727c-47a8-bebc-6f38db9f4971n%40googlegroups.com.


Re: [ansible-project] Lineinfile - Skip/Update an item inside "with_items" using conditions

2022-08-05 Thread Vladimir Botka
On Thu, 4 Aug 2022 23:43:59 -0700 (PDT)
farrukh ahmed  wrote:

> with_items:
>   - { regexp: '^text1', line: "text1 = {{ VAR1 }}", when: VAR1 is defined }
>   - { regexp: '^text2', line: "text2 = {{ VAR2 }}" }
>   - { regexp: '^text3', line: "text3 = {{ VAR3 }}" }

Set *default* to avoid errors, add attribute *def*, and select lines.
For example,

shell> cat pb.yml
---
- hosts: localhost
  gather_facts: false
  vars:
lines:
  - regexp: '^text1'
line: 'text1 = {{ VAR1|d("undef") }}'
def: '{{ VAR1 is defined }}'
  - regexp: '^text2'
line: 'text2 = {{ VAR2|d("undef") }}'
def: '{{ VAR2 is defined }}'
  - regexp: '^text3'
line: 'text3 = {{ VAR3|d("undef") }}'
def: '{{ VAR3 is defined }}'
  tasks:
- debug:
var: item
  loop: "{{ lines|selectattr('def') }}"

gives

shell> ansible-playbook pb.yml -e VAR1=test

PLAY [localhost]
*

TASK [debug]
*
ok: [localhost] => (item={'regexp': '^text1', 'line': 'text1 = test',
'def': True}) => ansible_loop_var: item item:
def: true
line: text1 = test
regexp: ^text1

-- 
Vladimir Botka

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20220805111742.55c17fbf%40gmail.com.


pgpZj2RZv0VfV.pgp
Description: OpenPGP digital signature