Re: [ansible-project] key from ansible_facts seen by debug but not by the actual task

2022-12-13 Thread Adrian Sevcenco

On 14.12.2022 08:05, Dick Visser wrote:

It's the other way around from what you state in the subject.
Your debug task using the variable ansible_all_ipv4_addresses, which works.

But then when of your copy task uses 
ansible_facts['ansible_all_ipv4_addresses'], which gives the error.
Try using just ansible_all_ipv4_addresses in the when clause?

THANKS a lot!!! That was it!!!
i was so hunged up on using the ansible_facts like i use for OS conditions that 
i did not even considered
despite the evidence show by debug module (where i considered that to be an 
artefact of jinja usage)

Once again many many thanks!!
Adrian




On Wed, 14 Dec 2022 at 00:31, Adrian Sevcenco mailto:adrian@gmail.com>> wrote:

Hi! So, while i do use conditions like: ansible_facts[ 'distribution' ] == 
'CentOS'
now i'm trying to use ansible_all_ipv4_addresses

i have the following code:

- debug:
      msg: |
        ansible_all_ipv4_addresses: {{ ansible_all_ipv4_addresses }}
        _network_reduced: "{{ ansible_all_ipv4_addresses | 
ansible.utils.reduce_on_network('XX.XXX.XX.0/24') }}"
        _result_lenght: "{{ ansible_all_ipv4_addresses | 
ansible.utils.reduce_on_network('XX.XXX.XX.0/24') | length }}"
        _result_value: "{{ ( ansible_all_ipv4_addresses | 
ansible.utils.reduce_on_network('XX.XXX.XX.0/24') | length |
int ) > 0 }}"

- name: Chrony configuration 85
    ansible.builtin.copy:
      src: "{{ playbook_dir }}/../additions/chrony/chrony.conf.client_pub85"
      dest: /etc/chrony.conf
      mode: 0644
      owner: root
      group: root
      backup: true
      force: true
    register: chrony_is_configured
    when:
      - ( ( ansible_facts['ansible_all_ipv4_addresses'] | 
ansible.utils.reduce_on_network( 'XX.XXX.XX.0/24' ) |
length |
int ) > 0 )


the problem is that the debug see the ansible_all_ipv4_addresses while the 
when condition does not
i get:

Dec 14 2022 01:18:00 - chrony_cfg.yml -  - debug - OK - {"msg": 
"ansible_all_ipv4_addresses: ['XX.XXX.XX.21',
'172.18.0.21']\n_network_reduced: \"['XX.XXX.XX.21']\"\n_result_lenght: 
\"1\"\n_result_value: \"True\"\n",
"_ansible_verbose_always": true, "_ansible_no_log": null, "changed": false}

Dec 14 2022 01:18:00 - chrony_cfg.yml - Chrony configuration 85 - ansible.builtin.copy - 
FAILED - {"msg": "The
conditional check '( ( ansible_facts['ansible_all_ipv4_addresses'] | 
ansible.utils.reduce_on_network(
'XX.XXX.XX.0/24' )
| length | int ) > 0 )' failed. The error was: error while evaluating 
conditional (( (
ansible_facts['ansible_all_ipv4_addresses'] | ansible.utils.reduce_on_network( 
'XX.XXX.XX.0/24' ) | length | int ) > 0
)): 'dict object' has no attribute 'ansible_all_ipv4_addresses'. 'dict 
object' has no attribute
'ansible_all_ipv4_addresses'\n\nThe error appears to be in 
'/home/adrian/ansible/playbooks/tasks/pkg_chrony_task.yml':
line 15, column 3, but may\nbe elsewhere in the file depending on the exact 
syntax problem.\n\nThe offending line
appears to be:\n\n\n- name: Chrony configuration 85\n  ^ here\n", 
"_ansible_no_log": false}

so, beside that i can check in my redis backend that 
'ansible_all_ipv4_addresses' is present, the fact is that
the debug task works ..

So, does anyone have any idea what is going on?
Thanks a lot!!
Adrian

-- 
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/514517aa-4361-c76a-1410-8523c13f7174%40gmail.com

.

--
Sent from Gmail Mobile

--
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/CAF8BbLa-YT2JFrSgJxZPz3-yzav0V4AUz9y2T-UCmnx8grN1-A%40mail.gmail.com 
.


--
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/d139a13f-30a1-05c9-5e3b-85fdbffebfa4%40gmail.com.


Re: [ansible-project] key from ansible_facts seen by debug but not by the actual task

2022-12-13 Thread Dick Visser
On Wed, 14 Dec 2022 at 00:31, Adrian Sevcenco 

   _network_reduced: "{{ ansible_all_ipv4_addresses |
> ansible.utils.reduce_on_network('XX.XXX.XX.0/24') }}"


Something unrelated, but I had not heard of this filter. Looking at the
docs on
https://docs.ansible.com/ansible/latest/collections/ansible/utils/reduce_on_network_filter.html,
it seems to me that it is doing the same thing as:

ansible.utils.ipaddr('XX.XXX.XX.0/24')

Or am I missing something?

Thx!

-- 
Sent from Gmail Mobile

-- 
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/CAF8BbLbC3OxxcEwBzchO52fDcJELGkm1BhAwcSsfmU_-8Fp%2Bug%40mail.gmail.com.


Re: [ansible-project] key from ansible_facts seen by debug but not by the actual task

2022-12-13 Thread Dick Visser
It's the other way around from what you state in the subject.
Your debug task using the variable ansible_all_ipv4_addresses, which works.

But then when of your copy task uses
ansible_facts['ansible_all_ipv4_addresses'],
which gives the error.
Try using just ansible_all_ipv4_addresses in the when clause?


On Wed, 14 Dec 2022 at 00:31, Adrian Sevcenco  wrote:

> Hi! So, while i do use conditions like: ansible_facts[ 'distribution' ] ==
> 'CentOS'
> now i'm trying to use ansible_all_ipv4_addresses
>
> i have the following code:
>
> - debug:
>  msg: |
>ansible_all_ipv4_addresses: {{ ansible_all_ipv4_addresses }}
>_network_reduced: "{{ ansible_all_ipv4_addresses |
> ansible.utils.reduce_on_network('XX.XXX.XX.0/24') }}"
>_result_lenght: "{{ ansible_all_ipv4_addresses |
> ansible.utils.reduce_on_network('XX.XXX.XX.0/24') | length }}"
>_result_value: "{{ ( ansible_all_ipv4_addresses |
> ansible.utils.reduce_on_network('XX.XXX.XX.0/24') | length |
> int ) > 0 }}"
>
> - name: Chrony configuration 85
>ansible.builtin.copy:
>  src: "{{ playbook_dir }}/../additions/chrony/chrony.conf.client_pub85"
>  dest: /etc/chrony.conf
>  mode: 0644
>  owner: root
>  group: root
>  backup: true
>  force: true
>register: chrony_is_configured
>when:
>  - ( ( ansible_facts['ansible_all_ipv4_addresses'] |
> ansible.utils.reduce_on_network( 'XX.XXX.XX.0/24' ) | length |
> int ) > 0 )
>
>
> the problem is that the debug see the ansible_all_ipv4_addresses while the
> when condition does not
> i get:
>
> Dec 14 2022 01:18:00 - chrony_cfg.yml -  - debug - OK - {"msg":
> "ansible_all_ipv4_addresses: ['XX.XXX.XX.21',
> '172.18.0.21']\n_network_reduced: \"['XX.XXX.XX.21']\"\n_result_lenght:
> \"1\"\n_result_value: \"True\"\n",
> "_ansible_verbose_always": true, "_ansible_no_log": null, "changed": false}
>
> Dec 14 2022 01:18:00 - chrony_cfg.yml - Chrony configuration 85 -
> ansible.builtin.copy - FAILED - {"msg": "The
> conditional check '( ( ansible_facts['ansible_all_ipv4_addresses'] |
> ansible.utils.reduce_on_network( 'XX.XXX.XX.0/24' )
> | length | int ) > 0 )' failed. The error was: error while evaluating
> conditional (( (
> ansible_facts['ansible_all_ipv4_addresses'] |
> ansible.utils.reduce_on_network( 'XX.XXX.XX.0/24' ) | length | int ) > 0
> )): 'dict object' has no attribute 'ansible_all_ipv4_addresses'. 'dict
> object' has no attribute
> 'ansible_all_ipv4_addresses'\n\nThe error appears to be in
> '/home/adrian/ansible/playbooks/tasks/pkg_chrony_task.yml':
> line 15, column 3, but may\nbe elsewhere in the file depending on the
> exact syntax problem.\n\nThe offending line
> appears to be:\n\n\n- name: Chrony configuration 85\n  ^ here\n",
> "_ansible_no_log": false}
>
> so, beside that i can check in my redis backend that
> 'ansible_all_ipv4_addresses' is present, the fact is that
> the debug task works ..
>
> So, does anyone have any idea what is going on?
> Thanks a lot!!
> Adrian
>
> --
> 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/514517aa-4361-c76a-1410-8523c13f7174%40gmail.com
> .
>
-- 
Sent from Gmail Mobile

-- 
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/CAF8BbLa-YT2JFrSgJxZPz3-yzav0V4AUz9y2T-UCmnx8grN1-A%40mail.gmail.com.


[ansible-project] key from ansible_facts seen by debug but not by the actual task

2022-12-13 Thread Adrian Sevcenco

Hi! So, while i do use conditions like: ansible_facts[ 'distribution' ] == 
'CentOS'
now i'm trying to use ansible_all_ipv4_addresses

i have the following code:

- debug:
msg: |
  ansible_all_ipv4_addresses: {{ ansible_all_ipv4_addresses }}
  _network_reduced: "{{ ansible_all_ipv4_addresses | 
ansible.utils.reduce_on_network('XX.XXX.XX.0/24') }}"
  _result_lenght: "{{ ansible_all_ipv4_addresses | 
ansible.utils.reduce_on_network('XX.XXX.XX.0/24') | length }}"
  _result_value: "{{ ( ansible_all_ipv4_addresses | ansible.utils.reduce_on_network('XX.XXX.XX.0/24') | length | 
int ) > 0 }}"


- name: Chrony configuration 85
  ansible.builtin.copy:
src: "{{ playbook_dir }}/../additions/chrony/chrony.conf.client_pub85"
dest: /etc/chrony.conf
mode: 0644
owner: root
group: root
backup: true
force: true
  register: chrony_is_configured
  when:
- ( ( ansible_facts['ansible_all_ipv4_addresses'] | ansible.utils.reduce_on_network( 'XX.XXX.XX.0/24' ) | length | 
int ) > 0 )



the problem is that the debug see the ansible_all_ipv4_addresses while the when 
condition does not
i get:

Dec 14 2022 01:18:00 - chrony_cfg.yml -  - debug - OK - {"msg": "ansible_all_ipv4_addresses: ['XX.XXX.XX.21', 
'172.18.0.21']\n_network_reduced: \"['XX.XXX.XX.21']\"\n_result_lenght: \"1\"\n_result_value: \"True\"\n", 
"_ansible_verbose_always": true, "_ansible_no_log": null, "changed": false}


Dec 14 2022 01:18:00 - chrony_cfg.yml - Chrony configuration 85 - ansible.builtin.copy - FAILED - {"msg": "The 
conditional check '( ( ansible_facts['ansible_all_ipv4_addresses'] | ansible.utils.reduce_on_network( 'XX.XXX.XX.0/24' ) 
| length | int ) > 0 )' failed. The error was: error while evaluating conditional (( ( 
ansible_facts['ansible_all_ipv4_addresses'] | ansible.utils.reduce_on_network( 'XX.XXX.XX.0/24' ) | length | int ) > 0 
)): 'dict object' has no attribute 'ansible_all_ipv4_addresses'. 'dict object' has no attribute 
'ansible_all_ipv4_addresses'\n\nThe error appears to be in '/home/adrian/ansible/playbooks/tasks/pkg_chrony_task.yml': 
line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line 
appears to be:\n\n\n- name: Chrony configuration 85\n  ^ here\n", "_ansible_no_log": false}


so, beside that i can check in my redis backend that 
'ansible_all_ipv4_addresses' is present, the fact is that
the debug task works ..

So, does anyone have any idea what is going on?
Thanks a lot!!
Adrian

--
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/514517aa-4361-c76a-1410-8523c13f7174%40gmail.com.


[ansible-project] ANSIBLE - Venv - Use /usr/bin/ansible

2022-12-13 Thread julien mahieu
Hi experts,

In order to manage network devices, I'm currently setting up an environment 
with ansible inside a python venv on a linux Alma 8 platform.

Below, the steps I used to install the components :

*# Venv creation*
[MyUser@MyAnsibleVm ~]$python3 -m venv ansible4.10
[MyUser@MyAnsibleVm ~]$source ansible4.10/bin/activate

*# pip installation / upgrade*
(ansible4.10) [MyUser@MyAnsibleVm ~]$python3 -m ensurepip --default-pip
(ansible4.10) [MyUser@MyAnsibleVm ~]$python3 -m pip install --upgrade pip

*# Ansible installation*
(ansible4.10) [MyUser@MyAnsibleVm ~]$python3 -m pip install ansible

At the end we have ansible installed correctly. All the steps seems to be 
good.
*# List packages*
(ansible4.10) [MyUser@MyAnsibleVm ~]$ python3 -m pip list
Package Version
 ---
*ansible 4.10.0*
*ansible-core 2.11.12*
cffi 1.15.1
cryptography 38.0.3
Jinja2 3.0.3
...

Unfortunately, if I check the ansible version inside my venv, I still have 
my old global ansible version, rather than the new venv version :

(ansible4.10) [MyUser@MyAnsibleVm ~]$ ansible --version
*ansible 2.9.10*
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/MyUser/.ansible/plugins/modules', 
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible

python version = 3.6.8 (default, May 24 2021, 08:40:28) [GCC 8.4.1 20200928 
(Red Hat 8.4.1-1)]

Any ideas to help ?

Thank you

Ju

-- 
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/cd2d126a-756b-4e36-93bc-89cccd0f5cf7n%40googlegroups.com.


Re: [ansible-project] at - specific time & date

2022-12-13 Thread Vladimir Botka
On Tue, 13 Dec 2022 15:15:06 +0100 (CET)
dulhaver via Ansible Project  wrote:

> > Yes, it's worth PR. I'll write tests for this. The question is which
> > collection?
> 
> 
> honestly, I am still not really 100% sure what the term
> 'collection' actually means in the Ansible context and how things
> are organized. The issue, however, lives in
> ansible-collection/ansible.posix. Isn't that hint enough?

I'm not sure if this kind of datetime conversion belongs to POSIX.

To learn about collections see https://github.com/ansible-collections

For example, Ansible.Posix, where the module ansible.posix.at comes
from, is "Ansible Collection targeting POSIX and POSIX-ish platforms."
https://docs.ansible.com/ansible/latest/collections/ansible/posix/index.html


-- 
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/20221213155643.7a48cea6%40gmail.com.


pgpRbwAkotuZb.pgp
Description: OpenPGP digital signature


Re: [ansible-project] at - specific time & date

2022-12-13 Thread dulhaver via Ansible Project
> Yes, it's worth PR. I'll write tests for this. The question is which
> collection?


honestly, I am still not really 100% sure what the term 'collection' actually 
means in the Ansible context and how things are organized. The issue, however, 
lives in ansible-collection/ansible.posix. Isn't that hint enough?



> On 12/13/2022 1:20 PM CET Vladimir Botka  wrote:
> 
>  
> > count: "{{ at_datetime|at_minutes }}"
> > ...
> > would it be worth a PR on the issue or is this too quick'n'dirty? 
> 
> It's quick'n'clean I'd say :)
> 
> Yes, it's worth PR. I'll write tests for this. The question is which
> collection?
> 
> -- 
> 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/20221213132000.59eb4000%40gmail.com.

-- 
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/1457110830.3856.1670940906516%40office.mailbox.org.


[ansible-project] Ansible dellemc.os10 does not initiate SSH

2022-12-13 Thread Maloy Ghosh
Hi,
On reading the docs my assumption has been that using `ansible_connection` 
= 'network_cli', tells ansible to run ssh to target and run commands. 

In my case it is not happening.

*$ cat /etc/issue*
Ubuntu 20.04.4 LTS \n \l

*$ ansible --version*
ansible* 2.9.6*
  config file = /etc/ansible/ansible.cfg
  configured module search path = 
['/home/dushyant/.ansible/plugins/modules', 
'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0]

$ *ansible-galaxy collection install dellemc.os10*   # Install dellemc roles

I setup my test dir as follows.

*$ tree .*
.
___ datacenter.yaml
___ host_vars
___ ___ R1.yaml
___ inventory.yaml

1 directory, 3 files


*$ cat datacenter.yaml *
---
- hosts: datacenter
  connection: network_cli
  collections:
- dellemc.os10
  vars:
build_dir: "/path/ansible/build/debug"

  roles:
- os10_system

*$ cat inventory.yaml *
R1 ansible_host=192.168.111.117 

[site1]
R1

[datacenter:children]
site1

*$ cat host_vars/R1.yaml *
ansible_network_os: dellemc.os10.os10
ansible_connection: network_cli
ansible_ssh_user: test
ansible_ssh_pass: test123

os10_system:
  hostname: "VLT1-Primary"


When I run *ansible-playbook -vvv -i inventory.yaml datacenter.yaml*
ansible-playbook 2.9.6 


  ...   
Parsed /path/ansible/test/inventory.yaml inventory source with ini plugin   




   
PLAYBOOK: datacenter.yaml 
*
1 plays in datacenter.yaml 




   
PLAY [datacenter] 
*


   
TASK [Gathering Facts] 


*task path: /path/ansible/test/datacenter.yaml:2   

  <192.168.111.117> ESTABLISH 
LOCAL CONNECTION FOR USER: dushyant   *

 
<192.168.111.117> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo 
/home/dushyant/.ansible/tmp/ansible-local-893052ib2n_n69/ansible-tmp-1670934084.286-89363922941759
 
`" && echo ansible-tmp-1670934084.286-
89363922941759="` echo 
/home/dushyant/.ansible/tmp/ansible-local-893052ib2n_n69/ansible-tmp-1670934084.286-89363922941759
 
`" ) && sleep 0'   
  
 Attempting python interpreter discovery



*I do not see any SSH attempt in card. The `LOCAL` connection log line and 
username makes me wonder that it is not picking up my host_vars/ settings.*

*I have also tried changing variable 'ansible_ssh_user' variable to 
`ansible_user` as well. *

I must be missing something very simple. Any pointers would be helpful.
 

-- 
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/4ad0b2f3-4500-40b2-bcfe-3e9106453929n%40googlegroups.com.


Re: [ansible-project] at - specific time & date

2022-12-13 Thread Vladimir Botka
> count: "{{ at_datetime|at_minutes }}"
> ...
> would it be worth a PR on the issue or is this too quick'n'dirty? 

It's quick'n'clean I'd say :)

Yes, it's worth PR. I'll write tests for this. The question is which
collection?

-- 
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/20221213132000.59eb4000%40gmail.com.


pgpGVDMiFBB_w.pgp
Description: OpenPGP digital signature


Re: [ansible-project] at - specific time & date

2022-12-13 Thread dulhaver via Ansible Project
That works! Even with looping

##
- hosts: localhost

  tasks:
- ansible.posix.at:
command: date > /tmp/test_at
count: "{{ at_datetime|at_minutes }}"
units: minutes
  vars:
at_datetime: "{{ item }}"
  loop:
- "2022-12-13 12:09"
- "2022-12-13 12:10"
- "2022-12-13 12:13"
##

would it be worth a PR on the issue or is this too quick'n'dirty? 

It seems to be a different approach then the one suggested in the PR though (I 
can not judge on any of that due to missing python skills).




> On 12/13/2022 11:03 AM CET Vladimir Botka  wrote:
> 
>  
> On Tue, 13 Dec 2022 09:23:40 +0100 (CET)
> dulhaver via Ansible Project  wrote:
> 
> > thx that really works. It is not really user-friendly though.
> > 
> > Apparently I am not the only one thinking this should be easier, so there 
> > is an issue for adding a more intuitive way to specify time and date 
> > https://github.com/ansible-collections/ansible.posix/issues/326
> 
> > > now_datetime: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
> > > at_datetime: "2022-12-12 17:30:00"
> > > at_seconds: "{{ ((at_datetime|to_datetime) -
> > >  (now_datetime|to_datetime)).seconds }}"
> > > at_minutes: "{{ (at_seconds|int / 60)|int + 1 }}"
> > > 
> > > - ansible.posix.at:
> > > command: date > /tmp/test_at
> > > count: "{{ at_minutes }}"
> > > units: minutes
> 
> Yes, it's rather awkward. I simplified the declaration of
> *at_seconds*. It might be a good idea to have a conversion function
> for this, e.g.
> 
> 
> - ansible.posix.at:
> command: date > /tmp/test_at
> count: "{{ at_datetime|at_minutes }}"
> units: minutes
>   vars:
> at_datetime: "2022-12-12 17:30:00"
>   
> 
> Try
> 
> shell> cat plugins/filter/at_minutes.py
> from datetime import datetime
> 
> 
> def at_minutes(at_datetime):
> timesince = datetime.fromisoformat(at_datetime) - datetime.now()
> return int(timesince.total_seconds() / 60 + 1)
> 
> 
> class FilterModule(object):
> 
> def filters(self):
> return {
> 'at_minutes': at_minutes,
> }
> 
> 
> -- 
> 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/20221213110300.52f9ca3e%40gmail.com.

-- 
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/1586929021.1050941.1670932154662%40office.mailbox.org.


Re: [ansible-project] at - specific time & date

2022-12-13 Thread Vladimir Botka
On Tue, 13 Dec 2022 09:23:40 +0100 (CET)
dulhaver via Ansible Project  wrote:

> thx that really works. It is not really user-friendly though.
> 
> Apparently I am not the only one thinking this should be easier, so there is 
> an issue for adding a more intuitive way to specify time and date 
> https://github.com/ansible-collections/ansible.posix/issues/326

> > now_datetime: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
> > at_datetime: "2022-12-12 17:30:00"
> > at_seconds: "{{ ((at_datetime|to_datetime) -
> >  (now_datetime|to_datetime)).seconds }}"
> > at_minutes: "{{ (at_seconds|int / 60)|int + 1 }}"
> > 
> > - ansible.posix.at:
> > command: date > /tmp/test_at
> > count: "{{ at_minutes }}"
> > units: minutes

Yes, it's rather awkward. I simplified the declaration of
*at_seconds*. It might be a good idea to have a conversion function
for this, e.g.


- ansible.posix.at:
command: date > /tmp/test_at
count: "{{ at_datetime|at_minutes }}"
units: minutes
  vars:
at_datetime: "2022-12-12 17:30:00"
  

Try

shell> cat plugins/filter/at_minutes.py
from datetime import datetime


def at_minutes(at_datetime):
timesince = datetime.fromisoformat(at_datetime) - datetime.now()
return int(timesince.total_seconds() / 60 + 1)


class FilterModule(object):

def filters(self):
return {
'at_minutes': at_minutes,
}


-- 
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/20221213110300.52f9ca3e%40gmail.com.


pgp4WQz0aegz5.pgp
Description: OpenPGP digital signature


Re: [ansible-project] at - specific time & date

2022-12-13 Thread dulhaver via Ansible Project
thx that really works. It is not really user-friendly though.

Apparently I am not the only one thinking this should be easier, so there is an 
issue for adding a more intuitive way to specify time and date 
https://github.com/ansible-collections/ansible.posix/issues/326



> On 12/12/2022 5:37 PM CET Vladimir Botka  wrote:
> 
>  
> On Mon, 12 Dec 2022 15:30:07 +0100 (CET)
> dulhaver via Ansible Project  wrote:
> 
> > I want to automate a list of command exections with at.
> > What is the concepts here to specify something like '16:00 2023-01-19'?
> 
> For example, to schedule a command at "2022-12-12 17:30:00" declare
> the variables
> 
> now_datetime: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
> at_datetime: "2022-12-12 17:30:00"
> at_seconds: "{{ ((at_datetime|to_datetime) -
>  (now_datetime|
>   to_datetime('%Y-%m-%d %H:%M:%S'))).seconds }}"
> at_minutes: "{{ (at_seconds|int / 60)|int + 1 }}"
> 
> gives
> 
> now_datetime: 2022-12-12 17:26:23
> at_datetime: 2022-12-12 17:30:00
> at_seconds: 217
> at_minutes: 4
> 
> Use the variable *at_minutes*
> 
> - ansible.posix.at:
> command: date > /tmp/test_at
> count: "{{ at_minutes }}"
> units: minutes
> - command: at -l
>   register: out
> - debug:
> var: out.stdout
> 
> will display the queue
> 
>   out.stdout: "5\tMon Dec 12 17:30:00 2022 a admin"
> 
> The command executed as expected
> 
> shell> cat /tmp/test_at 
> Mon 12 Dec 2022 05:30:00 PM CET
> 
> Example of a complete playbook for testing
> 
> - hosts: localhost
> 
>   vars:
> 
> now_datetime: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
> at_datetime: "2022-12-12 17:30:00"
> at_seconds: "{{ ((at_datetime|to_datetime) -
>  (now_datetime|
>   to_datetime('%Y-%m-%d %H:%M:%S'))).seconds }}"
> at_minutes: "{{ (at_seconds|int /
> 60)|int + 1 }}"
> 
>   tasks:
> 
> - debug:
> msg: |
>   now_datetime: {{ now_datetime }}
>   at_datetime: {{ at_datetime }}
>   at_seconds: {{ at_seconds }}
>   at_minutes: {{ at_minutes }}
> 
> - ansible.posix.at:
> command: date > /tmp/test_at
> count: "{{ at_minutes }}"
> units: minutes
> 
> - command: at -l
>   register: out
> 
> - debug:
> var: out.stdout
> 
> -- 
> 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/20221212173718.597130f3%40gmail.com.

-- 
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/471839507.1031492.1670919820411%40office.mailbox.org.