Ansible not picking up custom module - module

I'm having issues with Ansible picking up a module that I've added.
The module is called 'passwordstore' https://github.com/morphje/ansible_pass_lookup/.
I'm using Ansible 2.2
In my playbook, I've added a 'library' folder and have added the contents of that GitHub directory to that folder. I've also tried uncommenting library = /usr/share/ansible/modules and adding the module files there and still doesn't get picked up.
Have also tried setting environment variable to ANSIBLE_LIBRARY=/usr/share/ansible/modules
My Ansible playbook looks like this:
---
- name: example play
hosts: all
gather_facts: false
tasks:
- name: set password
debug: msg="{{ lookup('passwordstore', 'files/test create=true')}}"
And when I run this I get this error;
ansible-playbook main.yml
PLAY [example play] ******************************************************
TASK [set password] ************************************************************
fatal: [backend.example.name]: FAILED! => {"failed": true, "msg": "lookup plugin (passwordstore) not found"}
fatal: [mastery.example.name]: FAILED! => {"failed": true, "msg": "lookup plugin (passwordstore) not found"}
to retry, use: --limit #/etc/ansible/roles/test-role/main.retry
Any guidance on what I'm missing? It may just be the way in which I'm trying to add the custom module, but any guidance would be appreciated.

It's a lookup plugin (not a module), so it should go into a directory named lookup_plugins (not library).
Alternatively, add the path to the cloned repository in ansible.cfg using the lookup-plugins setting.

Related

Ansible Tower 3.7.0 Copy Module Fails To Find or Access Directory

I have an issue with Ansible Tower 3.7.0 (ansible 2.9.7) when using the Copy module I receive this error message:
TASK [Copy Installation Directory For CentOS 7] ********************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the file to exist on the remote, see the remote_src option
fatal: [devmachine]: FAILED! => {"changed": false, "msg": "Could not find or access '/var/lib/awx/projects/xagt_install/Test_Directory' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
However, that directory path does exist:
[root#tower xagt_install]# pwd
/var/lib/awx/projects/xagt_install
[root#tower xagt_install]# ls -Alh
drwxr-xr-x. 2 awx awx 98 Jun 17 12:57 Test_Directory
Here is the task/play:
- name: Copy Installation Directory For CentOS 7
copy:
src: /var/lib/awx/projects/xagt_install/Test_Directory
dest: /tmp/
remote_src: no
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and 'xagt' in ansible_facts.packages)
It appears the "Test_Directory" has the appropriate permissions. Anyone have an idea as to why this module is reporting it cannot "find or access" the directory?
Disabling: Settings --> Jobs --> Enable Job Isolation fixed my issue and the Copy module works.
I assume if I left the Job Isolation enabled then I would need to store directories in /tmp in order for the Copy module to access them?

Multiple entries for the same bucket(default) in the passwd file

I am trying to re-run an Ansible script on an old 3rd party integration, the command looks like this:
- name: "mount s3fs Fuse FS on boot from [REDACTED] on [REDACTED]"
mount:
name: "{{ [REDACTED] }}/s3/file_access"
src: "{{ s3_file_access_bucket }}:{{ s3_file_access_key }}"
fstype: fuse.s3fs
opts: "_netdev,uid={{ uid }},gid={{ group }},mp_umask=022,allow_other,nonempty,endpoint={{ s3_file_access_region }}"
state: mounted
tags:
- [REDACTED]
I'm receiving this error:
fatal: [REDACTED]: FAILED! => {"changed": false, "failed": true, "msg": "Error mounting /home/[REDACTED]: s3fs: there are multiple entries for the same bucket(default) in the passwd file.\n"}
I'm trying to find a passwd file to clean out, but I don't know where to find one.
Anyone recognizes this error?
s3fs checks /etc/passwd-s3fs and $HOME/.passwd-s3fs for credentials. It appears that one of these files has duplicate entries that you need to remove.
Your Ansible src stanza also attempts to supply credentials but I do not believe this will work. Instead you can supply these via the AWSACCESSKEYID and AWSSECRETACCESSKEY environment variables.

trouble with pysphere - ansible

i am trying to deploy a VM via Ansible on my ESXi host.
I am using the following role for this:
- vsphere_guest:
vcenter_hostname: emea-esx-s18t.****.net
username: ****
password: ****
guest: newvm001
state: powered_off
vm_extra_config:
vcpu.hotadd: yes
mem.hotadd: yes
notes: This is a test VM
vm_disk:
disk1:
size_gb: 10
type: thin
datastore: ****
vm_nic:
nic1:
type: vmxnet3
network: VM Network
network_type: standard
vm_hardware:
memory_mb: 4096
num_cpus: 4
osid: windows7Server64Guest
scsi: paravirtual
esxi:
datacenter: MyDatacenter
hostname: esx-s18t.****.net
when i execute this role now via a playbook i get the following message:
root#ansible1:~/ansible# ansible-playbook -i Inventory vmware_deploy.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [172.20.22.5]
TASK [vmware : vsphere_guest] **************************************************
fatal: [172.20.22.5]: FAILED! => {"changed": false, "failed": true, "msg": "pysphere module required"}
PLAY RECAP *********************************************************************
172.20.22.5 : ok=1 changed=0 unreachable=0 failed=1
So it seems to be "pysphere" module is missing. i've already checked that with the command:
root#ansible1:~/ansible# pip install pysphere
Requirement already satisfied (use --upgrade to upgrade): pysphere in /usr/local/lib/python2.7/dist-packages/pysphere-0 .1.7-py2.7.egg
Then i did the "upgrade" and get the following message back:
root#ansible1:~/ansible# pip install pysphere --upgrade
Requirement already up-to-date: pysphere in /usr/local/lib/python2.7/dist-packages/pysphere-0.1.7-py2.7.egg
So it seems to be it is already installed and its up-to-date , why do i get this error message then?
How can i fix it that my god damn role works fine now?
Jesus, Ansible makes me crazy ..
I hope you guys can help me, thanks in advance!
kind regards,
kgierman
EDIT:
so i've writen a new playbook with the old stuff, the new playbool lookes like this(i've added your localhost and connection local stuff):
---
- hosts: localhost
connection: local
tasks:
vsphere_guest:
vcenter_hostname: emea-esx-s18t.****.net
username: ****
password: ****
guest: newvm001
state: powered_off
vm_extra_config:
vcpu.hotadd: yes
mem.hotadd: yes
notes: This is a test VM
vm_disk:
disk1:
size_gb: 10
type: thin
datastore: ****
vm_nic:
nic1:
type: vmxnet3
network: VM Network
network_type: standard
vm_hardware:
memory_mb: 4096
num_cpus: 4
osid: windows7Server64Guest
scsi: paravirtual
esxi:
datacenter: MyDatacenter
hostname: esx-s18t.****.net
so when i execute this playbook i get the following error:
root#ansible1:~/ansible# ansible-playbook vmware2.yml
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/root/ansible/vmware2.yml': line 7, column 19, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
vcenter_hostname: emea-esx-s18t.sddc-hwl-family.net
username: root
^ here
the struggle is real -.-
You generally should execute provisioning modules such as vsphere_guest on your local ansible machine.
I suspect that 172.20.22.5 is actually your ESX host, and ansible try to execute module from there, where pysphere is surely absent.
Use:
- hosts: localhost
tasks:
- vsphere_guest:
...
Ran into this issue once again on macOS / OSX...
It seems to be related to PYTHONPATH.
I have this in my .profile:
export PYTHONPATH="/usr/local/lib/python2.7/site-packages"
[ ... further down ... ]
export PYTHONPATH="/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages:/usr/local/Cellar/ansible/2.2.1.0/libexec/vendor/lib/python2.7/site-packages:$PYTHONPATH"
The first line with PYTHONPATH is where pysphere and other system modules reside.
Also take note of the specific version of Ansible!
Anyway, this seems to resolve the issue.
Source: https://github.com/debops/debops-tools/issues/159#issuecomment-236536195

Overriding Ansible module options defaults

I'm struggling to find out how to override Ansible module options defaults without hand rolling it with variables. Even better if there would be a way to override module options defaults only for a subset of hosts.
Say on couple hosts Git is available at /bin/git, as expected. On couple other hosts Git is at /usr/local/bin/git. How can I override the git module executable option default for the latter group of hosts?
At the moment I'm setting a hosts group variable like:
git_executable=/usr/local/bin/git
and using it with default(omit) filter everywhere git is used like so:
- git: "executable={{git_executable|default(omit)}} ..."
So it gets properly overriden on hosts where it's defined, and ignored on others.
executable may not be the best example here, since that may be controlled with PATH environment variable or something. So what about any kind of module option in general that I'd like to override for just some hosts but otherwise fall back to module default?
Since there is couple more of such basic differences in this environment, it's quite tedious to sprinkle this kind of default lookup using variables all over the place just in case it gets run on a host with non-default setup. Is there a way to do this better?
I don't think there is a better option. Modules only know what you pass to them. They do not have access to global vars, server facts or anything else unless you explicitly pass it as a module parameter.
If this really really is important and you want to invest some time, you could create your own action plugin(s). Action plugins are local actions, therefore have access to the Ansible runnerc class and all its properties including facts etc. So you could handle the default parameters or executable detection in there based on server facts and then call the git or whatever module programatically. Huge overhead in my opinion but that depends on view and might be feasible on your end.
Though take care, action plugins are 100% undocumented. Ansible 2.0 is going to be released in the next days. They claim 100% backwards compatibility but I wouldn't be surprised if that only counts for documented features.
In this specific case git.executable as long as it's in PATH or '/sbin', '/usr/sbin', '/usr/local/sbin' git module would find it because it uses the basic.get_bin_path()
On the larger topic, personally I would go with what you already did. But if you are bent on it one other possible hack would be to [mis]use the include statement to create a wrapper for each module that supplies the default value you want from some variable.
Obviously you would have to specify the path somewhere yourself either in group_vars or host/role/... vars. Or a variable defined in the play's vars section.
$ cat my_echo.yml
- shell: "{{echo_exec}} '{{text}}'"
$ cat playbook.yml
- hosts: localhost
tags: so
gather_facts: False
vars:
echo_exec: echo
tasks:
- include: my_echo.yml text='some text'
changed_when: False
- hosts: localhost
tags: so
gather_facts: False
vars:
echo_exec: printf
tasks:
- include: my_echo.yml text='some text'
changed_when: False
$ ansible-playbook playbook.yml -t so -v
PLAY [localhost] **************************************************************
TASK: [shell {{echo_exec}} '{{text}}'] ****************************************
changed: [localhost] => {"changed": true, "cmd": "echo 'some text'", "delta": "0:00:00.003782", "end": "2015-03-20 17:45:58.352069", "rc": 0, "start": "2015-03-20 17:45:58.348287", "stderr": "", "stdout": "some text", "warnings": []}
PLAY [localhost] **************************************************************
TASK: [shell {{echo_exec}} '{{text}}'] ****************************************
changed: [localhost] => {"changed": true, "cmd": "printf 'some text'", "delta": "0:00:00.003705", "end": "2015-03-20 17:45:58.690657", "rc": 0, "start": "2015-03-20 17:45:58.686952", "stderr": "", "stdout": "some text", "warnings": []}
PLAY RECAP ********************************************************************
localhost : ok=2 changed=2 unreachable=0 failed=0
$

Ansible error change handler is not defined

I'm trying to run my first playbook to install Java on four servers and subsequently define a JAVA_HOME environment variable.
ansible-playbook site.yml --check
PLAY [crave_servers] **********************************************************
GATHERING FACTS ***************************************************************
ok: [54.174.151.196]
ok: [54.174.197.35]
ok: [54.174.207.83]
ok: [54.174.208.240]
TASK: [java | install Java JDK] ***********************************************
changed: [54.174.197.35]
changed: [54.174.151.196]
changed: [54.174.208.240]
changed: [54.174.207.83]
ERROR: change handler (setvars) is not defined
I've placed my site.yml under /etc/ansible
---
- hosts: crave_servers
remote_user: ubuntu
sudo: yes
roles:
- java
I've placed main.yml under /etc/ansible/java/tasks
---
- name: install Java JDK
apt: name=default-jdk state=present
notify:
- setvars
I've placed main.yml under /etc/ansible/handlers
---
- name: setvars
shell: echo "JAVA_HOME=\"/usr/lib/jvm/java-7-openjdk-amd64\"" >> /etc/environment
Now I'm not sure if the syntax is structure of my handlers is correct. But it's obvious from the output that Ansible is able to find the correct role and execute the correct task. But the task can't find the handler.
Nobody else seems to have the same problem. And I don't really know how to debug it because my ansible version seems to be missing the config file.
You should put your handler to /etc/ansible/java/handlers/main.yml
As handlers are part of a role.
Remarks:
You should not use your handler as it would paste the line into /etc/environment each time you call this playbook. I would recommend the lineinefile module.
You should reconsider your decision to put ansible playbooks into /etc