Thanks, Will Yes, I may have exaggerated a bit by going with 'avoid at all cost'... :-) You are right about the modules > command > shell statement. But that still made me think the solution I used was not the way to go.
"(though you might have some chicken-and-egg constraints guiding you down your current path.)" Yup, that's exactly the reason. For now I have to rely on collecting that information from an already established way of locating our servers. Eventually will migrate away from that and have something more coupled with Ansible. Thank you for your suggestions, by the way... Regards, Alex On Thu, Mar 7, 2024 at 11:25 AM Will McDonald <wmcdon...@gmail.com> wrote: > I suspect the broad notion that shell == bad and native statements == good > is probably just conflation/extrapolation. And human nature :) > > The generally received wisdom when I was taught Ansible by Ansible folk > was native modules > command > shell. > > Note this is not 'no shell at any costs', but rather, try and avoid shell > because it can be fragile with globbing, expansion, unexpected behaviour > with weird file names etc. > > Second thought, on this topic is you could collapse the shell to command > here like so: > > - name: Collect server location command > ansible.builtin.command: /bin/awk '/{{ inventory_hostname }}/ { print > $3 }' {{ server_list_file}} > register: command_result > changed_when: ( command_result.rc != 0 ) > > And finally, while the general pattern probably fits your purposes Alex, > it kinda feels like getting stuff that might more naturally be considered > inventory information from flat/static files? That's probably totally cool > for your current needs but I'd potentially think about exposing location as > a fact, either through inference from dynamic inventory/inventory plugins, > or baking into custom facts so the host can report its location (though you > might have some chicken-and-egg constraints guiding you down your current > path.) > > > On Thu, 7 Mar 2024 at 17:42, Alex Wanderley <alex.wander...@edmonton.ca> > wrote: > >> Thanks a lot for the insights, Tod. Very appreciated... >> And suggestions as well. I'll give them a try. >> >> I'm still in an Ansible learning process and sometimes I also come upon >> that curiosity of yours... >> How come the notion of avoiding shell "at all cost" exists even for very >> specific situations, like this one. >> That's why I, based on the "no shell" assumption, tried to make my >> playbook better. >> >> Regards, >> >> Alex >> >> On Thu, Mar 7, 2024 at 9:54 AM Todd Lewis <uto...@gmail.com> wrote: >> >>> I'm curious how these notions caught on that very clearly expressed >>> shell expressions are inherently "bad", and that unfathomably complex >>> jinja2 expressions are inherently "better". >>> >>> I seriously doubt you're going to find a more maintainable, all-Ansible >>> expression to accomplish what you're doing. >>> >>> However, I would suggest a couple alterations. >>> (A) I'd make that "changed_when:" into a "failed_when:", and >>> (B) I'd add "changed_when: false" (because it doesn't ever change >>> anything). >>> (C) I might also add a "vars:" to your set_fact task that maps >>> "location[XYZ]" to your smtp.conf expressions: >>> >>> vars: >>> locmap: >>> locationX: snmpd.conf-X >>> locationY: snmpd.conf-Y >>> locationZ: snmpd.conf-Z >>> >>> Then your set_fact becomes "snmpd_conf: '{{ >>> locmap[grep_result.stdout_lines[0]] }}' >>> That way you don't have to change "code" to update your location map >>> data. >>> >>> On 3/7/24 11:02 AM, Alex Wanderley wrote: >>> >>> Hello, >>> >>> (And apologies if this is something too trivial...) >>> >>> I have a working playbook that searches and collects a string from a >>> file and, based on that string, sets another variable. >>> But I'm retrieving the string using grep/awk: >>> >>> - name: Collect server location >>> ansible.builtin.shell: "/bin/grep {{ inventory_hostname }} {{ >>> server_list_file }} | /bin/awk '{print $3}'" >>> register: grep_result >>> changed_when: ( grep_result.rc != 0 ) >>> >>> "{{ server_list_file }}" points to a file that looks like this: >>> serverA flavor locationX owner >>> serverB flavor locationY owner >>> serverC flavor locationZ owner >>> >>> For completeness, this is how I'm using "grep_result": >>> - name: Set snmpd config file >>> ansible.builtin.set_fact: >>> snmpd_conf: "{% if grep_result.stdout == 'locationX' %}snmpd.conf-X\ >>> {% elif grep_result.stdout == 'locationY' %}snmpd.conf-Y\ >>> {% else %}snmpd.conf-Z\ >>> {% endif %}" >>> >>> How could I, as simply as using the command/shell modules, accomplish >>> the same results without resourcing to shell commands? >>> >>> Thanks a lot, >>> >>> Alex >>> >>> -- >>> >>> >>> >>> [image: Edmonton_sig_RGB_S.jpg] >>> >>> Alex Wanderley >>> >>> Application and Infrastructure Analyst II Server Solutions & Automation >>> >>> Financial and Corporate Services | Open City and Technology >>> >>> >>> >>> 780-496-4156 Office >>> >>> 780-819-0273 Mobile >>> >>> >>> >>> City of Edmonton >>> >>> Century Place, 19th Floor >>> >>> 9803 102A Avenue NW >>> >>> Edmonton AB, T5J 3A3 >>> >>> >>> >>> All information contained in this email post is proprietary to the City >>> of Edmonton, confidential and intended only for the addressed recipient. If >>> you have received this post in error, please disregard the contents, inform >>> the sender of the misdirection, and remove it from your system. The >>> copying, dissemination or distribution of this email, if misdirected, is >>> strictly prohibited. >>> >>> *The contents of this message and any attachment(s) are confidential, >>> proprietary to the City of Edmonton, and are intended only for the >>> addressed recipient. If you have received this in error, please disregard >>> the contents, inform the sender of the misdirection, and remove it from >>> your system. The copying, dissemination, or distribution of this message, >>> if misdirected, is strictly prohibited.* -- >>> 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/CADp8UUQB4UhuBYY1ujzs%2BJpyyV8WTr6-xu4bV8uFNWz3vmMB1g%40mail.gmail.com >>> <https://groups.google.com/d/msgid/ansible-project/CADp8UUQB4UhuBYY1ujzs%2BJpyyV8WTr6-xu4bV8uFNWz3vmMB1g%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >>> >>> -- >>> Todd >>> >>> -- >>> 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/a45140e9-6b7f-44e2-a4e4-4240d76f2fba%40gmail.com >>> <https://groups.google.com/d/msgid/ansible-project/a45140e9-6b7f-44e2-a4e4-4240d76f2fba%40gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >> >> >> -- >> >> >> >> [image: Edmonton_sig_RGB_S.jpg] >> >> Alex Wanderley >> >> Application and Infrastructure Analyst II >> Server Solutions & Automation >> >> Financial and Corporate Services | Open City and Technology >> >> >> >> 780-496-4156 Office >> >> 780-819-0273 Mobile >> >> >> >> City of Edmonton >> >> Century Place, 19th Floor >> >> 9803 102A Avenue NW >> >> Edmonton AB, T5J 3A3 >> >> >> >> All information contained in this email post is proprietary to the City >> of Edmonton, confidential and intended only for the addressed recipient. If >> you have received this post in error, please disregard the contents, inform >> the sender of the misdirection, and remove it from your system. The >> copying, dissemination or distribution of this email, if misdirected, is >> strictly prohibited. >> >> *The contents of this message and any attachment(s) are confidential, >> proprietary to the City of Edmonton, and are intended only for the >> addressed recipient. If you have received this in error, please disregard >> the contents, inform the sender of the misdirection, and remove it from >> your system. The copying, dissemination, or distribution of this message, >> if misdirected, is strictly prohibited.* >> >> -- >> 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/CADp8UUQCOhc_5GiJb0YMduvkFpwcU9zKqAFS3MnSO3T3y6qDxg%40mail.gmail.com >> <https://groups.google.com/d/msgid/ansible-project/CADp8UUQCOhc_5GiJb0YMduvkFpwcU9zKqAFS3MnSO3T3y6qDxg%40mail.gmail.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 ansible-project+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/CAKtKohSt77zCi2ueEjiAQmq6oa6dTF02OJbsfqw%2B77MVYnzAQA%40mail.gmail.com > <https://groups.google.com/d/msgid/ansible-project/CAKtKohSt77zCi2ueEjiAQmq6oa6dTF02OJbsfqw%2B77MVYnzAQA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- [image: Edmonton_sig_RGB_S.jpg] Alex Wanderley Application and Infrastructure Analyst II Server Solutions & Automation Financial and Corporate Services | Open City and Technology 780-496-4156 Office 780-819-0273 Mobile City of Edmonton Century Place, 19th Floor 9803 102A Avenue NW Edmonton AB, T5J 3A3 All information contained in this email post is proprietary to the City of Edmonton, confidential and intended only for the addressed recipient. If you have received this post in error, please disregard the contents, inform the sender of the misdirection, and remove it from your system. The copying, dissemination or distribution of this email, if misdirected, is strictly prohibited. -- *The contents of this message and any attachment(s) are confidential, proprietary to the City of Edmonton, and are intended only for the addressed recipient. If you have received this in error, please disregard the contents, inform the sender of the misdirection, and remove it from your system. The copying, dissemination, or distribution of this message, if misdirected, is strictly prohibited.* -- 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/CADp8UURMYwgnCyfkqBmjGPZki__zCRSNG%2B20691exjVNaCu-1w%40mail.gmail.com.