Ansible: append a regular expression on an existing line in a file - shell

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

Related

Ansible postgresql_query Yield AttributeError: 'NoneType' object has no attribute '__version__' Error

I have a short Ansible playbook, mybook.yml:
- hosts: "{{ host }}"
become: yes
become_user: root
vars:
stl_postgres_port: 6543
tasks:
- debug: msg="{{ stl_database }}"
- debug: msg="{{ stl_postgres_port }}"
- debug: msg="{{ postgres_password }}"
- debug: msg="{{ get_db_version }}"
- name: Check the database versions
postgresql_query:
db: "{{ stl_database }}"
port: "{{ stl_postgres_port }}"
login_host: localhost
login_password: "{{ postgres_password }}"
query: "{{ get_db_version }}"
register: db_version_return
- debug: msg="{{ db_version_return }}"
I run it like this:
$ ansible-playbook mybook.yml -e "host=my-host"
I have the host var files defined at /etc/ansible/host_vars/my-host
stl_hostname: my-host
stl_gcp_project: stl-webapp
stl_gcp_zone: us-central1-b
postgres_password: 1234abcd
stl_database: appdbmyhost
get_db_version: select * from webapp.code_version order by webapp_version desc limit 1;
I was hoping to get the last entry from webapp_version table. But I am getting the output with the error from the postgresql_query module:
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************
Saturday 12 March 2022 05:19:44 +0000 (0:00:00.025) 0:00:00.025 ********
ok: [my-host]
TASK [debug] ****************************************************************************************************************************************************************************************************************************
Saturday 12 March 2022 05:19:46 +0000 (0:00:01.919) 0:00:01.945 ********
ok: [my-host] => {
"msg": "appdbmyhost"
}
TASK [debug] ****************************************************************************************************************************************************************************************************************************
Saturday 12 March 2022 05:19:46 +0000 (0:00:00.026) 0:00:01.971 ********
ok: [my-host] => {
"msg": 6543
}
TASK [debug] ****************************************************************************************************************************************************************************************************************************
Saturday 12 March 2022 05:19:46 +0000 (0:00:00.023) 0:00:01.995 ********
ok: [my-host] => {
"msg": "1234abcd"
}
TASK [debug] ****************************************************************************************************************************************************************************************************************************
Saturday 12 March 2022 05:19:46 +0000 (0:00:00.025) 0:00:02.021 ********
ok: [my-host] => {
"msg": "select * from webapp.code_version order by webapp_version desc limit 1;"
}
TASK [Check the database versions] ******************************************************************************************************************************************************************************************************
Saturday 12 March 2022 05:19:47 +0000 (0:00:00.026) 0:00:02.047 ********
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute '__version__'
fatal: [my-host]: FAILED! => {"changed": false, "module_stderr": "Shared connection to wag-tiger closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/home/philip/.ansible/tmp/ansible-tmp-1647062387.0483847-6513-40239082180393/AnsiballZ_postgresql_query.py\", line 100, in <module>\r\n _ansiballz_main()\r\n File \"/home/philip/.ansible/tmp/ansible-tmp-1647062387.0483847-6513-40239082180393/AnsiballZ_postgresql_query.py\", line 92, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/philip/.ansible/tmp/ansible-tmp-1647062387.0483847-6513-40239082180393/AnsiballZ_postgresql_query.py\", line 41, in invoke_module\r\n run_name='__main__', alter_sys=True)\r\n File \"/usr/lib/python3.6/runpy.py\", line 205, in run_module\r\n return _run_module_code(code, init_globals, run_name, mod_spec)\r\n File \"/usr/lib/python3.6/runpy.py\", line 96, in _run_module_code\r\n mod_name, mod_spec, pkg_name, script_name)\r\n File \"/usr/lib/python3.6/runpy.py\", line 85, in _run_code\r\n exec(code, run_globals)\r\n File \"/tmp/ansible_postgresql_query_payload_rq74h0ai/ansible_postgresql_query_payload.zip/ansible_collections/community/postgresql/plugins/modules/postgresql_query.py\", line 511, in <module>\r\n File \"/tmp/ansible_postgresql_query_payload_rq74h0ai/ansible_postgresql_query_payload.zip/ansible_collections/community/postgresql/plugins/modules/postgresql_query.py\", line 405, in main\r\n File \"/tmp/ansible_postgresql_query_payload_rq74h0ai/ansible_postgresql_query_payload.zip/ansible_collections/community/postgresql/plugins/module_utils/postgres.py\", line 192, in get_conn_params\r\nAttributeError: 'NoneType' object has no attribute '__version__'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
PLAY RECAP ******************************************************************************************************************************************************************************************************************************
my-host : ok=5 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Full error:
Shared connection to wag-tiger closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File "/home/philip/.ansible/tmp/ansible-tmp-1647062387.0483847-6513-40239082180393/AnsiballZ_postgresql_query.py", line 100, in \r\n _ansiballz_main()\r\n File "/home/philip/.ansible/tmp/ansible-tmp-1647062387.0483847-6513-40239082180393/AnsiballZ_postgresql_query.py", line 92, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File "/home/philip/.ansible/tmp/ansible-tmp-1647062387.0483847-6513-40239082180393/AnsiballZ_postgresql_query.py", line 41, in invoke_module\r\n run_name='main', alter_sys=True)\r\n File "/usr/lib/python3.6/runpy.py", line 205, in run_module\r\n return _run_module_code(code, init_globals, run_name, mod_spec)\r\n File "/usr/lib/python3.6/runpy.py", line 96, in _run_module_code\r\n mod_name, mod_spec, pkg_name, script_name)\r\n File "/usr/lib/python3.6/runpy.py", line 85, in _run_code\r\n exec(code, run_globals)\r\n File "/tmp/ansible_postgresql_query_payload_rq74h0ai/ansible_postgresql_query_payload.zip/ansible_collections/community/postgresql/plugins/modules/postgresql_query.py", line 511, in \r\n File "/tmp/ansible_postgresql_query_payload_rq74h0ai/ansible_postgresql_query_payload.zip/ansible_collections/community/postgresql/plugins/modules/postgresql_query.py", line 405, in main\r\n File "/tmp/ansible_postgresql_query_payload_rq74h0ai/ansible_postgresql_query_payload.zip/ansible_collections/community/postgresql/plugins/module_utils/postgres.py", line 192, in get_conn_params\r\nAttributeError: 'NoneType' object has no attribute 'version'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error
What does AttributeError: 'NoneType' object has no attribute "__version__" mean? I don't think I missed anything.
What's interesting is that if I switch the host name, some of them work and I can get the query output. All the host_var files are pretty much the same.
Any idea?
Thanks!

ansible error when use unarchive module on local system

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

How to fix the error 'module' object has no attribute 'UnexpectedResponse' in Ansible playbook

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

Create Azure Keyvault secrets via Ansible

Ansible version 2.7.6
Ansible playbook looks like this:
- hosts: localhost
name: Create instance of Key Vault
tasks:
- name: Create a secret
azure_rm_keyvaultsecret:
secret_name: test25
secret_value: 218y490randomstuff9515215123
keyvault_uri: https://vrandomg3252.vault.azure.net/
tags:
testing: test
Output generate error "No handlers could be found for logger"
PLAY [Create instance of Key Vault] *************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [localhost]
TASK [Create a secret] **************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "No handlers could be found for logger \"keyring.backend\"\nTraceback (most recent call last):\n File \"/home/ansibleadm/.ansible/tmp/ansible-tmp-1549893073.25-278009488310625/AnsiballZ_azure_rm_keyvaultsecret.py\", line 113, in <module>\n _ansiballz_main()\n File \"/homedirectoryname/.ansible/tmp/ansible-tmp-1549893073.25-278009488310625/AnsiballZ_azure_rm_keyvaultsecret.py\", line 113, in <module>\n _ansiballz_main()\n File \"/homedirectoryname/.ansible/tmp/ansible-tmp-1549893073.25-278009488310625/AnsiballZ_azure_rm_keyvaultsecret.py\", line 105, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/home/ansibleadm/.ansible/tmp/ansible-tmp-1549893073.25-278009488310625/AnsiballZ_azure_rm_keyvaultsecret.py\", line 48, in invoke_module\n imp.load_module('__main__', mod, module, MOD_DESC)\n File \"/tmp/ansible_azure_rm_keyvaultsecret_payload_VsqDCW/__main__.py\", line 215, in <module>\n File \"/tmp/ansible_azure_rm_keyvaultsecret_payload_VsqDCW/__main__.py\", line 211, in main\n File \"/tmp/ansible_azure_rm_keyvaultsecret_payload_VsqDCW/__main__.py\", line 126, in __init__\n File \"/tmp/ansible_azure_rm_keyvaultsecret_payload_VsqDCW/ansible_azure_rm_keyvaultsecret_payload.zip/ansible/module_utils/azure_rm_common.py\", line 308, in __init__\n File \"/tmp/ansible_azure_rm_keyvaultsecret_payload_VsqDCW/__main__.py\", line 174, in exec_module\n File \"/tmp/ansible_azure_rm_keyvaultsecret_payload_VsqDCW/__main__.py\", line 199, in create_secret\n File \"/usr/local/lib/python2.7/dist-packages/azure/keyvault/key_vault_client.py\", line 1586, in set_secret\n raise models.KeyVaultErrorException(self._deserialize, response)\nazure.keyvault.models.key_vault_error.KeyVaultErrorException: (Forbidden) Access denied\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1
How to use azure_rm_keyvaultsecret module in right way?
You are missing auth parameters. See https://docs.ansible.com/ansible/latest/modules/azure_rm_keyvaultsecret_module.html
- name: create a secret for mysql
azure_rm_keyvaultsecret:
secret_name: test25
secret_value: 218y490randomstuff9515215123
keyvault_uri: https://vrandomg3252.vault.azure.net/
client_id: "{{ AZURE_CLIENT_ID }}"
secret: "{{ AZURE_CLIENT_SECRET }}"
tenant: "{{ AZURE_TENANT_ID }}"
subscription_id: "{{ AZURE_SUBSCRIPTION_ID }}"

Ansible bigip_command module

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!

Resources