Hey Chris,
I typically use modules to do targeted tasks instead of trying to batch multiple operations into a single task, but in the interest of educating myself, I took a swing at putting your example into a single task. The following should give you some ideas.
- name: a play
hosts: all
connection: local
tasks:
-
name: TextExpander Settings Directory
file:
path: “/tmp/Application\ Support/TextExpander/”
state: “directory”
-
name: TextExpander Settings Symlink
file:
src: “/tmp/Dropbox/TextExpander/Settings.textexpander”
dest: “/tmp/Application\ Support/TextExpander/Settings.textexpander”
state: “link”
force: “yes”
-
name: a play
hosts: all
connection: local
tasks:
- name: TextExpander Settings
file:
path: “{{ item.path|default(omit) }}”
state: “{{ item.state }}”
src: “{{ item.src|default(omit) }}”
dest: “{{ item.dest|default(omit) }}”
force: “{{ item.force|default(omit) }}”
with_items:
- path: “/tmp/Application\ Support/TextExpander2/”
state: “directory”
- src: “/tmp/Dropbox/TextExpander2/Settings.textexpander”
dest: “/tmp/Application\ Support/TextExpander2/Settings.textexpander”
state: “link”
force: “yes”
The first play is just a mock-up of the example you provided.
The second play is the same example (I just changed the directory name to “TextExpander2”) with a single task used with a loop and jinja’s omit filter. I’m looping over a list of dictionaries and using omit on values that I know may not exist. State always exists.
Now, does this make it look cleaner? Well, that’s subjective so I’ll let you decide. You could take the content in the with_items list and put it in a vars file to make the play itself look smaller (and allow your list of things to grow nearly unbounded).
When I run it on my box over here it goes off without a hitch and gives me (what I think) are the results you’re looking for
SEA-ML-RUPP1:ui trupp$ ansible-playbook -i localhost, ok.yaml
PLAY [a play] *****************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [TextExpander Settings Directory] ***************************************
changed: [localhost]
TASK: [TextExpander Settings Symlink] *****************************************
changed: [localhost]
PLAY [a play] *****************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [TextExpander Settings] *************************************************
changed: [localhost] => (item={‘path’: ‘/tmp/Application Support/TextExpander2/’, ‘state’: ‘directory’})
changed: [localhost] => (item={‘dest’: ‘/tmp/Application Support/TextExpander2/Settings.textexpander’, ‘src’: ‘/tmp/Dropbox/TextExpander2/Settings.textexpander’, ‘state’: ‘link’, ‘force’: ‘yes’})
PLAY RECAP ********************************************************************
localhost : ok=5 changed=3 unreachable=0 failed=0
SEA-ML-RUPP1:ui trupp$
Hope that helps.
-tim