This is my yml file:
- hosts: webservers
vars:
www_port: 80
become: yes
tasks:
- name: install lsof if it's redhat
yum:
name: lsof
state: latest
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'
- name: install lsof if it's debian
apt:
name: lsof
state: latest
when: ansible_distribution == 'Ubuntu' or ansible_distribution == 'Debian'
- name: Finding out what web server it uses
shell: "lsof -i :{{ www_port }} | grep LISTEN | cut -d ' ' -f 1"
register: result
- name: output the result of what web server it uses
debug: msg="{{ result.stdout_lines|first }}"
- name: list all vhosts if it uses httpd
shell: cd /etc/httpd/sites-enabled; ls | while read file; do awk '/^ServerName/ {f=1} f {for(i=1;i<=NF;i++)$
register: rezult
when: result.stdout_lines|first == "httpd"
- name: output vhosts rezult if it uses httpd
debug: msg="{{ rezult.stdout_lines }}"
when: result.stdout_lines|first == "httpd"
- name: list all vhosts if it uses nginx
shell: cd /etc/nginx/sites-enabled; ls | while read file; do awk '/^server_name/ {f=1} f {for(i=1;i<=NF;i++$
register: recult
when: result.stdout_lines|first == "nginx"
- name: output vhosts recult (results) if it uses nginx
debug: msg="{{ recult.stdout_lines }}"
when: result.stdout_lines|first == "nginx"
I want to turn these “shell:” module commands into my own module using a bash script since I don’t know python. Essentially this playbook outputs the name of the webserver the host uses and it outputs all the vhosts in the the webserver configuration file. I want to combine these shell: module commands into one module that does all of those things.
ansible.cfg file states that the library of my own modules are in /usr/share/my_modules so I made that directory and made a vhosts.bash script that is basically the second shell command in the yml file. I replaced that line (“shell: ….”) with ony “vhosts:” but that didn’t work.
This is the vhosts.bash file in /usr/share/my_modules
#!/bin/bash
cd /etc/httpd/sites-enabled;
ls | while read file;
do awk '/^ServerName/ {f=1} f {for(i=1;i<=NF;i++) if ($i~/./) print $i; else if($i!="ServerName") exit}' $file;
done;
and when I run the playbook I get:
- name: list all vhosts if it uses httpd
^ here
but it has nothing to do with the name, this is the line in the yml:
- name: list all vhosts if it uses httpd
vhosts:
register: rezult
I don’t know if I completly missed the point but that’s kind of how this guy did it in this video: https://www.youtube.com/watch?time_continue=1&v=aY7lmtW8ROM
2
Answers
Not sure your indentation is right, the line after name should be in line like:
- name: list all vhosts if it uses httpd
vhosts:
Looks like you are picking up the default module paths:
You will need to either move your module, change the global ansible.cfg, or create a local ansible.cfg. If the module is specific to this playbook, you can also consider just placing it in a folder called
library
in the playbook directory.The second issue you will run into is that your bash module needs to output the success/failure status in a way that is readable by Ansible. As an example, you would need to have output similar to: