Is there a way you can ignore an error "wait_for" throws when a conditional statement hasn't been satisfied?
In my play, I have a task to see which LTM in the pair is active, and it fails when it hits the standby (which makes sense).
But it would be nice if you could ignore this error.
Traceback (most recent call last):
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 691, in <module>
main()
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 680, in main
results = mm.exec_module()
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 617, in exec_module
result = manager.exec_module()
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 409, in exec_module
changed = self.execute()
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 498, in execute
raise FailedConditionsError(errmsg, failed_conditions)
ansible.module_utils.network.common.parsing.FailedConditionsError: One or more conditional statements have not been satisfied.
fatal: [x.x.x.x -> localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 691, in <module>\n main()\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 680, in main\n results = mm.exec_module()\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 617, in exec_module\n result = manager.exec_module()\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 409, in exec_module\n changed = self.execute()\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 498, in execute\n raise FailedConditionsError(errmsg, failed_conditions)\nansible.module_utils.network.common.parsing.FailedConditionsError: One or more conditional statements have not been satisfied.\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
And here is the task I'm working with: (Note: the host inventory file contains 2 IPs only)
- name : Checking which LTM is active....
bigip_command:
server: "{{ inventory_hostname }}"
user: "{{ remote_username }}"
password: "{{ remote_passwd }}"
commands:
- "tmsh show sys failover"
- "tmsh list /sys management-ip | grep -o x.x.x.x"
wait_for:
- result[0] contains active
validate_certs: no
delegate_to: localhost
Please check the module documentation here -- There is a "match" directive that defaults to "all" which implies that in the "wait_for" section all checks should pan out as true. You can explicitly set this to "any" which would make your run pass if at least 1 condition in "wait_for" is met.
You also have a "retries" and "interval" flag which will tell the module how many retries to make and how far apart they should be, respectively.
I hope this helps.
Cheers!
Related
I'm trying to write an ansible role to install helm-charts.
The role structure, simplified for posting, is something like this:
my_role
└───defaults
└───main.yml
└───tasks
└───main.yml
└───templates
└───main.yml
The role's default value file includes a variable chart_values used to pass the chart's values.
It is used only in the template/main.yml file, inside it there is simply the variable expansion, like this:
"{{ chart_values }}"
In tasks/main.yml the template is used as follows:
- name: Deploy chart.
kubernetes.core.helm:
release_name: "{{ chart_release_name }}"
release_namespace: "{{ chart_release_namespace }}"
release_state: present
chart_ref: "{{ chart_chart_ref }}"
chart_version: "{{ chart_chart_version }}"
update_repo_cache: true
release_values: "{{ lookup('template', 'chart_values.yml') | from_yaml }}"
Values seem to be rendered correctly, except when there is a multiline object containing quotes.
For instance, while deploying gitlab runners, a toml configuration snippet is required.
When I try to run the playbook with
chart_values:
replicas: 5
runners:
config: |
[[runners]]
[runners.kubernetes]
namespace = "gitlab-runners"
image = "ubuntu:20.04"
Ansible returns the following error:
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: in "<unicode string>", line 2, column 770
fatal: [127.0.0.1]: FAILED! => {"changed": false}
The more verbose output is:
The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/ansible/executor/task_executor.py", line 504, in _execute
self._task.post_validate(templar=templar)
File "/usr/lib/python3/dist-packages/ansible/playbook/task.py", line 285, in post_validate
super(Task, self).post_validate(templar)
File "/usr/lib/python3/dist-packages/ansible/playbook/base.py", line 650, in post_validate
value = templar.template(getattr(self, name))
File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 913, in template
d[k] = self.template(
File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 869, in template
result = self.do_template(
File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 1139, in do_template
res = j2_concat(rf)
File "<template>", line 12, in root
File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 298, in wrapper
ret = func(*args, **kwargs)
File "/usr/lib/python3/dist-packages/ansible/plugins/filter/core.py", line 207, in from_yaml
return yaml_load(text_type(to_text(data, errors='surrogate_or_strict')))
File "/usr/local/lib/python3.8/dist-packages/yaml/__init__.py", line 81, in load
return loader.get_single_data()
File "/usr/local/lib/python3.8/dist-packages/yaml/constructor.py", line 49, in get_single_data
node = self.get_single_node()
File "yaml/_yaml.pyx", line 674, in yaml._yaml.CParser.get_single_node
File "yaml/_yaml.pyx", line 860, in yaml._yaml.CParser._parse_next_event
yaml.parser.ParserError: did not find expected <document start>
in "<unicode string>", line 2, column 770
fatal: [127.0.0.1]: FAILED! => {
"changed": false
}
Pls. Help
I`m have this playbook:
---
- name: Install service
hosts: some_host
vars:
app: "{{ app_name }}-{{ tag }}.war"
app_folder: "{{ client_dir }}/{{ app_name }}"
tasks:
- name: Unpack archive Java
unarchive:
src={{ java_distrib }}
dest={{ app_folder }}
creates="{{ app_folder }}/{{ java_home }}"
mode=0555
all work fine when i install on remote host.
But when hosts is same machine from i run ansible-playbook i got this error:
TASK [Unpack archive Java] *********************************************************************************************************
fatal: [10.2.9.6]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 10.2.9.6 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/home/stend/.ansible/tmp/ansible-tmp-1586175452.58-42679814338845/AnsiballZ_unarchive.py\", line 114, in <module>\r\n _ansiballz_main()\r\n File \"/home/stend/.ansible/tmp/ansible-tmp-1586175452.58-42679814338845/AnsiballZ_unarchive.py\", line 106, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/stend/.ansible/tmp/ansible-tmp-1586175452.58-42679814338845/AnsiballZ_unarchive.py\", line 49, in invoke_module\r\n imp.load_module('__main__', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_unarchive_payload_f_poEj/__main__.py\", line 157, in <module>\r\n File \"/tmp/ansible_unarchive_payload_f_poEj/ansible_unarchive_payload.zip/ansible/module_utils/urls.py\", line 402, in <module>\r\nAttributeError: 'module' object has no attribute 'HTTPSConnection'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
and im not understand whats going on. Why connection closed? im try unarchive to local machine. Where i`m wrong? Pls help !
Problem solved, i`m re install python and library libssl1.1
Here is the playbook for your reference:
---
- name: Closing notes
hosts: XXXX
gather_facts: yes
tasks:
- name: Modify a change request
snow_record:
state: present
table: change_request
username: admin
password: XXXXX
instance: XXXXX
number: XXXXX
data:
state: -3
register: incident
- debug:
var: incident.record.state
Output:
fatal: [XXXXXXX]: FAILED! => {
"changed": false,
"module_stderr": "Shared connection to XXXXXXX closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1577091421.74-160409156883610/AnsiballZ_snow_record.py\", line 114, in \r\n _ansiballz_main()\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1577091421.74-160409156883610/AnsiballZ_snow_record.py\", line 106, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1577091421.74-160409156883610/AnsiballZ_snow_record.py\", line 49, in invoke_module\r\n imp.load_module('main', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_snow_record_payload_qHs01F/main.py\", line 334, in \r\n File \"/tmp/ansible_snow_record_payload_qHs01F/main.py\", line 330, in main\r\n File \"/tmp/ansible_snow_record_payload_qHs01F/main.py\", line 319, in run_module\r\nAttributeError: 'module' object has no attribute 'UnexpectedResponse'\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
需要指定python的环境为 python3,就可以解决
You need to make the Python environment as Python 3, you can solve this problem
eg
[all:vars]
ansible_python_interpreter=/usr/bin/python3
I have to use Ansible modules in order to edit the ssl.conf file - every time I import a new certificate.
At this moment I'm using the shell module to execute a sed command but would like to use lineinfile, if possible
sed -i '/^SSLRequire/ s/.$/|^.*.us-gov-vodafone-1.*\//' ssl.conf -i_backup_$(date "+%m%d%y")
The given sed will remove the / present at end of line and append '|^..us-gov-vodafone-1./'
I tried different ways to achieve it but could not.
Any suggestions would be sincerely appreciated.
I tried below code but it does not give me proper result
---
- hosts: ub2
# user: ansible
vars:
test:
- '|^.*.us-gov-vodafone-1.*/'
tasks:
- name: append reguler expression
lineinfile:
dest: /tmp/ssl.conf
backrefs: yes
state: present
regexp: '^(SSLRequire(?!.*\b{{ test }}\b).*)$'
line: '\1 {{ test }}'
PLAY [ub2] ***************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.56.102]
TASK [append reguler expression] *****************************************************************************************************************************
fatal: [192.168.56.102]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.56.102 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1546198717.33-9064765281500/AnsiballZ_lineinfile.py\", line 113, in <module>\r\n _ansiballz_main()\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1546198717.33-9064765281500/AnsiballZ_lineinfile.py\", line 105, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1546198717.33-9064765281500/AnsiballZ_lineinfile.py\", line 48, in invoke_module\r\n imp.load_module('__main__', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_lineinfile_payload_LyoFsx/__main__.py\", line 524, in <module>\r\n File \"/tmp/ansible_lineinfile_payload_LyoFsx/__main__.py\", line 515, in main\r\n File \"/tmp/ansible_lineinfile_payload_LyoFsx/__main__.py\", line 264, in present\r\n File \"/usr/lib/python2.7/re.py\", line 194, in compile\r\n return _compile(pattern, flags)\r\n File \"/usr/lib/python2.7/re.py\", line 251, in _compile\r\n raise error, v # invalid expression\r\nsre_constants.error: bad character range\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
to retry, use: --limit #/home/ansible/find_and_append_2.retry
PLAY RECAP ***************************************************************************************************************************************************
192.168.56.102 : ok=1 changed=0 unreachable=0 failed=1
Trying to automate file uploading process to the Cisco Nexus 9000 device with Ansible Playbook.
- name: Upload File to Nexus Device
gather_facts: no
hosts: localhost
connection: local
tasks:
- name: Uploading file
nxos_file_copy:
provider:
username: admin
ssh_keyfile: "files/id_rsa"
host: 192.168.0.100
validate_certs: no
transport: cli
local_file: "config.txt"
remote_file: "config.txt"
When running the playbook I am getting following error
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py", line 255, in <module>
main()
File "/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py", line 245, in main
transfer_file(module, dest)
File "/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py", line 174, in transfer_file
port=port)
File "/home/user/venv/ansible-cox/local/lib/python2.7/site-packages/paramiko/client.py", line 424, in connect
passphrase,
File "/home/user/venv/ansible-cox/local/lib/python2.7/site-packages/paramiko/client.py", line 714, in _auth
raise saved_exception
paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted
fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py\", line 255, in <module>\n main()\n File \"/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py\", line 245, in main\n transfer_file(module, dest)\n File \"/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py\", line 174, in transfer_file\n port=port)\n File \"/home/user/venv/ansible-cox/local/lib/python2.7/site-packages/paramiko/client.py\", line 424, in connect\n passphrase,\n File \"/home/user/venv/ansible-cox/local/lib/python2.7/site-packages/paramiko/client.py\", line 714, in _auth\n raise saved_exception\nparamiko.ssh_exception.PasswordRequiredException: Private key file is encrypted\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
When SSH-ing from local terminal with the same key it works
(ansible) ~/B/c/Ansible on master ⨯ ssh admin#192.168.0.100 -i files/id_rsa
User Access Verification
NX9K#
I am using Ansible 2.5.4 version.
Any help is greatly appreciated!