Ansible: Copying files from local to remote - "AttributeError: 'dict' object has no attribute 'endswith'" - ansible

I am trying to copy files (scripts and rpms) stored locally to a set of servers. I can copy the files when the names are hard coded, but not when I am using a variable.
ansible-lint comes back with no errors.
When use variable replacement I get the error:
TASK [Copy cpu_gov.sh]
***************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'dict' object has no attribute 'endswith'
fatal: [ceph3]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}
In debug mode I can see that it is a Python error on a trailing "/". All other uses of the variable work fine, only when it is in the ""src:" field does it fail.
The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 145, in run
res = self._execute()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 650, in _execute
result = self._handler.run(task_vars=variables)
File "/usr/lib/python2.7/site-packages/ansible/plugins/action/copy.py", line 461, in run
trailing_slash = source.endswith(os.path.sep)
AttributeError: 'dict' object has no attribute 'endswith'
fatal: [ceph3]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
---
### Test
#
- hosts: all
vars:
#isdct_rpm: foobar.txt
isdct_rpm: isdct-3.0.16-1.x86_64.rpm
cpu_gov: cpu_gov.sh
irq_bal: irq_balance.sh
root_dir: /root
bin_dir: /root/bin
files_dir: /root/projects/ansible/bootstrap/files
remote_user: root
tasks:
These work just fine -
- name: ISDCT rpm exists?
stat:
path: "{{ root_dir }}/{{ isdct_rpm }}"
register: isdct_rpm
tags:
- tools
- name: cpu_gov exists?
stat:
path: "{{ bin_dir }}/{{ cpu_gov }}"
register: cpu_gov
tags:
- tools
- name: irq_balance exists?
stat:
path: "{{ bin_dir }}/{{ irq_bal }}"
register: irq_bal
tags:
- tools
The first task is the failing one:
- name: Copy ISDCT rpm
copy:
remote_src: no
src: "{{ isdct_rpm }}"
dest: "{{ root_dir }}"
when: not isdct_rpm.stat.exists
These work fine:
- name: Copy rpm
copy:
remote_src: no
src: isdct-3.0.16-1.x86_64.rpm
dest: /root
when: not isdct_rpm.stat.exists
- name: Copy cpu_gov.sh
copy:
remote_src: no
src: cpu_gov.sh
# - fails - src: "{{ cpu_gov }}"
dest: "{{ bin_dir }}"
when: not cpu_gov.stat.exists
- name: Copy irq_balance.sh
copy:
remote_src: no
src: irq_balance.sh
dest: /root
when: not irq_bal.stat.exists
The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 145, in run
res = self._execute()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 650, in _execute
result = self._handler.run(task_vars=variables)
File "/usr/lib/python2.7/site-packages/ansible/plugins/action/copy.py", line 461, in run
trailing_slash = source.endswith(os.path.sep)
AttributeError: 'dict' object has no attribute 'endswith'
fatal: [ceph3]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}

You have a variable in your vars section named isdct_rpm which is a string, but you're registering a dictionary variable with the same name in your ISDCT rpm exists? task. This overrides the string value.
Stop trying to use the same variable name for two different purposes and I suspect things will work as expected.

#larsks Answered my question. I was using the same name for my variable and the register value.
This works:
#
###--- Copy the scripts over if needed
#
- name: Copy ISDCT rpm
copy:
remote_src: no
src: "{{ isdct_rpm }}"
dest: "{{ root_dir }}"
when: not isdctrpm.stat.exists
- name: Copy cpu_gov.sh
copy:
remote_src: no
#src: cpu_gov.sh
src: "{{ cpu_gov }}"
dest: "{{ bin_dir }}"
when: not cpugov.stat.exists
- name: Copy irq_balance.sh
copy:
remote_src: no
src: "{{ irq_bal }}"
dest: "{{ bin_dir }}"
when: not irqbal.stat.exists

Related

ansible "with_lines: cat somefile" fails in a skipped block

I have a playbook with a block that has a when condition. Inside is a task with a loop. How can I change this loop so that when the condition is false the skipped task doesn't fail?
block:
- name: create a file
lineinfile:
line: "Hello World"
path: "{{my_testfile}}"
create: yes
- name: use the file
debug:
msg: "{{ item}}"
with_lines: cat "{{my_testfile}}"
when: false
TASK [create a file] ************************************************************************************************************************************************************
TASK [use the file] *************************************************************************************************************************************************************
cat: files/my/testfile: No such file or directory
fatal: [ipad-icpi01]: FAILED! => {"msg": "lookup_plugin.lines(cat \"files/mytestfile\") returned 1"}
Change your failing task to the following which will always be able to run, even if the file does not exists, and will not use the shell or command where there is no need to:
- name: use the file
debug:
msg: "{{ item }}"
loop: "{{ (lookup('file', my_testfile, errors='ignore') | default('', true)).split('\n') }}"
The key points:
use the file lookup plugin with errors='ignore' so that it returns the file content or None rather than an error when file does not exists.
use the default filter with second option to true so that it return default value if var exists but is null or empty.
split the result on new lines to get a list of lines (empty list if file does not exist).
Note: as reported by #Vladimir, I corrected your var name which is not valid in ansible.
Test the existence of the file. For example
- block:
- name: create a file
lineinfile:
line: "Hello World"
path: "{{ my_testfile }}"
create: yes
- name: use the file
shell: '[ -f "{{ my_testfile }}" ] && cat {{ my_testfile }}'
register: result
- name: use the file
debug:
msg: "{{ item }}"
loop: "{{ result.stdout_lines }}"
when: false
The lookup plugin file should be preferred.
I ended up with a mix of the provided answers. These tasks will be skipped without failing or creating a warning.
- block:
- name: create a file
lineinfile:
line: "Hello World"
path: "{{ my_testfile }}"
create: yes
- name: get the file
slurp:
src: "{{ my_testfile }}"
register: result
- name: use the file
debug:
msg: "{{ item }}"
loop: "{{ (result['content'] | b64decode).split('\n') }}"
when: false

Ansible: undefined variable when copying file on local

im about to make an ansible playbook for automatic updating some librarys
The error msg i got :
"msg": "The task includes an option with an undefined variable. The
error was: 'lib_wheel_path' is undefined\n\nThe error appears to
be in '/home/user/Desktop/setup/roles/lib/tasks/main.yml': line 4,
column 3, but may\nbe elsewhere in the file depending on the exact
syntax problem.\n\nThe offending line appears to be:\n\n\n- name:
Download lib\n ^ here\n"
my ansible/vars file contains the path to libs :
local_path: "~/Some_Libraries/"
lib_wheel_path: "{{ local_path }}lib/"
the ansible/role/lib/vars/main.yml file contains :
lib_path: "{{ lib_wheel_path }}"
lib_wheels: "'{{ lib_path }}' | regex_findall('(lib\\S*\\.whl)') | sort(reverse=True) }}"
the ansible/role/lib/tasks/main.yml file contains :
- name: Download lib
copy:
src: "{{ lib_path }}/{{ lib_wheels[0] }}"
dest: /tmp
remote_src: true
- name: Install lib
pip:
name: file:///tmp/{{ lib_wheels[0] }}
become: yes
It's expected it will include the ansible/vars which contains the lib_wheel_path, i f im not wrong about how ansible works
You have to include the var file.
for eg: If your var file is ansible/role/lib/vars/lib.yml
local_path: "~/Some_Libraries/"
lib_wheel_path: "{{ local_path }}lib/"
In your playbook include it as below.
- name: vars
include_vars: lib.yml
- name: Download lib
copy:
src: "{{ lib_path }}/{{ lib_wheels[0] }}"
dest: /tmp
remote_src: true
- name: Install lib
pip:
name: file:///tmp/{{ lib_wheels[0] }}
become: yes

How can I use the mv module Ansible

I am trying to use the mv module on Ansible but I am not having luck.
In my initial attempt I did the following:
- name: changing the name of the file
shell: mv /tmp/bundle /opt/Rocket.Chat
And I get the following error:
FAILED! => {"changed": true, "cmd": "mv /tmp/bundle /opt/Rocket.Chat", "delta": "0:00:00.033553", "end": "2019-02-11 06:06:43.273787", "msg": "non-zero return code", "rc": 1, "start": "2019-02-11 06:06:43.240234", "stderr": "mv: cannot move ‘/tmp/bundle’ to ‘/opt/Rocket.Chat/bundle’: File exists", "stderr_lines": ["mv: cannot move ‘/tmp/bundle’ to ‘/opt/Rocket.Chat/bundle’: File exists"], "stdout": "", "stdout_lines": []}
So, I changed it to:
- name: create directory
file:
state: directory
path: "/opt/Rocket.Chat"
- name: copy the files
copy:
src: "/tmp/bundle"
dest: "/opt/Rocket.Chat"
remote_src: yes
- name: delete the other files
file: path=/tmp/bundle state=absent
My new error is:
FAILED! => {"changed": false, "msg": "Remote copy does not support recursive copy of directory: /tmp/bundle"}
Seems that the "copy module to work with recursive and remote_src" does not work yet, but will be supported from May 2019
Here is a workaround, edit the folder names to your setup.
# Copy all files and directories from /usr/share/easy-rsa to /etc/easy-rsa
- name: List files in /usr/share/easy-rsa
find:
path: /usr/share/easy-rsa
recurse: yes
file_type: any
register: find_result
- name: Create the directories
file:
path: "{{ item.path | regex_replace('/usr/share/easy-rsa','/etc/easy-rsa') }}"
state: directory
mode: "{{ item.mode }}"
with_items:
- "{{ find_result.files }}"
when:
- item.isdir
- name: Copy the files
copy:
src: "{{ item.path }}"
dest: "{{ item.path | regex_replace('/usr/share/easy-rsa','/etc/easy-rsa') }}"
remote_src: yes
mode: "{{ item.mode }}"
with_items:
- "{{ find_result.files }}"
when:
- item.isdir == False

ansible AttributeError: 'list' object has no attribute 'startswith'

I am running ansible and trying to make this task work, it fails with this error:
An exception occurred during task execution. To see the full
traceback, use -vvv. The error was: AttributeError: 'list' object has
no attribute 'startswith' fatal: [test-1]: FAILED! =>
{"failed": true, "msg": "Unexpected failure during module execution.",
"stdout": ""} msg: Unexpected failure during module execution.
The code is:
- name: Register env Type
shell: facter configured_setup
register: setup
- name: foo tasks
shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
register: test
when: setup.stdout == "something"
- name: fetch group_vars
fetch:
src:
- { "{{ item }}", when: setup.stdout == "something" }
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
"fetch group_vars" is the task that always fails, any idea how this can work?
What I am trying to do is add more source files to fetch from different setvers.
So I want to have more lines under "src:", saying - { filename, when setup.stdout =="something else" }
The full error is:
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 96, in run
item_results = self._run_loop(items)
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 252, in _run_loop
res = self._execute(variables=task_vars)
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 446, in _execute
result = self._handler.run(task_vars=variables)
File "/usr/lib/python2.7/site-packages/ansible/plugins/action/fetch.py", line 62, in run
source = self._remote_expand_user(source)
File "/usr/lib/python2.7/site-packages/ansible/plugins/action/init.py", line 460, in _remote_expand_user
if not path.startswith('~'): # FIXME: Windows paths may start with "~ instead of just ~
AttributeError: 'list' object has no attribute 'startswith'
fatal: [test-1]: FAILED! => {"failed": true, "msg":
"Unexpected failure during module execution.", "stdout": ""} msg:
Unexpected failure during module execution.
The ansible documentation clearly states that fetch, fetches a file, not a list of files. Although one can program an application to deal with both a scalar and a sequence loaded from a YAML document, that is not automatic and would almost certainly have been reflected in the documentation.
Since you already have a sequence at a higher level, just extend that.
- name: Register env Type
shell: facter configured_setup
register: setup
- name: transparency tasks
shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
register: test
when: setup.stdout == "something"
- name: fetch group_vars
fetch:
src: { "{{ item }}", when: setup.stdout == "something" }
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
- name: fetch group_vars2
fetch:
src: { filename, when setup.stdout =="something else" }
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
You might be able to reduce the repetitiveness somewhat by using YAML's anchor and merge:
- name: Register env Type
shell: facter configured_setup
register: setup
- name: transparency tasks
shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
register: test
when: setup.stdout == "something"
- &fetchtask
name: fetch group_vars
fetch: &fetchsrc
src: { "{{ item }}", when: setup.stdout == "something" }
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
- <<: *fetchtask
name: fetch group_vars2
fetch:
<<: *fetchsrc
src: { filename, when setup.stdout =="something else" }
Ansible probably expands the {{...}} before handing the document to the YAML parser, otherwise the value for shell in the "transparency task" would throw an error. But you should probably still quote that like you do with the value for dest
So I ended up doing this (which is working):
- name: fetch group_vars test
fetch:
src: "{{ item }}"
dest: "{{group_vars}}"
flat: yes
with_items:
- "{{ test.stdout_lines[0] }}"
- "{{ test.stdout_lines[1] }}"
when: setup.stdout == "something" and {{something_else}} == True
I also noticed that there might be a bug in ansible related to registers.
while using the "when" statement, even if the condition is not met, the register statement takes affect:
- name: test tasks something enabled
shell: /tmp/{{populate_script}} -u -a {{hosts}} -r
register: variable
when: setup.stdout == "test" and something == True
- name: test tasks something disabled
shell: /tmp/{{populate_script}} -u -a {{hosts}}
register: variable
when: setup.stdout == "test" and something == False
only one of these conditions will be met, in case the first one is met the second condition will override "variable"

Ansible - How to use register name from with_items

In my current playbook, I have something like the following:
- name: Copy cov-analysis-linux64-8.0.0.tgz
copy: src=/home/devops/chroot/cov-analysis-linux64-8.0.0.tgz dest=/var/tmp/cov-analysis.tgz owner=devops
register: coverity
- name: Copy fortidev-2.02.chroot.tar.bz2
copy: src=/home/devops/chroot/fortidev-2.02.chroot.tar.bz2
dest=/var/tmp/fortidev2.chroot.tar.bz2 owner=devops
register: fortidev2
The list is getting longer and longer and for code readability, I want to use with_items to do it. I've updated it to something like:
- name: copy chroot tarball to the servers
copy: src={{ item.src }} dest={{ item.dest }} owner=devops
register: "{{ item.register }}"
with_items:
- { src: /home/devops/chroot/cov-analysis-linux64-8.0.0.tgz,
dest: /var/tmp/cov-analysis.tgz,
register: coverity
}
- { src: /home/devops/chroot/fortidev-2.02.chroot.tar.bz2,
dest: /var/tmp/fortidev2.chroot.tar.bz2,
register: fortidev2
}
- { src: /home/devops/chroot/fmdev-6.0.tar.xz,
dest: /var/tmp/fmdev6.tar.xz,
register: fmdev6
}
The problem is now when I want to refer to one of the registers in subsequent tasks, it couldn't find the variable. The code to refer it is:
- name: umount fortidev2 /proc|/dev|/tmp|/cov-analysis
command: umount {{ item }}
become: yes
when: fortidev2.changed
with_items:
- /home/devops/fortidev2/proc
- /home/devops/fortidev2/dev
- /home/devops/fortidev2/tmp
ignore_errors: yes
The error is
{"failed": true, "msg": "The conditional check 'fortidev2.changed' failed. The error was: error while evaluating conditional (fortidev2.changed): 'fortidev2' is undefined\n\nThe error appears to have been in '/var/lib/jenkins/jobs/Devops/jobs/update_chroot/workspace/roles/chroot/tasks/main.yml': line 70, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# unmount /proc and /dev first before deleting the folder\n- name: umount fortidev2 /proc|/dev|/tmp|/cov-analysis\n ^ here\n"}
Try this:
- name: copy chroot tarball to the servers
copy: src={{ item.src }} dest={{ item.dest }} owner=devops
register: tarballs
with_items:
- { src: /home/devops/chroot/cov-analysis-linux64-8.0.0.tgz,
dest: /var/tmp/cov-analysis.tgz,
name: coverity
}
- { src: /home/devops/chroot/fortidev-2.02.chroot.tar.bz2,
dest: /var/tmp/fortidev2.chroot.tar.bz2,
name: fortidev2
}
- { src: /home/devops/chroot/fmdev-6.0.tar.xz,
dest: /var/tmp/fmdev6.tar.xz,
name: fmdev6
}
- name: umount fortidev2 /proc|/dev|/tmp|/cov-analysis
command: umount {{ item }}
become: yes
when: tarballs.results | selectattr('item.name','equalto','fortidev2') | join('',attribute='changed') | bool
with_items:
- /home/devops/fortidev2/proc
- /home/devops/fortidev2/dev
- /home/devops/fortidev2/tmp

Resources