Can this be concatinated? Made more graceful?

17 views
Skip to first unread message

John Harmon

unread,
Oct 20, 2017, 5:24:02 PM10/20/17
to Ansible Project
I have the following, which works, but it just got me wondering if this can be concatenated or be written in a more graceful way? By that, I don't mean getent: database=passwd, key=nagios

- name:  Check for nagios installation
  getent
:
    database
: passwd
    key
: nagios
  ignore_errors
: yes
 
register: nagios_user

- name:  Copy nrpe.cfg to clients when installed
  copy
:
    src
: /etc/ansible/masterfiles/nagios/nrpe.cfg
    dest
: /usr/local/nagios/etc/
    owner
: nagios
   
group: nagios
    mode
: 0644
    force
: yes
 
when: nagios_user|succeeded
  notify
: reload xinetd


wildfan

unread,
Oct 21, 2017, 12:18:05 PM10/21/17
to Ansible Project
If it were my code, i'd probably instead of checking for the nagios user existing, check that the package is installed ( ie rpm -q and register output), or do a stat on the /usr/local/nagios/etc directory and copy the file if is exists..  its not a whole lot compact or cleaner than what you have, but it at least wont fail if for some reason you have a nagios user on a box, but dont have the directory structure in place such as your current code.  In your example, if the nagios user is there, it assumes that the /usr/local/nagios/etc path exists as a landing pad for your nrpe.cfg file.

Since i dont know what is you install nrpe via a package manager, below is an example using stat to check for the etc directory 

- name: Checking for existing nagios etc directory
  stat:
    path: "/usr/local/nagios/etc"
  register: nag_dir

- name: Copy nrpe.cfg to client
  copy
:
    src
: /etc/ansible/masterfiles/nagios/nrpe.cfg
    dest
: /usr/local/nagios/etc/
    owner: nagios
   
group: nagios
    mode
: 0644

 
when: nag_dir.isdir is defined and nag_dir.isdir
  notify
: reload xinetd


Reply all
Reply to author
Forward
0 new messages