[ansible-project] Re: how to use ansible tags with conditional includes?
well I guess the number of replies says it all: I must be trying to do something the language does not support. Ansible conditions operate on "include_vars" - include* statements in ansible are dynamic. Similarly, import* statements in ansible are static. Tags are applicable to statically imported constructs; tags are not applicable to dynamic inclusions such as include_roles or include_tasks. Therefore, since ansible conditionals are applicable to dynamic constructs, and ansible tags are applicable to static constructs; the two are mutually exclusive. The language does not support the ability to tag variables that are included via include_vars. Bummer. This begs the question: how does one conditionally include vars files in tagged tasks in ansible?? Anybody? On Friday, May 29, 2020 at 11:24:28 AM UTC-4, misterT1958 wrote: > > Hey folks, > > I have run into a problem that I have not been able to solve -- I hope > somebody out there has had better luck than I have. Here's the setup: > > At the top level directory there is an executive shell script that calls a > playbook, the particular playbook called is based on inputs to the script. > For each playbook there is a corresponding role subdirectory, and each role > subdirectory is further divided into a tasks subdirectory and a vars > subdirectory. There is one vars file corresponding to each task file. Each > role contains a set of unique tasks. If a task is used in more than one > playbook then the tasks and vars files are located under the common > subdirectory. > > > > The structure of my current ansible hierarchy is shown below. Note that > the common subdirectory and the compA and compB subdirectories look similar > to the compN subdirectory, but only the compN subdirectory is shown in > detail for simplification. > > > > top-level directory: > > compA_playbook.yaml > > compB_playbook.yaml > > … > > compN_playbook.yaml > > executive.sh > > roles > > |- common > > |- compA > > |- compB > > … > > |- compN > > |- tasks > > | task1.yaml > > | task2.yaml > > | … > > | taskm.yaml > > |- vars > > task1.yaml > > task2.yaml > > … > > taskm.yaml > > > > A typical playbook looks like this: > > --- > > - hosts: localhost > > vars_files: > > - roles/common/vars/fxn_1.yaml > > > > tasks: > > - name: function 1 > > import_tasks: roles/common/tasks/fxn_1.yaml > > > > … > > > > > > A typical vars file looked like this: > > --- > > fxn_1_bool: true > > fxn_1_id: ddtnn35 > > fxn_1_cfgfile: '"/xyz/home/user2/.functionX.cfg"' > > fxn_1_starttime: "2020-09-30 07:30:00" > > fxn_1_pauseinterval: 1 > > > > > > and a typical task file looked like this: > > --- > > - name: set downtime > > shell: > > > /opt/bin/set_downtime.py {{ item }} downtime add > > -t {{fxn_1_starttime}} -u {{fxn_1_id}}; > > loop: "{{ query('inventory_hostnames', '{{ HPC }}-spares') }}" > > loop_control: > > pause: "{{ fxn_1_pauseinterval }}" > > when: fxn_1_bool > > register: set_fxn_1 > > tags: [ FXN_1 ] > > > > The code is written so that the user may specify that an entire playbook > be executed (by default) OR that one or more tasks within a playbook could > be executed by specifying the appropriate tags (each task has a unique tag). > > > > This setup worked fine until I needed to add conditionals to choose > between var files for certain tasks. I could only get the conditionals to > work by replacing “vars_files:” with “include_vars:”; so now a typical > playbook looks like this: > > --- > > - hosts: localhost > > > > tasks: > >- include_vars: roles/common/vars/fxn_1.yaml > > when: newVar1 == true or newVar2 == true > > > >- include_vars: /some/other/dir/fxn_1.yaml > > when: newVar1 == false and newVar2 == false > > > >- name: function 1 > > import_tasks: roles/common/tasks/fxn_1.yaml > > > > The conditional clauses work fine – the correct var file is chosen based > on the values on newVar1 and newVar2. The problem comes when I use tags. > When I specify a tag the tag is only associated with the task, but NOT > associated with the include_vars, so when the task executes it fails
[ansible-project] how to use ansible tags with conditional includes?
Hey folks, I have run into a problem that I have not been able to solve -- I hope somebody out there has had better luck than I have. Here's the setup: At the top level directory there is an executive shell script that calls a playbook, the particular playbook called is based on inputs to the script. For each playbook there is a corresponding role subdirectory, and each role subdirectory is further divided into a tasks subdirectory and a vars subdirectory. There is one vars file corresponding to each task file. Each role contains a set of unique tasks. If a task is used in more than one playbook then the tasks and vars files are located under the common subdirectory. The structure of my current ansible hierarchy is shown below. Note that the common subdirectory and the compA and compB subdirectories look similar to the compN subdirectory, but only the compN subdirectory is shown in detail for simplification. top-level directory: compA_playbook.yaml compB_playbook.yaml … compN_playbook.yaml executive.sh roles |- common |- compA |- compB … |- compN |- tasks | task1.yaml | task2.yaml | … | taskm.yaml |- vars task1.yaml task2.yaml … taskm.yaml A typical playbook looks like this: --- - hosts: localhost vars_files: - roles/common/vars/fxn_1.yaml tasks: - name: function 1 import_tasks: roles/common/tasks/fxn_1.yaml … A typical vars file looked like this: --- fxn_1_bool: true fxn_1_id: ddtnn35 fxn_1_cfgfile: '"/xyz/home/user2/.functionX.cfg"' fxn_1_starttime: "2020-09-30 07:30:00" fxn_1_pauseinterval: 1 and a typical task file looked like this: --- - name: set downtime shell: > /opt/bin/set_downtime.py {{ item }} downtime add -t {{fxn_1_starttime}} -u {{fxn_1_id}}; loop: "{{ query('inventory_hostnames', '{{ HPC }}-spares') }}" loop_control: pause: "{{ fxn_1_pauseinterval }}" when: fxn_1_bool register: set_fxn_1 tags: [ FXN_1 ] The code is written so that the user may specify that an entire playbook be executed (by default) OR that one or more tasks within a playbook could be executed by specifying the appropriate tags (each task has a unique tag). This setup worked fine until I needed to add conditionals to choose between var files for certain tasks. I could only get the conditionals to work by replacing “vars_files:” with “include_vars:”; so now a typical playbook looks like this: --- - hosts: localhost tasks: - include_vars: roles/common/vars/fxn_1.yaml when: newVar1 == true or newVar2 == true - include_vars: /some/other/dir/fxn_1.yaml when: newVar1 == false and newVar2 == false - name: function 1 import_tasks: roles/common/tasks/fxn_1.yaml The conditional clauses work fine – the correct var file is chosen based on the values on newVar1 and newVar2. The problem comes when I use tags. When I specify a tag the tag is only associated with the task, but NOT associated with the include_vars, so when the task executes it fails because the variables are undefined. I need some way to attach the tag to the variable files as well as the tasks files. I have tried using ansible block, but couldn’t get that to work. Any input appreciated! misterT -- 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/d24686dc-7a92-48dd-8437-f12028400809%40googlegroups.com.
[ansible-project] Re: simple problem has me stumped
Ah, now I see what you mean - got it working! Thanks so much for the hand! kind regards, T On Monday, February 10, 2020 at 4:33:14 PM UTC-5, misterT1958 wrote: > > I have a simple scenario that fails, but I can't figure out why. Here's > the command line invocation and the result: > > [root@cluster-mgmt tasks]# ansible-playbook -i ./hosts main.yml > > PLAY [CLUSTERS team downtime orchestration play] > > > TASK [Gathering Facts] > ** > ok: [localhost] > > TASK [set nagios downtime] > ** > fatal: [localhost]: FAILED! => {"msg": "The task includes an option with > an undefined variable. The error was: 'myTestVar' is undefined\n\nThe error > appears to have been in > '/autofs/nccs-svm1_home1/tw58/SANDBOX/ANSIBLE_VARDIR/testVar/tasks/main.yml': > line 9, column 5, but may\nbe elsewhere in the file depending on the exact > syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: > set nagios downtime\n^ here\n"} > [WARNING]: Could not create retry file > '/autofs/nccs-svm1_home1/tw58/SANDBOX/ANSIBLE_VARDIR/testVar/tasks/main.retry'. > >[Errno 13] Permission denied: > > u'/autofs/nccs-svm1_home1/tw58/SANDBOX/ANSIBLE_VARDIR/testVar/tasks/main.retry' > > > PLAY RECAP > ** > localhost : ok=1changed=0unreachable=0failed=1 > > The playbook is simple: > > --- > # tasks file for testVar > > - hosts: localhost > name: CLUSTERS team downtime orchestration play > gather_facts: true > > tasks: > - name: set nagios downtime > debug: > msg: "the value of the variable {{ myTestVar }}" > > and the variable definition file is even more simple: > > [root@cluster-mgmt vars]# more main.yml > --- > # vars file for testVar > myTestVar: 58 > > This is my directory tree: > > [root@cluster-mgmt testVar]# tree > . > ├── ansible.cfg > ├── defaults > │ └── main.yml > ├── files > ├── handlers > │ └── main.yml > ├── hosts > ├── meta > │ └── main.yml > ├── README.md > ├── tasks > │ ├── ansible.cfg -> ../ansible.cfg > │ ├── hosts -> ../hosts > │ └── main.yml > ├── templates > ├── tests > │ ├── inventory > │ └── test.yml > └── vars > └── main.yml > > Been staring at this all day, trying different things, always get the > "undefined variable"!! > > Does anybody out there see the problem? > > Thanks > T > -- 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/b4e3325a-a426-4a04-9b62-5738b36b5bb9%40googlegroups.com.
[ansible-project] simple problem has me stumped
I have a simple scenario that fails, but I can't figure out why. Here's the command line invocation and the result: [root@cluster-mgmt tasks]# ansible-playbook -i ./hosts main.yml PLAY [CLUSTERS team downtime orchestration play] TASK [Gathering Facts] ** ok: [localhost] TASK [set nagios downtime] ** fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'myTestVar' is undefined\n\nThe error appears to have been in '/autofs/nccs-svm1_home1/tw58/SANDBOX/ANSIBLE_VARDIR/testVar/tasks/main.yml': line 9, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: set nagios downtime\n^ here\n"} [WARNING]: Could not create retry file '/autofs/nccs-svm1_home1/tw58/SANDBOX/ANSIBLE_VARDIR/testVar/tasks/main.retry'. [Errno 13] Permission denied: u'/autofs/nccs-svm1_home1/tw58/SANDBOX/ANSIBLE_VARDIR/testVar/tasks/main.retry' PLAY RECAP ** localhost : ok=1changed=0unreachable=0failed=1 The playbook is simple: --- # tasks file for testVar - hosts: localhost name: CLUSTERS team downtime orchestration play gather_facts: true tasks: - name: set nagios downtime debug: msg: "the value of the variable {{ myTestVar }}" and the variable definition file is even more simple: [root@cluster-mgmt vars]# more main.yml --- # vars file for testVar myTestVar: 58 This is my directory tree: [root@cluster-mgmt testVar]# tree . ├── ansible.cfg ├── defaults │ └── main.yml ├── files ├── handlers │ └── main.yml ├── hosts ├── meta │ └── main.yml ├── README.md ├── tasks │ ├── ansible.cfg -> ../ansible.cfg │ ├── hosts -> ../hosts │ └── main.yml ├── templates ├── tests │ ├── inventory │ └── test.yml └── vars └── main.yml Been staring at this all day, trying different things, always get the "undefined variable"!! Does anybody out there see the problem? Thanks T -- 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/d66b7858-ee9a-4c9a-ba0c-8c885fd1af88%40googlegroups.com.
[ansible-project] module like expect, but on windows
Ansible folk I have a package that I want Ansible to download and install on my host computer. After the download, the next step in the installation process is a configure - first step in the configure process is to respond to the question: "Do you accept the license?". If host box was Linux I could use the expect module. But my host is a Windows box. What is the Ansible *Windows* equivalent of expect? Thanks Tony -- 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 post to this group, send email to ansible-project@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fe3edd50-9872-45c3-826e-15f276270ad6%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.