How to pass a node attribute as variable into job that runs ansible playbook?

1,578 views
Skip to first unread message

refrie...@gmail.com

unread,
Feb 11, 2019, 4:38:37 PM2/11/19
to rundeck-discuss
Is it possible to pass the value of a given nodes attribute into a rundeck job that calls an Ansible playbook? I am using the rundeck-ansible-plugin.

I have node with an attribute defined of "software:type = ios"

The playbook will look something like this, where the line "ansible_network_os" defined under vars needs to reference the given nodes attribute - and should expand to be the value "ios" in this case.


---
- name: Cisco IOS - Login Banner - Remove
  connection
: network_cli
  gather_facts
: false
  vars
:
    ansible_network_os
: $software:type
  hosts
: all
  tasks
:
   
- ios_banner:
        banner
: login
        state
: absent


Searching around it appears that node attributes are strictly used for filtering target nodes to run jobs against.

Ultimately, I am trying to solve a situation where I need a job/playbook that can dynamically reference the OS type of the target node, so that my jobs and playbooks can be more re-usable. Eventually, the single playbook example above could be expanded to run against every device type supported (software type) and called within a single Rundeck job.

I know the OS type ahead of time, and they can either be present in the Ansible inventory file, or a Rundeck resource model file. I just can't figure out how to then pass that value into the job/playbook itself.

I can create Ansible groups <> Rundeck tags that group all devices of a certain type together. But I don't see how I can make use of tag values inside a job either.

refrie...@gmail.com

unread,
Feb 11, 2019, 6:32:36 PM2/11/19
to rundeck-discuss
I made a little bit of headway here, but not quite past the finish line.

I would need to use a resourcexml file for my inventory, instead of an Ansible inventory file, which provides great control over setting attributes and tags.

I could then reference node context variables using environment variables via the Ansible lookup function.

But I can't get it working when calling an Ansible playbook, only when calling an in-line script.

Using an in-line script that is dispatched locally that does something simple like "echo $RD_NODE_NAME" in the debug output I can see all of the node context variables.

Calling that from an inline playbook doesn't return any value.

Here is the playbook I am working with:

---
- name: debug variable
  gather_facts
: false
  hosts
: switch01.domain.com
  tasks
:
   
- debug: msg="{{ lookup('env','RD_NODE_NAME') }}"


Running that playbook I get an empty debug message. If I replace RD_NODE_NAME with just HOME, the variable outputs. If I add a new env variable for the Rundeck user on the shell, no output.

I feel like I am missing something very simple here. Is it because the ansible-plugin calls the ansible-playbook python script using python directly, instead of calling it from a shell?

rac...@rundeck.com

unread,
Feb 12, 2019, 4:36:04 PM2/12/19
to rundeck-discuss
Hi!

For that you need to define in "Extra Variables" your variable like: myserver: ${node.name} section (at the moment of creating or editing your job) and call it on your Playbook like:

{{ nodename}}

I did make an example for that (using inline ansible pplaybook):


<joblist>
<job>
<defaultTab>summary</defaultTab>
<description></description>
<dispatch>
<excludePrecedence>true</excludePrecedence>
<keepgoing>false</keepgoing>
<rankOrder>ascending</rankOrder>
<successOnEmptyNodeFilter>false</successOnEmptyNodeFilter>
<threadcount>1</threadcount>
</dispatch>
<executionEnabled>true</executionEnabled>
<id>d06fddc1-48c7-43c3-ab54-d683a10bd678</id>
<loglevel>INFO</loglevel>
<name>PrintVARS</name>
<nodeFilterEditable>false</nodeFilterEditable>
<nodefilters>
<filter>localhost</filter>
</nodefilters>
<nodesSelectedByDefault>true</nodesSelectedByDefault>
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<node-step-plugin type='com.batix.rundeck.plugins.AnsiblePlaybookInlineWorkflowNodeStep'>
<configuration>
<entry key='ansible-base-dir-path' value='/etc/ansible' />
<entry key='ansible-become' value='true' />
<entry key='ansible-become-method' value='su' />
<entry key='ansible-become-password-storage-path' value='keys/passwords/localhost' />
<entry key='ansible-become-user' value='root' />
<entry key='ansible-extra-vars' value='nodename: ${node.name}' />
<entry key='ansible-playbook-inline' value='---&#10;- name: This is a hello-world example&#10; hosts: all&#10; tasks:&#10; - name: Create a file called &apos;/home/vagrant/testfile.txt&apos;.&#10; copy: &#10; content: "{{ nodename }}" &#10; dest: /home/vagrant/testfile.txt&#10;' />
<entry key='ansible-ssh-auth-type' value='password' />
<entry key='ansible-ssh-password-storage-path' value='keys/passwords/localhost' />
<entry key='ansible-ssh-user' value='vagrant' />
</configuration>
</node-step-plugin>
</command>
</sequence>
<uuid>d06fddc1-48c7-43c3-ab54-d683a10bd678</uuid>
</job>
</joblist>

Hope it helps!
Reply all
Reply to author
Forward
0 new messages