I need help on Ansible Can anyone help me to share example to insert a new line . . .

Chandra Mohanty:
I need help on Ansible
Can anyone help me to share example to insert a new line <XYZ/> after line <ABC/> in a config file. The lines are in <> brackets

BR:

  • name: “Add line after ABC”
    ansible.builtin.lineinfile:
    path: /etc/ssh/sshd_config
    line: “XYZ/”
    insertafter: “ABC/”

Chandra Mohanty:
I get the below error

16:15:21 [WARNING]: No python interpreters found for host 192.xxx.xx.xx (tried
16:15:21 [‘/usr/bin/python’, ‘python3.7’, ‘python3.6’, ‘python3.5’, ‘python2.7’,
16:15:21 ‘python2.6’, ‘/usr/libexec/platform-python’, ‘/usr/bin/python3’, ‘python’])
16:15:21 fatal: [192.xxx.xx.xx]: FAILED! => {“ansible_facts”: {“discovered_interpreter_python”: “/usr/bin/python”}, “changed”: false, “module_stderr”: "Exception calling "Create" with "1" argument(s): "At line:4 char:21\r\n+ def _ansiballz_main():\r\n+ ~\r\nAn expression was expected after ‘(’.\r\nAt line:13 char:27\r\n+ except (AttributeError, OSError):\r\n+ ~\r\nMissing argument in parameter list.\r\nAt line:15 char:7\r\n+ if scriptdir is not None:\r\n+ ~\r\nMissing ‘(’ after ‘if’ in if statement.\r\nAt line:22 char:7\r\n+ if sys.version_info < (3,):\r\n+ ~\r\nMissing ‘(’ after ‘if’ in if statement.\r\nAt line:22 char:30\r\n+ if sys.version_info < (3,):\r\n+ ~\r\nMissing expression after ‘,’.\r\nAt line:22 char:25\r\n+ if sys.version_info < (3,):\r\n+ ~\r\nThe ‘<’ operator is reserved for future use.\r\nAt line:27 char:34\r\n+ def invoke_module(modlib_path, temp_path, json_params):\r\n+ ~\r\nMissing argument in parameter list.\r\nAt line:28 char:40\r\n+ z = zipfile.ZipFile(modlib_path, mode=‘a’)\r\n+ ~\r\nMissing argument in parameter list.\r\nAt line:31 char:33\r\n+ zinfo = zipfile.ZipInfo()\r\n+ ~\r\nAn expression was expected after ‘(’.\r\nAt line:34 char:25\r\n+ z.writestr(zinfo, sitecustomize)\r\n+ ~\r\nMissing argument in parameter list.\r\nNot all parse errors were reported. Correct the reported errors and try again."\r\nAt line:6 char:1\r\n+ $exec_wrapper = [ScriptBlock]::Create($split_parts[0])\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : NotSpecified: (:slight_smile: [], MethodInvocationException\r\n + FullyQualifiedErrorId : ParseException\r\n \r\nThe expression after ‘&’ in a pipeline element produced an object that was not valid. It must result in a command \r\nname, a script block, or a CommandInfo object.\r\nAt line:7 char:2\r\n+ &$exec_wrapper\r\n+ ~~~~~~~~~~~~~\r\n + CategoryInfo : InvalidOperation: (:slight_smile: [], RuntimeException\r\n + FullyQualifiedErrorId : BadExpression\r\n ", “module_stdout”: “”, “msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”, “rc”: 1}

Andrei Andriushin:
@Chandra Mohanty Hi!
Looks like a mistype. Can you share playbook and config file example?
It is also a good practice to use dynamic jinja templates with Ansible

Chandra Mohanty:

  • name: “Insert new tag RSWindowsBasic after RSWindowsNTLM present in the config file”
    tags: authtaginfile
    ansible.builtin.lineinfile:
    path: C:/rsreportserver.config
    line: “RSWindowsBasic/”
    insertafter: “RSWindowsNTLM/”
    backrefs: yes
    register: authtaginfile

Chandra Mohanty:
<Configuration>
<Authentication>
<AuthenticationTypes>
<WindowsNTLM/>
</AuthenticationTypes>
</Authentication>
</Configuration>

Chandra Mohanty:
I want to insert a tag <WindiwsBasic/> after <WindowsNTLM/>

Chandra Mohanty:
hi @Andrei Andriushin / @BR

Andrei Andriushin:

- name: "Insert new tag"
  tags: authtaginfile
  ansible.builtin.lineinfile:
   path: 'C:\rsreportserver.config'
   line: '&lt;WindowsBasic/&gt;'
   insertafter: '&lt;WindowsNTLM/&gt;'
  register: authtaginfile

Andrei Andriushin:
Try this one. As insertafter looks for a regular expression I’m not sure this will work. I would strongly recommend template module for this example, because lineinfile can make unpredictable changes another time if file will be changed from outside
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html

Chandra Mohanty:
ok thanks let me check