Ansible lineinfile module special character - ansible

When i try to add in sudoers at eof the following file with lineinfile module the special character "!" not added the line any more. Without the special character is added ok.
Any ideea?
ansible 10.x.x.x -m lineinfile -a "path=/etc/sudoers insertafter=EOF line='Defaults:user !requiretty' validate='/usr/sbin/visudo -cf %s' state=present"

Kind of tricky, but works
line: "Defaults:user {{ '!' }}requiretty"
You can use it in a playbook. One would run short of quotation marks in the command-line .

Related

Ansible 2.9 - Escape a single "\" backslash being passed into win_command from variable

vars:
- windowsLogonAccount: NT AUTHORITY\SYSTEM
tasks:
- name: Install Agent via CMD
win_command: "config.cmd --windowsLogonAccount '{{ windowsLogonAccount }}'"
args:
chdir: "c:/agent"
I'm attempting to get just a single backslash to pass via Ansible to Windows. I've tried double backslashes "\\" and I've tried {{ var | safe }}, but without luck. I'm hoping somebody in the community can point me in the right direction. I can't believe it is as complicated as some of the other examples I've seen using replace and regex...
Everything I try I keep getting a double backslash:
2021-10-19T04:45:52.8643709Z TASK [Get Variable Output to Screen] *******************************************
2021-10-19T04:45:52.9042957Z "msg": "My Variable Test Area 'NT AUTHORITY\\SYSTEM'"
I came here having not the exact same problem, but a similar one (passing an escaped backslash (\\) to the command), and my solution might also help here. When inserting variables into the command for command or win_command, it is always good to pipe the variable to the quote filter to preserve the content of the variable as is. In your case:
win_command: "config.cmd --windowsLogonAccount {{ windowsLogonAccount | quote }}"
----Resolved----
So this is a unique thing with Ansible Outputs to Terminal. They write output in valid JSON and thus "auto" escape special characters like backslashes in the output. But the actual value passed during run-time does not contain both back slashes. I'm posting this in case any other Ansible noobs run into this surprise.
Example output to screen:
2021-10-19T04:46:11.6003956Z: [COMPUTERNAME]: CHANGED! => {"changed": true, "cmd": "config.cmd --unattended --windowsLogonAccount 'NT AUTHORITY\SYSTEM'
Actual output to host:
config.cmd --unattended --windowsLogonAccount 'NT AUTHORITY\SYSTEM'

How to use escape characters in Ansible shell/grep command

I need to extract a hostname from a URL in a file on my remote host to then use that value in another task.
I have the following task in my playbook:
- name: Extract server name
shell: "grep ^serverURL=https:\/\/(.*)\/XMLRPC <filename>"
register: <variable>
But when I try and run it I get the following error (cut down to relevant bits):
Found unknown escape character. The offending line appears to be:
- name: Extract server name
shell: "grep ^serverURL=https:\/\/(.*)\/XMLRPC <filename>"
^ here
It seems there is a value started with a quote and the YAML parser is expecting to see the line ended with the same kind of quote.
Obviously it does start and finish with double quotes but the carat is at the escape character in my grep query, so it seems reasonable to think that this is the cause of the problem; if I remove the backslashes my grep query doesn't return anything.
How can I escape this correctly please?

ansible syntax for lineinfile search and replace

My current etc/group wheel is setup as the following
wheel:x:10:admin,sshgroup,domaingroup
I need it be
wheel:x:10:admin4,sshlocal,domainusers,domainadmins
- name: add a string to the new file
lineinfile:
dest=/etc/group
backrefs=true
regexp='wheel:x:10:'
line='wheel:x:10:admin4,sshlocal,domainusers,domainadmins'
state = present
error:
The offending line appears to be:
line='wheel:x:10:admin4,sshlocal,domainusers,domainadmins'
state = present
^ here
I tried both with and without escaping : and commmas but to no avail. Can you please suggest. Im using ansible 2.2.1.0
Nevermind guys, playbook ran this time. I think last line where state = present is listed, i looked like used tab spaces instead of spaces for yaml. After I deleted that line and readded it with spaces, it works now.
cat /etc/group | grep wheel
wheel:x:10:admin4,sshlocal,domainusers,domainadmins
Apparently, single quote is good enough to escape: characters in YAML - which is good.

How does --inventory option in Ansible work when it is given value with a trailing comma?

Man pages for ansible and ansible-playbook define -i option as:
-i PATH, --inventory=PATH
The PATH to the inventory hosts file, which defaults to
/etc/ansible/hosts.
Yet to run on a local system the following syntax is used in examples:
ansible -i "localhost," -c local -m ping localhost
What exactly is this "localhost," with comma at the end (otherwise it is treated as filename) and how does it relate to PATH?
This is (now, at least) a documented feature. From the man page:
-i, --inventory, --inventory-file
specify inventory host path or comma separated host list. --inventory-file is deprecated
(emphasis added)
What's still not in the manual is that "comma separated host list" means that you need to add a comma even if the "list" is a single item, to distinguish between "target a single host called hostname":
$ ansible -i 'hostname,' ...
and "load inventory from a file called hostname":
$ ansible -i 'hostname,' ...
If anyone out there has time, maybe you could submit a pull request to change the help text to explain this (and to add a hyphen in "comma-separated", but maybe that's just me..)
According to Michael DeHann, who created Ansible, the comma trick you're referring to is a hack that shouldn't be relied upon. It's a hack to run Ansible without an inventory file, for cases where you're going to run against localhost. That way you don't actually have to create an inventory file that just lists localhost.
Actually, when you want to run commands against a specific host, don't add -i, instead, run it in the following way:
ansible localhost -m ping
Use -i only to specify the path for dynamic inventory or hosts.

Remove a line from a file using ansible?

I have a file called authorized_keys. I need to delete a particular line using an Ansible script.
The problem is when I try to remove a line that includes a '+' character. Ansible is not able to remove this line.
e.g authorized_keys file is:
.....
abhi foo+bar saken
......(EOF)
I want to remove the abhi foo+bar saken line but Ansible is not removing this line because of the + character.
I am able to remove lines that do not contain a + character .
Task:
- name: Delete keys in sysadmin/.ssh/authoriezd_keys
lineinfile: dest=/home/{{name}}/.ssh/authorized_keys
state=absent
regexp='^{{key}}$'
PS: I am using Ansible's lineinfile module
The problem probably is that + has a meaning in a regular expression.
You should be able to fix it by escaping the +. If you can't do that from the source where {{ key }} is defined, you can escape it with the replace Jinja filter:
- name: Delete keys in sysadmin/.ssh/authoriezd_keys
lineinfile: dest=/home/{{name}}/.ssh/authorized_keys
state=absent
regexp='^{{ key | replace("+", "\+") }}$'
You might run into more problems if {{ key }} contains other characters which have a meaning in regular expressions. If that's the case I think the safe way would be to create your own filter plugin where you simply return the input passed through re.escape.

Resources