Issue with adding a "#" sign using Ansible lineinfile module - python

I am trying to add a # sign in front of a line under /etc/auto.master file as follow
name: Set auto.master step 1
lineinfile:
path: /etc/auto.master
regexp: '^+auto.master'
line: '^#+auto.master'
However, when I run my playbook, this is what I get
FAILED! => {"changed": false, "module_stderr": "Shared connection to ifmliae1p.bbh.com closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n 5.94-16341-164301267793697/AnsiballZ_lineinfile.py\", line 102, in \r\n _ansiballz_main()\r\n File \"/root/.ansible/tmp/ansible-tmp-1590180565.94-16341-164301267793697/Ansibal\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/root/.ansible/tmp/ansible-tmp-1590180565.94-16341-164301267793697/AnsiballZ_lineinfile.py\", line 40, in invokele.modules.files.lineinfile', init_globals=None, run_name='main', alter_sys=True)\r\n File \"/usr/lib64/python2.7/runpy.py\", line 176, in run_module\r\n fname, loader, pkg_name)\ne 82, in _run_module_code\r\n mod_name, mod_fname, mod_loader, pkg_name)\r\n File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code\r\n exec code in run_globals\r\n File _lineinfile_payload.zip/ansible/modules/files/lineinfile.py\", line 573, in \r\n File \"/tmp/ansible_lineinfile_payload_KyHa6O/ansible_lineinfile_payload.zip/ansible/modules/file\"/tmp/ansible_lineinfile_payload_KyHa6O/ansible_lineinfile_payload.zip/ansible/modules/files/lineinfile.py\", line 277, in present\r\n File \"/usr/lib64/python2.7/re.py\", line 190, in r\n File \"/usr/lib64/python2.7/re.py\", line 242, in _compile\r\n raise error, v # invalid expression\r\nsre_constants.error: nothing to repeat\r\n", "msg": "MODULE FAILURE\nSee stdo
I am not sure why it doesn't work. The examples on ansible documentation page seems to work fine.

Apparently, my thinking was going sideways. I thought this line was the issue
line: '^#+auto.master'
But this is the culprit
regexp: '^+auto.master'
The plus sign has to be escaped like so.
regexp: '^\+auto.master'
line: '#+auto.master'

Some possible issues:
YAML file wrong indentation
unescaped characters

Related

How do I pull change records from ServiceNow for a specific system?

I'm trying to automate patching and need to find a way to pull change records from ServiceNow for specific servers and find out if a change window is open for that server.
I tried doing this in Ansible but so far I've had no luck. I'm wondering if there is a better way to do this? Maybe through Python or just something simple with using curl and bash? I've heard of some people having luck with rest APIs but I know very little about it.
Here is an example of my playbook using the snow_record module:
- hosts: localhost
connection: local
gather_facts: no
ignore_errors: True
tasks:
- name: Grab a user record
snow_record:
username: testuser
password: somepass
instance: myinstance
state: present
number: CHG9999
table: change_request
lookup_field: cmdb_ci.name=someservername
Error output:
The full traceback is:
Traceback (most recent call last):
File "/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py", line 113, in <module>
_ansiballz_main()
File "/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py", line 105, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py", line 48, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/tmp/ansible_snow_record_payload_0p5jn8/__main__.py", line 332, in <module>
File "/tmp/ansible_snow_record_payload_0p5jn8/__main__.py", line 328, in main
File "/tmp/ansible_snow_record_payload_0p5jn8/__main__.py", line 317, in run_module
AttributeError: 'module' object has no attribute 'UnexpectedResponse'
fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n File \"/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py\", line 113, in <module>\n _ansiballz_main()\n File \"/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py\", line 105, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py\", line 48, in invoke_module\n imp.load_module('__main__', mod, module, MOD_DESC)\n File \"/tmp/ansible_snow_record_payload_0p5jn8/__main__.py\", line 332, in <module>\n File \"/tmp/ansible_snow_record_payload_0p5jn8/__main__.py\", line 328, in main\n File \"/tmp/ansible_snow_record_payload_0p5jn8/__main__.py\", line 317, in run_module\nAttributeError: 'module' object has no attribute 'UnexpectedResponse'\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
I tried snow_record_find. A module I found here.
Playbook:
- hosts: mytest
tasks:
- name: Grab a user record
snow_record_find:
username: testuser
password: somepass
instance: someinstance
#state: present
#number: null??
table: change_request
query:
AND:
equals:
active: "True"
type: "standard"
u_change_stage: "80"
contains:
u_template: "MY-Template"
Error output from that:
fatal: []: FAILED! => {
"changed": false,
"module_stderr": "Shared connection to closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/root/.ansible/tmp/ansible-tmp-1553019300.21-10858374496170/AnsiballZ_snow_record_find.py\", line 113, in <module>\r\n _ansiballz_main()\r\n File \"/root/.ansible/tmp/ansible-tmp-1553019300.21-10858374496170/AnsiballZ_snow_record_find.py\", line 105, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/root/.ansible/tmp/ansible-tmp-1553019300.21-10858374496170/AnsiballZ_snow_record_find.py\", line 48, in invoke_module\r\n imp.load_module('__main__', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_snow_record_find_payload_j9P62r/__main__.py\", line 260, in <module>\r\n File \"/tmp/ansible_snow_record_find_payload_j9P62r/__main__.py\", line 257, in main\r\n File \"/tmp/ansible_snow_record_find_payload_j9P62r/__main__.py\", line 249, in run_module\r\n File \"/usr/lib/python2.7/site-packages/pysnow/legacy_request.py\", line 91, in _all_inner\r\n yield self._get_content(response)\r\n File \"/usr/lib/python2.7/site-packages/pysnow/legacy_request.py\", line 332, in _get_content\r\n server_error['summary'], server_error['details']\r\npysnow.legacy_exceptions.UnexpectedResponse: Unexpected HTTP GET response code. Expected 200, got 401\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
Unfortunately, what you're attempting is not easy. There is not an existing API in ServiceNow that will return the next available maintenance window for a given Configuration Item (CI / Server).
You could reverse engineer the Maintenance schedule functionality and create your own Scripted API on the ServiceNow platform to complete, but that is likely not worth the effort.
Just FYI, you can use the REST API Explorer in ServiceNow to get code snippets for various languages, Python included.
And finally, there's some good REST Web Service tutorials available as a part of ServiceNow's Developer program, through which you're also able to get a free dedicated ServiceNow instance to play with.

Python Behave BDD Tutorial Error on Windows

So, I`m comparing some python test frameworks and came across behave. Thought it was interesting and worth a test drive.
Followed the steps on the tutorial, available at:
https://behave.readthedocs.io/en/stable/tutorial.html
When I ran the behave command on Powershell (Win10 and Python 2.7.10), I got the following error:
Exception TypeError: compile() expected string without null bytes
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\lib\site-packages\behave\__main__.py", line 187, in <module>
sys.exit(main())
File "C:\Python27\lib\site-packages\behave\__main__.py", line 183, in main
return run_behave(config)
File "C:\Python27\lib\site-packages\behave\__main__.py", line 127, in run_behave
failed = runner.run()
File "C:\Python27\lib\site-packages\behave\runner.py", line 804, in run
return self.run_with_paths()
File "C:\Python27\lib\site-packages\behave\runner.py", line 809, in run_with_paths
self.load_step_definitions()
File "C:\Python27\lib\site-packages\behave\runner.py", line 796, in load_step_definitions
load_step_modules(step_paths)
File "C:\Python27\lib\site-packages\behave\runner_util.py", line 412, in load_step_modules
exec_file(os.path.join(path, name), step_module_globals)
File "C:\Python27\lib\site-packages\behave\runner_util.py", line 385, in exec_file
code = compile(f.read(), filename2, "exec", dont_inherit=True)
TypeError: compile() expected string without null bytes
Has anyone encountered this error while trying to run behave? (Found some threads online related mainly to flask issues but I couldn't solve the problem)
Answering my own question here.
It was an encoding problem.
Sublime was saving my files with an encoding different from UTF-8.
File -> Save with Encoding -> UTF-8 did the trick.

Robot framework [ ERROR ] Unexpected error: IOError: [Errno 0] Error

I have this weird error that I have never seen before. I ran an RF automation regression test using Chrome and after a while, I got this error which basically stops the test execution and does not provide any test results.
I also noticed this new text line that I haven't seen before during text execution:
DevTools listening on
ws://127.0.0.1:12306/devtools/browser/df1c8bcc-867c-41bc-a9b4-c364bdfafec2
(The test scripts have been used before & passed without any issues)
W8-BENE date format - ddMMMMMyyyyDot :: This is to test the differ...
DevTools listening on
ws://127.0.0.1:12537/devtools/browser/6d32afb6-90f2-46d4-a18a-001fa61d2444
[ ERROR ] Unexpected error: IOError: [Errno 0] Error Traceback (most
recent call last): File
"c:\python27\lib\site-packages\robot\utils\application.py", line 83,
in _execute
rc = self.main(arguments, **options)
File "c:\python27\lib\site-packages\robot\run.py", line 445, in main
result = suite.run(settings)
File "c:\python27\lib\site-packages\robot\running\model.py", line
248, in run
self.visit(runner)
File "c:\python27\lib\site-packages\robot\model\testsuite.py", line
161, in visit
visitor.visit_suite(self)
File "c:\python27\lib\site-packages\robot\model\visitor.py", line
87, in visit_suite
suite.tests.visit(self)
File "c:\python27\lib\site-packages\robot\model\itemlist.py", line
76, in visit
item.visit(visitor)
File "c:\python27\lib\site-packages\robot\model\testcase.py", line
74, in visit
visitor.visit_test(self)
File "c:\python27\lib\site-packages\robot\running\runner.py", line
151, in visit_test
result)
File "c:\python27\lib\site-packages\robot\running\runner.py", line
182, in _run_teardown
exception = self._run_setup_or_teardown(teardown)
File "c:\python27\lib\site-packages\robot\running\runner.py", line
201, in _run_setup_or_teardown
StepRunner(self._context).run_step(data, name=name)
File "c:\python27\lib\site-packages\robot\running\steprunner.py",
line 57, in run_step
return runner.run(step, context)
File
"c:\python27\lib\site-packages\robot\running\librarykeywordrunner.py",
line 56, in run
return return_value File "c:\python27\lib\site-packages\robot\running\statusreporter.py", line
54, in exit
context.end_keyword(result)
File "c:\python27\lib\site-packages\robot\running\context.py", line
179, in end_keyword
self.output.end_keyword(keyword)
File "c:\python27\lib\site-packages\robot\output\output.py", line
65, in end_keyword
LOGGER.end_keyword(kw)
File "c:\python27\lib\site-packages\robot\output\logger.py", line
195, in end_keyword
logger.end_keyword(keyword)
File
"c:\python27\lib\site-packages\robot\output\console\verbose.py", line
62, in end_keyword
self._writer.keyword_marker(kw.status)
File
"c:\python27\lib\site-packages\robot\output\console\verbose.py", line
136, in keyword_marker
self._clear_status()
File
"c:\python27\lib\site-packages\robot\output\console\verbose.py", line
124, in _clear_status
self._write_info()
File
"c:\python27\lib\site-packages\robot\output\console\verbose.py", line
90, in _write_info
self._stdout.write(self._last_info)
File
"c:\python27\lib\site-packages\robot\output\console\highlighting.py",
line 51, in write
self.stream.write(console_encode(text, stream=self.stream))
W8-BENE date format - ddMMMMMyyyyDot :: This is to test the differ...
C:\Development\robot-scripts\WebApp>
Has anyone had the same issue? I can't seem to find any solution. I uninstalled and reinstalled everything even update all the web drivers.
I had the same error after update to WIN 10. Just did fresh python install and it works fine for me. also look here: https://github.com/robotframework/robotframework/issues/2709

gclient sync failed when downloading chromium

I'm doing gclient sync in linux and windows and in both I'm getting the same error after around 15 min.
Traceback (most recent call last):
File "/home/jgd/dev/depot_tools/gclient.py", line 2295, in <module>
sys.exit(main(sys.argv[1:]))
File "/home/jgd/dev/depot_tools/gclient.py", line 2281, in main
return dispatcher.execute(OptionParser(), argv)
File "/home/jgd/dev/depot_tools/subcommand.py", line 252, in execute
return command(parser, args[1:])
File "/home/jgd/dev/depot_tools/gclient.py", line 2030, in CMDsync
ret = client.RunOnDeps('update', args)
File "/home/jgd/dev/depot_tools/gclient.py", line 1340, in RunOnDeps
work_queue.flush(revision_overrides, command, args, options=self._options)
File "/home/jgd/dev/depot_tools/gclient_utils.py", line 1047, in run
self.item.run(*self.args, **self.kwargs)
File "/home/jgd/dev/depot_tools/gclient.py", line 766, in run
self.ParseDepsFile()
File "/home/jgd/dev/depot_tools/gclient.py", line 562, in ParseDepsFile
gclient_eval.Check(deps_content, filepath, global_scope, local_scope)
File "/home/jgd/dev/depot_tools/gclient_eval.py", line 240, in Check
result_scope = _gclient_exec(content, global_scope, filename=path)
File "/home/jgd/dev/depot_tools/gclient_eval.py", line 185, in _gclient_exec
_visit_in_module(stmt)
File "/home/jgd/dev/depot_tools/gclient_eval.py", line 169, in _visit_in_module
value = _gclient_eval(node.value, global_scope, filename=filename)
File "/home/jgd/dev/depot_tools/gclient_eval.py", line 146, in _gclient_eval
return _convert(node_or_string)
File "/home/jgd/dev/depot_tools/gclient_eval.py", line 145, in _convert
getattr(node, 'lineno', '<unknown>')))
ValueError: unexpected AST node: <_ast.Num object at 0x1cf5d10> Num(n=1) (file '/home/jgd/dev/jgd/webrtc/src/chromium/src/buildtools/DEPS', line 1)
Error: Command '/usr/bin/python -u src/sync_chromium.py --target-revision 316b880c55452eb694a27ba4d1aa9e74ec9ef342' returned non-zero exit status 1 in /home/jgd/dev/jgd/webrtc
Both are fresh installed and I have no idea about what is happening, it is the first time that this occurs to me. Know anyone how to solve it?
EDIT: The DEPS file that appears in error trace is the following:
recursion = 1
use_relative_paths = True
vars = {
"git_url": "https://chromium.googlesource.com",
"clang_format_rev": "0ed791d1387a3c9146ea6c453c646f3c0fc97784", # r282136
"libcxx_revision": "b1ece9c037d879843b0b0f5a2802e1e9d443b75a", # r256621
"libcxxabi_revision": "0edb61e2e581758fc4cd4cd09fc588b3fc91a653", # r256323
}
deps = {
"clang_format/script":
Var("git_url") + "/chromium/llvm-project/cfe/tools/clang-format.git#" +
Var("clang_format_rev"),
"third_party/libc++/trunk":
Var("git_url") + "/chromium/llvm-project/libcxx.git" + "#" +
Var("libcxx_revision"),
"third_party/libc++abi/trunk":
Var("git_url") + "/chromium/llvm-project/libcxxabi.git" + "#" +
Var("libcxxabi_revision"),
}
AFAIK this is auto-generated.
sync_chromium.py was removed back in December, replaced by a more efficient and far smaller download package. I suggest you follow the instructions at https://webrtc.org/native-code/development and get a newer checkout. It should be less likely to have download problems.
The issue here is the version of Python used. As you can see in command error prompt, the version that I have used is not 2.7+ as recomended in chromium website. Doing the change from Python to Python2.7 solve the issue.

ansible unexpected exception no escaped character

I am fairly new to Ansible and to configuration management tools in general. I've been playing around with it for the last two days and for the life of me I can't get past typing out ansible testserver. It comes back with an error message that says Unexpected Exception: No escaped character. The full error message is:
mac-dgarcia:playbooks dgarcia$ ansible testserver -i hosts -m ping -vvv
Using /Users/dgarcia/Documents/Playbooks/ansible.cfg as config file
Unexpected Exception: No escaped character
the full traceback was:
Traceback (most recent call last):
File "/Users/dgarcia/Documents/Playbooks/ansible/bin/ansible", line 79, in <module>
sys.exit(cli.run())
File "/Users/dgarcia/Documents/Playbooks/ansible/lib/ansible/cli/adhoc.py", line 106, in run
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.options.inventory)
File "/Users/dgarcia/Documents/Playbooks/ansible/lib/ansible/inventory/__init__.py", line 135, in __init__
self.parser = InventoryParser(filename=host_list)
File "/Users/dgarcia/Documents/Playbooks/ansible/lib/ansible/inventory/ini.py", line 45, in __init__
self._parse()
File "/Users/dgarcia/Documents/Playbooks/ansible/lib/ansible/inventory/ini.py", line 49, in _parse
self._parse_base_groups()
File "/Users/dgarcia/Documents/Playbooks/ansible/lib/ansible/inventory/ini.py", line 107, in _parse_base_groups
tokens = shlex.split(line)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shlex.py", line 279, in split
return list(lex)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shlex.py", line 269, in next
token = self.get_token()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shlex.py", line 96, in get_token
raw = self.read_token()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shlex.py", line 191, in read_token
raise ValueError, "No escaped character"
ValueError: No escaped character
Have searched everywhere I can on Google and came back with nothing. Any ideas?
I had the same issue and changing my host file to have only one line fixed the problem.
My host file looks like the following:
testserver ansible_ssh_host=128.0.0.1 ansible_ssh_port=2222 \
ansible_ssh_user=vagrant \
ansible_ssh_private_key_file=/home/bibryam/Desktop/.vagrant/machines/fabric/virtualbox/private_key

Categories

Resources