I'm using https://github.com/kubernetes-sigs/kubespray
Is there a way to know exactly which command the following Ansible code will execute on the remote server?
- name: ensure docker packages are installed
package:
name: "{{ docker_package_info.pkgs }}"
state: "{{ docker_package_info.state | default('present') }}"
module_defaults:
apt:
update_cache: true
dnf:
enablerepo: "{{ docker_package_info.enablerepo | default(omit) }}"
yum:
enablerepo: "{{ docker_package_info.enablerepo | default(omit) }}"
zypper:
update_cache: true
register: docker_task_result
until: docker_task_result is succeeded
retries: 4
delay: "{{ retry_stagger | d(3) }}"
notify: restart docker
when:
- not ansible_os_family in ["Flatcar Container Linux by Kinvolk"]
- not is_ostree
- docker_package_info.pkgs|length > 0
I'm getting the following error in RHEL8. I want to run manually docker-ce on the relevant VM and check the error.
{"attempts": 4, "changed": true, "cmd": "set -o pipefail && /usr/bin/docker ps -aq | xargs -r docker rm -fv", "delta": "0:00:00.008934", "end": "2021-07-16 18:46:54.009010", "msg": "non-zero return code", "rc": 127, "start": "2021-07-16 18:46:54.000076", "stderr": "/bin/bash: /usr/bin/docker: No such file or directory", "stderr_lines": ["/bin/bash: /usr/bin/docker: No such file or directory"], "stdout": "", "stdout_lines": []}
Thanks,
Related
I am getting below error (/bin/sh: nslookup: command not found ) when i am using vagrant for ansible code testing using molecule testig
TASK [Query nslookup own domain] ***********************************************
fatal: [ vagrant01 ]: FAILED! => {"changed": true, "cmd": "nslookup `hostname --fqdn`", "delta": "0:00:00.012528", "end": "2021-10-21 13:42:40.197574", "msg": "non-zero return code", "rc": 127, "start": "2021-10-21 13:42:40.185046", "stderr": "/bin/sh: nslookup: command not found", "stderr_lines": ["/bin/sh: nslookup: command not found"], "stdout": "", "stdout_lines": []}
PLAY RECAP
so it seems nslookup is not installed
You can add a task to install the necessary tools
- name: Install nslookup
apt:
name: dnsutils
state: present
If you are on red-hat distro you need following package
- name: Install nslookup
yum:
name: bind-utils
state: present
or if you want to distribute any bistro you can add both
- name: Install nslookup for ubuntu
apt:
name: dnsutils
state: present
when: ansible_os_family == 'Debian'
- name: Install nslookup for redhat family
yum:
name: bind-utils
state: present
when: ansible_os_family == 'RedHat'
I'm trying to pass the content of a text file I have in the local folder to an Ansible shell command and for whatever reason, I can't and I'm sure its something silly that I'm missing.
Any help would be really appreciated
Here is the ansible line:
- name: Connect K8s cluster to Shipa
shell: shipa cluster-add {{ cluster_name }} --addr $(< {{ cluster_name }}.txt) --cacert {{ cluster_name }}.crt --pool {{ cluster_name }} --ingress-service-type=LoadBalancer
args:
executable: /bin/bash
no_log: False
Whenever I run this, this is what I get in the log:
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "shipa cluster-add scale2 --addr $(< scale2.txt) --cacert scale2.crt --pool scale2 --ingress-service-type=LoadBalancer", "delta": "0:00:00.096162", "end": "2020-04-30 21:32:54.245781", "msg": "non-zero return code", "rc": 1, "start": "2020-04-30 21:32:54.149619", "stderr": "Error: 500 Internal Server Error: parse \"https://\\x1b[0;33mhttps://192.196.192.156\\x1b[0m\": net/url: invalid control character in URL", "stderr_lines": ["Error: 500 Internal Server Error: parse \"https://\\x1b[0;33mhttps://192.196.192.156\\x1b[0m\": net/url: invalid control character in URL"], "stdout": "", "stdout_lines": []}
Here is the content of the file:
# cat scale2.txt
https://192.196.192.156
Here is what Im trying to pass in the command:
shipa cluster-add scale2 --addr https://192.196.192.156 --cacert scale2.crt --pool scale2 --ingress-service-type=LoadBalancer
EDIT:
I also tried the following:
- name: Get K8s cluster address
shell: kubectl cluster-info | grep 'Kubernetes master' | awk '/http/ {print $NF}'
args:
executable: /bin/bash
register: cluster_address
no_log: False
- debug: var=cluster_address
- name: Save to file
copy:
content: "{{ ''.join(cluster_address.stdout_lines) | replace('\\u001b[0;33m', '')| replace('\\u001b[0m', '') }}"
dest: "cluster_address.json"
- name: Connect K8s cluster to Shipa
shell: shipa cluster-add {{ cluster_name }} --addr {{ lookup('file', 'cluster_address.json') }} --cacert {{ cluster_name }}.crt --pool {{ cluster_name }} --ingress-service-type=LoadBalancer
args:
executable: /bin/bash
no_log: False
But it saves the following content to the file:
# vi cluster_address.json
^[[0;33mhttps://192.192.56.192^[[0m
Here is the output of the ansible execution:
TASK [debug] **********************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"cluster_address": {
"changed": true,
"cmd": "kubectl cluster-info | grep 'Kubernetes master' | awk '/http/ {print $NF}'",
"delta": "0:00:00.144193",
"end": "2020-04-30 22:54:21.970460",
"failed": false,
"rc": 0,
"start": "2020-04-30 22:54:21.826267",
"stderr": "",
"stderr_lines": [],
"stdout": "\u001b[0;33mhttps://192.192.56.192\u001b[0m",
"stdout_lines": [
"\u001b[0;33mhttps://192.192.56.192\u001b[0m"
]
}
}
TASK [Save to file] ***************************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Connect K8s cluster to Shipa] ***********************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "shipa cluster-add scale1 --addr \u001b[0;33mhttps://192.192.56.192\u001b[0m --cacert scale1.crt --pool scale1 --ingress-service-type=LoadBalancer", "delta": "0:00:00.106936", "end": "2020-04-30 22:54:23.213485", "msg": "non-zero return code", "rc": 127, "start": "2020-04-30 22:54:23.106549", "stderr": "Error: 400 Bad Request: either default or a list of pools must be set\n/bin/bash: 33mhttps://192.192.56.192\u001b[0m: No such file or directory", "stderr_lines": ["Error: 400 Bad Request: either default or a list of pools must be set", "/bin/bash: 33mhttps://192.192.56.192\u001b[0m: No such file or directory"], "stdout": "", "stdout_lines": []}
You can disable the fancy terminal output by setting the TERM to dumb
- shell: kubectl cluster-info | awk '/Kubernetes master/ {print $NF}'
environment:
TERM: dumb
register: cluster_info
- set_fact:
cluster_addr: '{{ cluster_info.stdout | trim }}'
then, separately, you don't need to use the file lookup for a value that you have in a jinja2 variable already:
- name: Connect K8s cluster to Shipa
command: shipa cluster-add {{ cluster_name }} --addr {{ cluster_addr }} --cacert {{ cluster_name }}.crt --pool {{ cluster_name }} --ingress-service-type=LoadBalancer
args:
executable: /bin/bash
I have written the following ansible playbook to find the disk failure on the raid
- name: checking raid status
shell: "cat /proc/mdstat | grep nvme"
register: "array_check"
- debug:
msg: "{{ array_check.stdout_lines }}"
Following is the output I got
"msg": [
"md0 : active raid1 nvme0n1p1[0] nvme1n1p1[1]",
"md2 : active raid1 nvme1n1p3[1](F) nvme0n1p3[0]",
"md1 : active raid1 nvme1n1p2[1] nvme0n1p2[0]"
]
I want to extract the disk name which is failed from the register variable array_check.
How do I do this in the ansible? Can I use set_fact module in ansible? Can I use grep, awk, sed command on the register variable array_check
This is the playbook I am using to check the health status of a drive using smartctl
- name: checking the smartctl logs
shell: "smartctl -H /dev/{{ item }}"
with_items:
- nvme0
- nvme1
And I am facing the following error
(item=nvme0) => {"changed": true, "cmd": "smartctl -H /dev/nvme0", "delta": "0:00:00.090760", "end": "2019-09-05 11:21:17.035173", "failed": true, "item": "nvme0", "rc": 127, "start": "2019-09-05 11:21:16.944413", "stderr": "/bin/sh: 1: smartctl: not found", "stdout": "", "stdout_lines": [], "warnings": []}
(item=nvme1) => {"changed": true, "cmd": "smartctl -H /dev/nvme1", "delta": "0:00:00.086596", "end": "2019-09-05 11:21:17.654036", "failed": true, "item": "nvme1", "rc": 127, "start": "2019-09-05 11:21:17.567440", "stderr": "/bin/sh: 1: smartctl: not found", "stdout": "", "stdout_lines": [], "warnings": []}
The desired output should be something like this,
=== START OF SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED
Below is the complete playbook including the logic to execute multiple commands in a single task using with_items,
---
- hosts: raid_host
remote_user: ansible
become: yes
become_method: sudo
tasks:
- name: checking raid status
shell: "cat /proc/mdstat | grep 'F' | cut -d' ' -f6 | cut -d'[' -f1"
register: "array_check"
- debug:
msg: "{{ array_check.stdout_lines }}"
- name: checking the samrtctl logs for the drive
shell: "/usr/sbin/smartctl -H /dev/{{ item }} | tail -2|awk -F' ' '{print $6}'"
with_items:
- "nvme0"
- "nvme1"
register: "smartctl_status"
- name: Create directory for python files
file: path=/home/vuser/test/
state=directory
owner={{ user }}
group={{ user }}
mode=755
- name: Copy python file over
copy:
src=sample.py
dest=/home/vuser/test/sample.py
owner={{ user }}
group={{ user }}
mode=777
- name: Execute script
command: python sample.py
args:
chdir: /home/vuser/test/
ignore_errors: yes
error
fatal: [n]: FAILED! => {"changed": true, "cmd": ["python", "sample.py"], "delta": "0:00:00.003200", "end": "2019-07-18 13:57:40.213252", "msg": "non-zero return code", "rc": 1, "start": "2019-07-18 13:57:40.221132", "stderr": "", "stderr_lines": [], "stdout": "1", "stdout_lines": ["1"]}
not able to figure out, help would be appreciated
Change the indent like below and remove ignore_errors.
- name: Execute script
command: python sample.py
args:
chdir: /home/vuser/test/
register: cat_contents
- name: Print contents
debug:
msg: "{{ cat_contents.stdout }}"
- name: Create directory for python files
file: path=/home/vuser/test/
state=directory
owner={{ user }}
group={{ user }}
mode=755
- name: Copy python file over
copy:
src=/home/vuser/sample.py
dest=/home/vuser/test/
owner={{ user }}
group={{ user }}
mode=777
- name: Execute script
command: python sample.py
args:
chdir: /home/vuser/test/
The sample.py is copied correctly in the destination folder at the node1 at dest=/home/vuser/test/
but the I get this error after I have done the change also
fatal: [node1]: FAILED! => {"changed": true, "cmd": ["python", "sample.py"], "delta": "0:00:00.002113", "end": "2019-07-19 10:59:53.7535351", "msg": "non-zero return code", "rc": 1, "start": "2019-07-19 10:59:53.358678548", "stderr": "", "stderr_lines": [], "stdout": "hello world", "stdout_lines": ["hello world"]}
Self explanatory. I want to link based on $(which {{ item }}).
Already saw the register function, but as I need to do a nested loop I'm not sure how to use it.
name: Link bins to user path
command: 'ln -s \$(which {{ item.1 }}) /home/{{ item.0 }}/bin/{{ item.1 }}'
with_nested:
- "{{ jail_users }}"
- "{{ jail_user_commands }}
Output:
failed: [rousertest] (item=[u'bob', u'date']) => {"changed": true,
"cmd": ["ln", "-s", "$(which", "date)", "/home/bob/bin/date"], "delta":
"0:00:00.011825", "end": "2019-07-11 08:17:32.921705", "item": ["bob", "date"], "msg": "non-zero return code", "rc": 1, "start": "2019-07-11
08:17:32.909880", "stderr": "ln: target ‘/home/bob/bin/date’ is not a
directory", "stderr_lines": ["ln: target ‘/home/bob/bin/date’ is not a
directory"], "stdout": "", "stdout_lines": []
Of course I was expecting something like this:
sudo ansible server -i inventory -m shell -a 'echo $(which date)'
rousertest | SUCCESS | rc=0 >>
/usr/bin/date
Below is the play. Avoid using command module for link. Use file module with state=link.
- name: Link binary
hosts: all
gather_facts: true
vars:
files:
- date
- ls
users:
- user1
- user2
tasks:
- name: Find paths
command: which {{ item }}
with_items:
- "{{ files }}"
register: result
- name: Link bins to user path
file:
src: "{{ item.1.stdout }}"
dest: "/home/{{ item.0 }}/bin/{{ item.1.item }}"
owner: "{{ item.0 }}"
group: "{{ item.0 }}"
state: link
with_nested:
- "{{ users }}"
- "{{ result.results }}"