Removing files except for some in ansible [duplicate] - ansible

This question already has answers here:
How to use Ansible's with_item with a variable?
(2 answers)
Closed 5 years ago.
I have the following files in my tmp directory
root#ansible:/tmp/test$ ls /tmp/test/
file1 file2 file3
I also have the following playbook which is modeled after this
vars:
exclude_files: file1
tasks:
- name: check files
shell: ls -l /tmp/test
register: capture
- name: remove files
file: path=/tmp/test/{{item}} state=absent
with_items: capture.stdout_lines
when: item not in exclude_files
- name: debug variable
debug: msg={{exclude_files}}
Unfortunately the second task isn't removing file2,file3. Instead it's treating the registered variable as a file.
ok: [172.16.2.3] => (item=capture.stdout_lines) => {
"changed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"content": null,
"delimiter": null,
"diff_peek": null,
"directory_mode": null,
"follow": false,
"force": false,
"group": null,
"mode": null,
"original_basename": null,
"owner": null,
"path": "/tmp/test/capture.stdout_lines",
"recurse": false,
"regexp": null,
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"state": "absent",
"unsafe_writes": null,
"validate": null
}
},
"item": "capture.stdout_lines",
"path": "/tmp/test/capture.stdout_lines",
"state": "absent"
Any idea why this code isn't working? I am using ansible 2.3.

with_items: capture.stdout_lines should be with_items: "{{capture.stdout_lines}}"
Bare variables in with_... are deprecated long ago.

Related

VirtualBox silent install via Ansible fails?

$ ansible --version
ansible 2.10.8
Per Oracle VM VirtualBox 6.1 Silent Install (How-To Guide), I should be able to install VirtualBox silently like this
C:\temp> VirtualBox-6.1.28-147628-Win.exe --silent --ignore-reboot
and if I run the above command in a Windows command prompt, it succeeds and installs VirtualBox.
Now I do this in Ansible
- name: "Install {{ artifact_filename }}"
win_package:
path: "C:\\temp\\VirtualBox-6.1.28-147628-Win.exe"
arguments: "--silent --ignore-reboot"
state: present
and I get this when I run the playbook with the task.
fatal: [10.227.x.x]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"arguments": "--silent --ignore-reboot",
"chdir": null,
"client_cert": null,
"client_cert_password": null,
"creates_path": null,
"creates_service": null,
"creates_version": null,
"expected_return_code": [
0,
3010
],
"follow_redirects": "safe",
"force_basic_auth": false,
"headers": null,
"http_agent": "ansible-httpget",
"log_path": null,
"maximum_redirection": 50,
"password": null,
"path": "C:\\temp\\VirtualBox-6.1.28-147628-Win.exe",
"product_id": null,
"provider": "auto",
"proxy_password": null,
"proxy_url": null,
"proxy_use_default_credential": false,
"proxy_username": null,
"state": "present",
"url_method": null,
"url_password": null,
"url_timeout": 30,
"url_username": null,
"use_default_credential": false,
"use_proxy": true,
"username": null,
"validate_certs": true,
"wait_for_children": false
}
},
"msg": "unexpected rc from 'C:\\temp\\VirtualBox-6.1.28-147628-Win.exe --silent --ignore-reboot': see rc, stdout, and stderr for more details",
"rc": 1,
"reboot_required": false,
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
What am I missing? TIA.
I RTFM'd the win_package documentation. It says regarding the arguments option
arguments This is only used for the msi, msp, and registry providers.
So I do this instead
- name: "Install VirtualBox"
win_command: "C:\\temp\\VirtualBox-6.1.28-147628-Win.exe --silent --ignore-reboot"
If anyone has a better answer I'm all eyes.

Ansible Symbolic Link Task Role Failure

I am new to ansible and executing the following ansible task:
- name: Create symbolic links
file:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ jboss_usr }}"
group: "{{ jboss_grp }}"
state: link
with_items:
- { src: "/apps/etc/jboss", dest: "/etc/jboss" }
- { src: "/apps/var/log/jboss", dest: "/var/log/jboss" }
And I got the following error:
2018-12-21 21:27:23,469 p=15185 u=ex_sam | failed: [hostname.x] (item={u'dest': u'/etc/jboss', u'src': u'/apps/etc/jboss'}) => {
"changed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"content": null,
"delimiter": null,
"dest": "/etc/jboss",
"diff_peek": null,
"directory_mode": null,
"follow": true,
"force": true,
"group": "jboss",
"mode": null,
"original_basename": null,
"owner": "jboss",
"path": "/etc/jboss",
"recurse": false,
"regexp": null,
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": "/apps/etc/jboss",
"state": "link",
"unsafe_writes": null,
"validate": null
}
},
"item": {
"dest": "/etc/jboss-as",
"src": "/apps/etc/jboss"
},
"msg": "Error while linking: [Errno 13] Permission denied",
"path": "/etc/jboss-as",
"state": "absent"
}
I am trying to find out why the symbolic link creation failed.
I read the following:
https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#id6
I says the "changed" attribute is a boolean indicating if the task had to make changes.
But, there are lots of null parameters in the invocation:module_args elements of the json
Does that mean the values are really “null” or they are being set to a default value?
I have looked into the ansible documentation and I am not sure if the invocation:module_args null values are representative of the outcome of the trying to create the symbolic link i.e. are the null input or output of executing the tasks.
I think some of the nulls are defaults, but will appreciate some helpful comments on the possible relation between the json returned in my ansible error log and the actual "Error while linking: [Errno 13] Permission denied".
Thanks all for you anticipated help.
I think, is the permissions on /etc. So probably you need to add the option:
become: true
To your task.

Is there a way to capture the failure output of lineinfile in Ansible

I have a playbook with lineinfile. I need to capture the failure.
When I use register the failed ones of lineinfile are not being capture also the success one does not have enough information like a general register values. (Eg. there is no rc code, stdout or anything)
I need to capture the hosts on which the lineinfile task failed.
Please let me know if there is any method to achieve the requirement.
lineinfile:
path: /test
regexp: "^host"
line: "host myhost 127.0.0.1"
state: present
register: result
I am trying capture this message that is displayed in the verbose but unable to capture it with a register so that I can use the when condition and say when rc == 257 it is to be marked as failed.
fatal: [examplehost]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"attributes": null,
"backrefs": false,
"backup": true,
"content": null,
"create": false,
"delimiter": null,
"directory_mode": null,
"firstmatch": false,
"follow": false,
"force": null,
"group": null,
"insertafter": null,
"insertbefore": null,
"line": "HOST example.something 127.0.0.1",
"mode": null,
"owner": null,
"path": "/test/my",
"regexp": "^HOST",
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"state": "present",
"unsafe_writes": null,
"validate": null
}
},
"msg": "Destination /test/my does not exist !",
"rc": 257
I don't understand your complaint; lineinfile: is going to ensure that line appears in the target file, and will only use the regexp: bit to assist it in placing the line. If the file does not have a line that starts with host, then lineinfile: will just append the line: to the bottom of the file, and cheerfully report that it did so in result.msg:
changed: [host0] => {"backup": "", "changed": true, "msg": "line added"}
changed: [host1] => {"backup": "", "changed": true, "msg": "line replaced"}

Ansible to Proxmox returning 500 error

I am trying to use Ansible to provision some VMs on my newly set up Proxmox VE. I have installed proxmoxer and request with PIP on both my local Mac and Proxmox VE (Python 2 on Proxmox and Python 3 locally). I use Ansible 2.4.3.0, Proxmox version: 5.1-41.
I do have a vm with the id of 100, which was created from a Debian template, the vm is located on local-lvm (pve).
My full playbook can be found at: https://github.com/atwright147/ansible-contact-book-proxmox-provisioner, the specific task is pasted below:
---
- proxmox_kvm:
api_user: root#pam
api_password: REDACTED
api_host: pve
vmid: 100
state: current
When running this script via: ansible-playbook -vvv --connection=local -i hosts site.yml I get the following error:
The full traceback is:
File "/tmp/ansible_TDEJsZ/ansible_module_proxmox_kvm.py", line 1227, in main
current = getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status']
File "/usr/local/lib/python2.7/dist-packages/proxmoxer/core.py", line 84, in get
return self(args)._request("GET", params=params)
File "/usr/local/lib/python2.7/dist-packages/proxmoxer/core.py", line 79, in _request
resp.content))
fatal: [192.168.0.22]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"acpi": true,
"agent": null,
"api_host": "pve",
"api_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"api_user": "root#pam",
"args": null,
"autostart": false,
"balloon": 0,
"bios": null,
"boot": "cnd",
"bootdisk": null,
"clone": null,
"cores": 1,
"cpu": "kvm64",
"cpulimit": null,
"cpuunits": 1000,
"delete": null,
"description": null,
"digest": null,
"force": null,
"format": "qcow2",
"freeze": null,
"full": true,
"hostpci": null,
"hotplug": null,
"hugepages": null,
"ide": null,
"keyboard": null,
"kvm": true,
"localtime": null,
"lock": null,
"machine": null,
"memory": 512,
"migrate_downtime": null,
"migrate_speed": null,
"name": null,
"net": null,
"newid": null,
"node": null,
"numa": null,
"numa_enabled": null,
"onboot": true,
"ostype": "l26",
"parallel": null,
"pool": null,
"protection": null,
"reboot": null,
"revert": null,
"sata": null,
"scsi": null,
"scsihw": null,
"serial": null,
"shares": null,
"skiplock": null,
"smbios": null,
"snapname": null,
"sockets": 1,
"startdate": null,
"startup": null,
"state": "current",
"storage": null,
"tablet": false,
"target": null,
"tdf": null,
"template": false,
"timeout": 30,
"update": false,
"validate_certs": false,
"vcpus": null,
"vga": "std",
"virtio": null,
"vmid": 100,
"watchdog": null
}
},
"msg": "Unable to get vm None with vmid = 100 status: 500 Internal Server Error: {\"data\":null}"
}
Ansible info:
ansible 2.4.3.0
config file = /Users/andy/Development/proxmox-playbooks/contact-book/ansible.cfg
configured module search path = ['/Users/andy/.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.4 (default, Jan 25 2018, 18:48:20) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]
What am I doing wrong?
It turned out that I was making several mistakes here.
I should have been using the proxmox module rather than proxmox_kvm
I needed to use the storage param to set up the container in local-lvm e.g. storage: local-lvm
My final, working task looks like this:
- name: "Create a Linux Container (LXC)"
proxmox:
node: pve
api_user: root#pam
api_password: proxmox_password
api_host: pve
password: vm_password
hostname: vm.hostname.local
ostemplate: "local:vztmpl/ubuntu-16.04-standard_16.04-1_amd64.tar.gz"
storage: local-lvm
cores: 2
state: present

How to create a directory with particular SE Linux context on it

Here is what I have:
- name: Create directories that will be used as persistent volumes
become: yes
become_method: sudo
file:
path: /tmp/pv-{{ item }}
state: directory
mode: "g=rwx"
group: "root"
selevel: _default
seuser: _default
serole: _default
setype: svirt_sandbox_file_t
with_items:
- cassandra
- services
The two directories are correctly created, group a rights are ok. But the SE Linux context is wrong.
$ ll -dZ /tmp/pv-cassandra
drwxrwxr-x. 2 jkremser root unconfined_u:object_r:user_tmp_t:s0 40 Mar 9 15:19 /tmp/pv-cassandra
This is the part of the debugging output:
ok: [localhost] => (item=cassandra) => {
"changed": false,
"diff": {
"after": {
"path": "/tmp/pv-cassandra"
},
"before": {
"path": "/tmp/pv-cassandra"
}
},
"gid": 0,
"group": "root",
"invocation": {
"module_args": {
"backup": null,
"content": null,
"delimiter": null,
"diff_peek": null,
"directory_mode": null,
"follow": false,
"force": false,
"group": "root",
"mode": "g=rwx",
"original_basename": null,
"owner": null,
"path": "/tmp/pv-cassandra",
"recurse": false,
"regexp": null,
"remote_src": null,
"selevel": "_default",
"serole": "_default",
"setype": "svirt_sandbox_file_t",
"seuser": "_default",
"src": null,
"state": "directory",
"unsafe_writes": null,
"validate": null
},
"module_name": "file"
},
"item": "cassandra",
"mode": "0775",
"owner": "root",
"path": "/tmp/pv-cassandra",
"size": 80,
"state": "directory",
"uid": 0
}
What am I doing wrong? My OS is Fedora 25.
If I use the copy module, instead of the file, that has almost the same parameters, it throws this error:
Aborting, target uses selinux but python bindings (libselinux-python) aren't installed
After installing the libselinux-python package, it works! So it looks like the file module silently swallows the error and does what it can do :( I can't depend on the fact that user has the libselinux-python package installed.
I'll probably call the chcon myself as a shell command or add the package as a prerequisite:
- name: Install the libselinux-python package
package:
name: libselinux-python
state: present

Resources