1

I have a bash script I am running in ansible with the script module. However, it needs a specific positional parameter to be specified as a server-name to function in this case as $1. I am attempting to find a way to do that in ansible, but so far have been unsuccessful. I would love to do this with the built in modules, but the selinux one requires installations on the target machine to enable this functionality, and that won't fly on production servers. :(

Just below is how I would run the script without ansible:

bash selinux-disabler-ans.sh <servername>

The above runs just fine on it's own. However, how would I provide in an ansible playbook? Here is what I'm using for that section below:

- name: Remediate/Disable selinux, it will only harm configurations right now, and should be disabled.
script: /opt/selinux-disabler-ans.sh

I can't just hardcode an argument for the script as I need it to be able to change, as this will run across an entire host realm. I'd really love for this to pick up the positional parameter at runtime, possibly using the host it's running on right at the moment, or specified through --limit perhaps? Right now I'm running this script with my own juryrigged automation, but I really want ansible to be able to do it. Anyone have any ideas?

  • Is this script run directly on each host, or does something within the script connect to the given servername? – thrig Sep 14 '16 at 19:14
  • It's run directly on each host. I.E. copied over there and then run with sudo: sudo bash selinux-disabler-ans.sh <servername> It's that <servername> parameter it needs in the form of an actual servername, or I have it coded to just quit. (it couldn't do anything anyways at that point) – Viscosity Sep 14 '16 at 19:28
  • Is the server name a constant, or a variable? If a variable (or a derived attribute) then you can just put in with "{{ ... }}" e.g. script: /opt/selinux-disabler-ans.sh "{{ ansible_hostname }}" or script: /opt/selinux-disabler-ans.sh "{{ ansbible_fqdn }}" – Stephen Harris Sep 14 '16 at 19:40
  • Stephen, yeah looked at your answer and at kalavan's and that's "most" of what I needed. Now because of how I have to run the script I have one final issue. How do I escape quotes like I can in bash itself in an ansible playbook? For instance: local_action: scp -p shared-home-updater.sh me@server1:/tmp/ && ssh -t me@server1 "sudo bash /tmp/shared-home-updater.sh "{{ ansible_hostname }}"; rm -f /tmp/shared-home-updater.sh" The above won't work, quotes from ansible_hostname and in the bash script syntax are interfering with each other. There must be some way to deal with it. – Viscosity Sep 15 '16 at 15:04
1

First I will try to help with ansible. You can use variables, for example you could write something like:

- name: Remediate/Disable selinux, it will only harm configurations right now, and should be disabled.
script: /opt/selinux-disabler-ans.sh "{{ ansible_hostname }}"

Which will use hostname collected from the host during 'setup' phase of a playbook. Take a look on variables that ansible collects for you from remote machine here. Of course, you could set up your own variables. Take a pick on ansible's online documentation - it is great!

Now some remarks. I don't know why your script takes an argument as a server name. Note, that according to documentation script module takes local script (local to the host from which ansible is run), copies it to remote host and runs it there. So, maybe you don't even need this argument :)

And if you are starting with ansible - try writing playbooks using roles from the start. They are not so hard to come by and helps a lot to manage code in more complex environment.

  • Also ansible servername -m setup | grep hostname to inspect what hostname-related things are available. – thrig Sep 14 '16 at 19:48
  • Kalavan, yeah that's helpful, I looked through some of the documentation, but it didn't seem to list that much for the script module. But there are reasons why that script takes an argument, it was in operation before ansible was around, and second, it logs onto a server to do something to another server (which must be specified at runtime) I wish there was a huge list of options for all usages of variables on their site since that's how I'm used to reading things. But I'll spend more time on that page. For now I'll try your solution, thanks Kalavan! – Viscosity Sep 15 '16 at 13:32
  • Shoot, well, that's only HALF the answer it seems. That script needs to be transfered TO a specific server every time to be run, and then on top of that needs the positional parameter input as well during it's own runtime. So now I have to figure out how to have that script always run on a certain host, but not the one ansible is running the task for. Meh I suppose I could always overwrite the file via scp transfers after edits via the current ansible host.. but, there has to be another way to do this. – Viscosity Sep 15 '16 at 13:50
  • Got it, using your answer as a template now that I know what "syntaxes" to look for, I found that delegate_to and the local_action: shorthand syntax in a playbook are able to do the other half of what I need. Between the two of these, I should be able to make things work! – Viscosity Sep 15 '16 at 14:00
  • Stephen, yeah looked at your answer and at kalavan's and that's "most" of what I needed. Now because of how I have to run the script I have one final issue. How do I escape quotes like I can in bash itself in an ansible playbook? For instance: local_action: scp -p shared-home-updater.sh me@server1:/tmp/ && ssh -t me@server1 "sudo bash /tmp/shared-home-updater.sh "{{ ansible_hostname }}"; rm -f /tmp/shared-home-updater.sh" The above won't work, quotes from ansible_hostname and in the bash script syntax are interfering with each other. There must be some way to deal with it. – Viscosity Sep 15 '16 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.