Well it is easier as I thought. Should have tried by myself before having
asked here. Sharing the result. Still need to derive the var from the file
stat, but hoping it is possible:
test.yml:
# ansible-playbook -l offline -i hosts.cfg test.yml
- hosts: all
vars:
test: true
tasks:
- name: Copy config file, when test is defined
tags: copy
template:
src: templates/test.j2
dest: test.cfg
when: test
- name: Copy config file, when test is not defined
tags: copy
template:
src: templates/test.j2
dest: test.cfg
when: not test
templates/test.j2:
{% if test %}
Test is defined
{% else %}
Test is not defined
{% endif %}
A run with test=true skips the second copy task (as desired) and produces a
file, which states "Test is defined" (as desired). The other way around
with test=false
decades software schrieb am Mittwoch, 5. Januar 2022 um 19:22:43 UTC+1:
> Exactly. Thanks
>
>
> Am 05.01.2022 um 19:21 schrieb Wei-Yen Tan <[email protected]>:
>
> Can't see why you can't just run it before the template task. But your
> choice really.
>
> Get Outlook for iOS <https://aka.ms/o0ukef>
> ------------------------------
> *From:* [email protected] <[email protected]> on
> behalf of decades software <[email protected]>
> *Sent:* Thursday, January 6, 2022 7:14:58 AM
> *To:* [email protected] <[email protected]>
> *Subject:* Re: [ansible-project] Variable state depending on existence of
> a file on host system - possible?
>
> It is a grown project, I can't change everything just because of this.
>
> Anyway, if it is not possible, then ok. I can live with the glitch
>
>
> Am 05.01.2022 um 19:13 schrieb Wei-Yen Tan <[email protected]>:
>
> Why put in a jinja file? Run the assertion first in the role task /fail
> exit etc. Then run the template task
>
> Get Outlook for iOS <https://aka.ms/o0ukef>
> ------------------------------
> *From:* [email protected] <[email protected]> on
> behalf of decades software <[email protected]>
> *Sent:* Thursday, January 6, 2022 7:11:11 AM
> *To:* [email protected] <[email protected]>
> *Subject:* Re: [ansible-project] Variable state depending on existence of
> a file on host system - possible?
>
> Looks pretty verbose. I have some doubts to be able to use this in a .j2
> file.
>
> Am 05.01.2022 um 19:08 schrieb Wei-Yen Tan <[email protected]>:
>
> It is the example. Register
>
> Get Outlook for iOS <https://aka.ms/o0ukef>
> ------------------------------
> *From:* [email protected] <[email protected]> on
> behalf of decades software <[email protected]>
> *Sent:* Thursday, January 6, 2022 7:06:59 AM
> *To:* [email protected] <[email protected]>
> *Subject:* Re: [ansible-project] Variable state depending on existence of
> a file on host system - possible?
>
> Well, yes. I know, how to stat a file. I don't know how to create a
> variable from that with the same scope as a variable defined in
> default.yml.
>
> I thought I was precise enough. Never mind. Disregard my question. Will
> find it out.
>
>
> Am 05.01.2022 um 18:34 schrieb Wei-Yen Tan <[email protected]>:
>
> Use stat module to get your required facts. Bring it into assert or fail
>
>
> https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html
>
> Get Outlook for iOS <https://aka.ms/o0ukef>
> ------------------------------
> *From:* 'Neil Young' via Ansible Project <[email protected]>
> *Sent:* Thursday, January 6, 2022 6:26:54 AM
> *To:* [email protected] <[email protected]>
> *Subject:* Re: [ansible-project] Variable state depending on existence of
> a file on host system - possible?
>
> It is not the problem of existence. The var exists all the time. It is
> about the wrong state
>
> Von meinem iPad gesendet
>
> Am 05.01.2022 um 18:18 schrieb Wei-Yen Tan <[email protected]>:
>
>
> You could use the assert task to check that the variable exist before
> starting the run
>
> Get Outlook for iOS <https://aka.ms/o0ukef>
> ------------------------------
> *From:* 'Neil Young' via Ansible Project <[email protected]>
> *Sent:* Thursday, January 6, 2022 6:16:28 AM
> *To:* Ansible Project <[email protected]>
> *Subject:* [ansible-project] Variable state depending on existence of a
> file on host system - possible?
>
> I'm currently having a var in my ./vars/default.yml, which is used at
> several points in my playbook for branching installation operations.
>
> boolean_var: true
>
> Depending on this I'm performing (or not) install steps
>
> ....
> when: not boolean_var
>
> So far so good. I'm also using this var in some j2 templates like so:
>
> {% if boolean_var %}
> ...
> {% endif %}
>
> My problem is, that I mostly forget to set this variable to a proper
> state, which reflects the real status on the host system, so I have to
> abort an update.
> I would rather like to derive it from a pre_task, checking the existence
> of a particular file on the host before starting all operations.
>
> Is there a way to define a variable with the same scope (playbook and
> templates) at runtime?
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/5da6e38e-ae18-4b28-98f9-4341465c2cbfn%40googlegroups.com
>
> <https://groups.google.com/d/msgid/ansible-project/5da6e38e-ae18-4b28-98f9-4341465c2cbfn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/MWKwiX9ICR8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/SYBP282MB35259E19C8AA647EF3B46381AD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM
>
> <https://groups.google.com/d/msgid/ansible-project/SYBP282MB35259E19C8AA647EF3B46381AD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM?utm_medium=email&utm_source=footer>
> .
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/01C05F93-94B6-4412-A639-6B22D5AD6415%40googlemail.com
>
> <https://groups.google.com/d/msgid/ansible-project/01C05F93-94B6-4412-A639-6B22D5AD6415%40googlemail.com?utm_medium=email&utm_source=footer>
> .
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/MWKwiX9ICR8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/SYBP282MB3525B92047533293BE36CDDBAD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM
>
> <https://groups.google.com/d/msgid/ansible-project/SYBP282MB3525B92047533293BE36CDDBAD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM?utm_medium=email&utm_source=footer>
> .
>
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/6151BEBF-2100-47D4-BEBB-8B7DAFA015E0%40freenet.de
>
> <https://groups.google.com/d/msgid/ansible-project/6151BEBF-2100-47D4-BEBB-8B7DAFA015E0%40freenet.de?utm_medium=email&utm_source=footer>
> .
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/MWKwiX9ICR8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/SYBP282MB35253A0A94C37D3B58787DE8AD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM
>
> <https://groups.google.com/d/msgid/ansible-project/SYBP282MB35253A0A94C37D3B58787DE8AD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM?utm_medium=email&utm_source=footer>
> .
>
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/0BC29B56-FBB8-4806-9A93-0A26B283FDBC%40freenet.de
>
> <https://groups.google.com/d/msgid/ansible-project/0BC29B56-FBB8-4806-9A93-0A26B283FDBC%40freenet.de?utm_medium=email&utm_source=footer>
> .
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/MWKwiX9ICR8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/SYBP282MB35256C3EEB45432169B04949AD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM
>
> <https://groups.google.com/d/msgid/ansible-project/SYBP282MB35256C3EEB45432169B04949AD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM?utm_medium=email&utm_source=footer>
> .
>
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/3DC0AC14-FEBB-4CD0-A62A-426D8D5B0CB1%40freenet.de
>
> <https://groups.google.com/d/msgid/ansible-project/3DC0AC14-FEBB-4CD0-A62A-426D8D5B0CB1%40freenet.de?utm_medium=email&utm_source=footer>
> .
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/MWKwiX9ICR8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
>
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/SYBP282MB35253DBC1004532300D8D629AD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM
>
> <https://groups.google.com/d/msgid/ansible-project/SYBP282MB35253DBC1004532300D8D629AD4B9%40SYBP282MB3525.AUSP282.PROD.OUTLOOK.COM?utm_medium=email&utm_source=footer>
> .
>
>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/68c52ece-9456-4948-b2ea-e371c0f61760n%40googlegroups.com.