Thanks a lot Todd for making us learn new thing and developing more interest in ansible.
Absolute thanks Todd. On Wednesday, November 1, 2023 at 7:12:16 PM UTC+5:30 Todd Lewis wrote: > The short answer is no. If you want to modify the string with backrefs, > then lineinfile will not create the line if it doesn't exist. > > But step back and consider your options for managing your override.conf > file. > > A. The best scenario is to take total ownership of the entire file, > generating it from variables in your project. You have to take changes into > account as you upgrade kafka etc, but you should do that in any case. > > B. If for some reasons you aren't ready or able to take ownership of the > whole file, then the next best thing is to at least take complete ownership > of the 'Environment="KAFKA…' line, and build it from information in your > project. You can still have appropriate differences on various instances, > but that would all by controlled by data and logic in your Ansible project. > See the example below. > > C. The absolute worst scenario, the situation where you should be saying, > "How can we move to option 'B' or 'A' as soon as possible?", is when you > try to tweak a line that may or may not exist, in a file that may or may > not exist, and that may have unknown variances between hosts for reasons > that (apparently) aren't coming from your configuration management system. > As a stop-gap step in a house-on-fire situation, you've got to do what > you've got to do. But you should stop doing it as soon as possible. > > That's not to say that 'C' doesn't work; it does, and I've managed files > that way for years. But I got in that situation for two reasons: 1) Nobody > told me what I just told you, and 2) I figured my problem was to tweak a > line in a file, and if there was an Ansible module to do just that, then it > must be an okay way to do it. > > Here's a short playbook with variables to inform the line in question. > Clearly you could use group_vars, host_vars, and/or inventory variables to > tailor the result for individual hosts. But I strongly encourage you to > pull the responsibility for that entire line (option 'B') or better yet > that entire file (option 'A') into your Ansible project. > > --- > # sameer-0.yml > - name: KAFKA_JMX options management > hosts: localhost > gather_facts: false > vars: > filename: sameer-0-override.conf > kafka_jmx_opts: > - > '-javaagent:/usr/share/java/kafka/jolokia.jar=port=8778,host=pilot01.test' > - '-Dcom.sun.management.jmxremote' > - '-Dcom.sun.management.jmxremote.authenticate=false' > - '-Dcom.sun.management.jmxremote.ssl=false' > - '-Djava.rmi.server.hostname=pilot01.test' > - '-Dcom.sun.management.jmxremote.rmi.port=1099' > kafka_jmx_opts_str: 'KAFKA_JMX_OPTS={{ kafka_jmx_opts | join(" ") }}' > tasks: > - name: Manage kafka_jms_opts in override.conf > ansible.builtin.lineinfile: > path: '{{ filename }}' > regexp: 'Environment="KAFKA_JMX_OPTS=.*"' > line: 'Environment="{{ kafka_jmx_opts_str }}"' > state: present > create: true # You should have "owner:", "group:", and "mode:" here > too. > > > > On 11/1/23 7:12 AM, Sameer Modak wrote: > > Hello Team, > > I could figure out after 10 tries this is how i got it > > - name: append the line > lineinfile: > path: /tmp/hello.txt > backrefs: yes > regexp: '^(.*KAFKA_JMX_OPTS.*)$"'. <------------------------ > line: '\1 > -javaagent:{{jmxexporter_jar_path}}={{zookeeper_jmxexporter_port}}:{{zookeeper_jmxexporter_config_path}}"'' > > but like can we handle the if else in one line in file > > this will not work when file does not have "KAFKA_JMX_OPTS" in it. Can we > do it in same taks using item 2 regexp > > if sting absent add entire line else if string present append it. > > > > > > > On Wednesday, November 1, 2023 at 4:11:40 PM UTC+5:30 Sameer Modak wrote: > >> We want to only append string >> >> -javaagent:{{jmxexporter_jar_path}}={{zookeeper_jmxexporter_port}}:{{zookeeper_jmxexporter_config_path}}"' >> >> >> to a file override.conf >> if we have "Environment="KAFKA_JMX_OPTS" in it else >> add new line "actual line" >> >> >> Environment="KAFKA_OPTS=-javaagent:{{jmxexporter_jar_path}}={{zookeeper_jmxexporter_port}}:{{zookeeper_jmxexporter_config_path}}"' >> >> So basically append if something is there else add new line. >> >> I m struggling to get regex ryt. >> >> Currently the override.conf file has this below content with jmx >> parameters in it so we want append above line/string to end of the >> existing line >> >> Environment="KAFKA_JMX_OPTS=-javaagent:/usr/share/java/kafka/jolokia.jar=port=8778,host=pilot01.test >> >> -Dcom.sun.management.jmxremote >> -Dcom.sun.management.jmxremote.authenticate=false >> -Dcom.sun.management.jmxremote.ssl=false >> -Djava.rmi.server.hostname=pilot01.test >> -Dcom.sun.management.jmxremote.rmi.port=1099" >> > -- > 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-proje...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/73e8499f-c1f0-4183-88c7-4a55b034b161n%40googlegroups.com > > <https://groups.google.com/d/msgid/ansible-project/73e8499f-c1f0-4183-88c7-4a55b034b161n%40googlegroups.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/7bce2653-6158-457c-b862-2303dd817d0fn%40googlegroups.com.