Running db2relocatedb with ansible - linux

I have this ansible playbook:
---
- hosts: all
gather_facts: False
become: yes
become_user: myins
become_method: sudo
tasks:
- name: test someting
command: "cd /db2/myins"
- name: relocate it
command: "db2relocatedb -f /db2/myins/relocate.cfg"
...
if I run the command by itself it works and if I use cat /db2/myins/relocate.cfg instead of db2relocatedb -f it works too.
If I run it like this I get:
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
File "/tmp/ansible_command_payload_HGHkvR/ansible_command_payload.zip/ansible/module_utils/basic.py", line 2561, in run_command
cmd = subprocess.Popen(args, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
fatal: [mounttest]: FAILED! => {
"changed": false,
"cmd": "db2relocatedb -f /db2/myins/relocate.cfg",
"invocation": {
"module_args": {
"_raw_params": "db2relocatedb -f /db2/myins/relocate.cfg",
"_uses_shell": false,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"msg": "[Errno 2] No such file or directory",
"rc": 2
}
Why can I cat the file but not use it?
Thanks,
Arengin

the problem is inside relocate.cfg, check all Pathes make sure they are full qualified, make sure no escape character is somewhere

Related

du: cannot access ‘/home/*’: No such file or directory

I'm trying to create an ansible playbook to check disk utilization of each users in the /home/ directory
---
- name: User Home Directory Stat
hosts: linux7
become: true
vars:
directory: /home/*
tasks:
- name: Check available user directory and disk utilization
ansible.builtin.command: "sudo du -sh {{ directory }} "
register: msg
- debug:
var: msg.stdout_lines
However, i get
FAILED! => {"changed": true, "cmd": ["sudo", "du", "-sh", "/home/*"], "delta": "0:00:00.018716", "end": "2022-09-29 10:44:06.375725", "msg": "non-zero return code", "rc": 1, "start": "2022-09-29 10:44:06.357009", "stderr": "du: cannot access ‘/home/*’: No such file or directory", "stderr_lines": ["du: cannot access ‘/home/*’: No such file or directory"], "stdout": "", "stdout_lines": []}

Ansible: Jinja2 doesn't read dictionary object correctly

EDIT: I HAD THE WRONG FILE OPEN IN ATOM, CONTINUE TO MY ANSWER TO KNOW HOW.
I have a playbook that is supposed to spin up websites for three fictional company names, however in one of my Jinja2 templates, it doesn't find a variable that is clearly located in a dictionary. Any thoughts are greatly appreciated!
Ansible Version:
ansible 2.10.13
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Jan 27 2021, 01:17:18) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
playbook.yml
---
- hosts: company_servers
vars:
company_site:
totsuki:
companyname: UA High School
apache_dir: totsuki_academy
filename: totsuki_academy
companyid: 1
uahs:
companyname: UA High School
apache_dir: ua_high_school
filename: ua_highschool
companyid: 2
we:
companyname: Wayne Enterprises
apache_dir: wayne_enterprises
filename: ua_highschool
companyid: 3
tasks:
- name: Install Apache
dnf:
name: httpd
state: latest
- name: Create Website Folders
file:
path: /var/www/html/{{ item.value.apache_dir }}
state: directory
mode: 0775
with_dict: "{{company_site}}"
- name: Create HTML files
template:
src: htmltemplate2.html.j2
dest: /var/www/html/{{ item.value.apache_dir }}/index.html
owner: apache
group: apache
mode: 0775
with_dict: "{{company_site}}"
- name: Apache Preference Import
template:
src: httpd.conf
dest: /etc/httpd/conf/httpd.conf
mode: 0775
- name: Create Configuration Folders
file:
path: /etc/httpd/{{ item }}
state: directory
mode: 0775
loop:
- sites-enabled
- sites-available
- name: Apache Preference Import
template:
src: httpd.conf
dest: /etc/httpd/sites-available
mode: 0775
- name: Create a symbolic link
file:
src: /etc/httpd/sites-available
dest: /etc/httpd/sites-enabled
state: link
- name: Restart Apache
service:
name: httpd
state: restarted
- name: Stop Firewall
service:
name: firewalld
state: stopped
Infuriating Error:
The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1066, in do_template
res = j2_concat(rf)
File "<template>", line 14, in root
File "/usr/local/lib/python3.6/site-packages/jinja2/runtime.py", line 903, in _fail_with_undefined_error
raise self._undefined_exception(self._undefined_message)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'companyname'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/action/template.py", line 139, in run
resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1103, in do_template
raise AnsibleUndefinedVariable(e)
ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'
failed: [192.168.100.6] (item={'key': 'we', 'value': {'companyname': 'Wayne Enterprises', 'apache_dir': 'wayne_enterprises', 'filename': 'ua_highschool', 'companyid': 3}}) => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"key": "we",
"value": {
"apache_dir": "wayne_enterprises",
"companyid": 3,
"companyname": "Wayne Enterprises",
"filename": "ua_highschool"
}
},
"msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'"
}
The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1066, in do_template
res = j2_concat(rf)
File "<template>", line 14, in root
File "/usr/local/lib/python3.6/site-packages/jinja2/runtime.py", line 903, in _fail_with_undefined_error
raise self._undefined_exception(self._undefined_message)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'companyname'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/action/template.py", line 139, in run
resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1103, in do_template
raise AnsibleUndefinedVariable(e)
ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'
failed: [192.168.100.7] (item={'key': 'we', 'value': {'companyname': 'Wayne Enterprises', 'apache_dir': 'wayne_enterprises', 'filename': 'ua_highschool', 'companyid': 3}}) => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"key": "we",
"value": {
"apache_dir": "wayne_enterprises",
"companyid": 3,
"companyname": "Wayne Enterprises",
"filename": "ua_highschool"
}
},
"msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'"
}
Also, I have noticed that Wayne enterprises 'filename' wasn't correct and I did adjust that.
It says object has no attribute 'companyname'
and here is my html.j2
<html>
<head>
<title>{{ item.value.companyname }}</title>
</head>
<body>
<h1>Welcome to the {{ item.value.companyname }} website</h1>
IP Address: {{ ansible_default_ipv4.address }}
</body>
</html>
This was solved by taking a look at the path of the file I had open in Atom, and comparing it to the path of the file that Ansible was running. It's important to know, that when you're using the template module in Ansible, that files you're referencing without an exact path should be stored in ~/templates and Ansible will make that folder for you upon running a playbook with the template module.
Common sense in my mind thought it might reference the directory of the playbook, but is not how Ansible works.

shell command with Ansible playbook doesn't work

I have added to my playbook a small task that should change umask on my linux machine:
- name: set umask to 0022
shell: umask 0022
When running the playbook, I can see this task passes successfully:
changed: [myHostName] => {
"changed": true,
"cmd": "umask 0022",
"delta": "0:00:00.004660",
"end": "2020-08-04 16:28:44.153261",
"invocation": {
"module_args": {
"_raw_params": "umask 0022",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"rc": 0,
"start": "2020-08-04 16:28:44.148601",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
but After the playbook finishes, I check the umask and see that it was not changed at all:
-bash-4.2$ umask
0044
I also put a debug in my playbook right after the task I showed above, and the debug also shows that the umask was not changed..
Tried also with
become: yes
But got the same result..
When I do the command on my Linux manually, it will work:
-bash-4.2$ umask 0022
-bash-4.2$ umask
0022
Q: After the playbook finishes, I check the umask and see that it was not changed at all.
A: This is correct. Ansible isn't really doing things through the shell i.e. the changes live in this one session only.

How to check for a process if running then exit else remove and install a package

I need to check for a process on few remote hosts, if the process is running then exit without doing anything, if the process is not running then remove the existing package and install a different version.
I have tried the below ansible code, but I am not sure in ansible how to exit if the process is already running. Can someone please help here? Thanks in advance.
- hosts: all
become: yes
tasks:
- name: check if http is running
shell: "pgrep http"
register: running_processes
failed_when: running_processes.rc > 1
- debug:
var: running_processes
- block:
- debug:
msg: http is running. End of play.
- meta: end_host
when: running_processes.stdout_lines|length > 0
- debug:
msg: http is not running. Continue play.
- name: remove the old http package
yum:
name: http
state: absent
- name: install http
yum:
name: http.rpm
state: present
But I am getting below error while running this playbook.
fatal: [host1]: FAILED! => {"msg": "The conditional check 'running_processes.stdout_lines|length > 0' failed. The error was: error while evaluating conditional (running_processes.stdout_lines|length > 0): 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in 'http.yml': line 14, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - block:\n - debug:\n ^ here\n"}
Output of running_processes
ok: [host1] => {
"running_processes": {
"changed": true,
"cmd": "pgrep http",
"delta": "0:00:00.025309",
"end": "2019-08-26 14:44:21.019275",
"failed": false,
"failed_when_result": false,
"msg": "non-zero return code",
"rc": 1,
"start": "2019-08-26 14:44:20.993966",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
I am not getting this error now, but - meta: end_host is throwing an error.
ERROR! invalid meta action requested: end_host
The error appears to have been in 'http.yml': line 20, column 10, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
msg: http is running. End of play.
- meta: end_host
^ here
It is working as expected where the process is running. But it is skipping where the process is not running.
ok: [host1] => {
"running_processes": {
"changed": true,
"cmd": "pgrep http",
"delta": "0:00:00.019929",
"end": "2019-08-26 16:57:59.940856",
"failed": false,
"failed_when_result": false,
"rc": 0,
"start": "2019-08-26 16:57:59.920927",
"stderr": "",
"stderr_lines": [],
"stdout": "8743",
"stdout_lines": [
"8743"
]
}
}
ok: [host2] => {
"running_processes": {
"changed": true,
"cmd": "pgrep http",
"delta": "0:00:00.018915",
"end": "2019-08-26 16:57:59.338179",
"failed": false,
"failed_when_result": false,
"msg": "non-zero return code",
"rc": 1,
"start": "2019-08-26 16:57:59.319264",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
TASK [debug] ***********************************************************************************************************************************************************
ok: [host1] => {
"msg": "http is running. End of play."
}
skipping: [host2]
ERROR! invalid meta action requested: end_host
The error appears to have been in 'http.yml': line 19, column 10, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
msg: http is running. End of play.
- meta: end_host
^ here
(ansible 2.8.3)
Use pgrep to avoid this, e.g.
- name: check if httpd is running
shell: "pgrep httpd"
register: running_processes
failed_when: running_processes.rc > 1
- block:
- debug:
msg: httpd is running. End of play.
- meta: end_play
when: running_processes.stdout_lines|length > 0
- debug:
msg: httpd is not running. Continue play.
Notes
Equivalent condition is
when: running_processes.rc == 0
- meta: end_play terminates all hosts. Use - meta: end_host to end current host only.
Below is the example of running_processes when there is no match
ok: [localhost] => {
"running_processes": {
"changed": true,
"cmd": "pgrep httpd",
"delta": "0:00:00.013312",
"end": "2019-08-26 20:40:59.908060",
"failed": false,
"failed_when_result": false,
"msg": "non-zero return code",
"rc": 1,
"start": "2019-08-26 20:40:59.894748",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}

Ansible file module error - chown failed: failed to look up user

I am trying to change the owner of a file using file module. I tried this piece of code:
---
- hosts: super_group
remote_user: ec2-user
tasks:
- name: Checking the user name
shell: /usr/bin/whoami
register: username
- name: Debugging the whoami username
debug: msg={{ username }}
- name: Changing the owner of a file
file: path=/home/ec2-user/test owner={{ username }}
Error:
TASK [Changing the owner of a file] ********************************************
fatal: [test]: FAILED! => {"changed": false, "failed": true, "gid": 0, "group": "root", "mode": "0644", "msg": "chown failed: failed to look up user {'stderr_lines': [], 'changed': True, 'end': '2017-07-10 01:49:11.495709', 'stdout': 'ec2-user', 'cmd': '/usr/bin/whoami', 'start': '2017-07-10 01:49:11.492286', 'delta': '0:00:00.003423', 'stderr': '', 'rc': 0, 'stdout_lines': ['ec2-user']}", "owner": "ec2-user", "path": "/home/ec2-user/test", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 0, "state": "file", "uid": 1000}
to retry, use: --limit #/home/ec2-user/ansible/test.retry
debug module is giving me this output:
TASK [Debugging the whoami username] *******************************************
ok: [test] => {
"msg": {
"changed": true,
"cmd": "/usr/bin/whoami",
"delta": "0:00:00.003423",
"end": "2017-07-10 01:49:11.495709",
"rc": 0,
"start": "2017-07-10 01:49:11.492286",
"stderr": "",
"stderr_lines": [],
"stdout": "ec2-user",
"stdout_lines": [
"ec2-user"
]
}
}
Note:
If I hardcode the value of username then it works fine:
- name: Changing the owner of a file
file: path=/home/ec2-user/test owner=ec2-user
Please let me know how to resolve this issue.
There is no issue. You want to use username.stdout, not username.
Please check the value you printed with the debug module and use reasoning.

Resources