Idempotent unpacking

Try this. Though you'll want 0.8 such that the boolean works as you
would expect.

      - action: get_url ...
        register: get_url_result

      - action: command tar ...
        only_if: "${get_url_result.changed}"

(Adding an easier alternative to the only_if syntax is on the top of
my list for 0.9)

Seems like this request could be grouped with the easy conditionals
stuff earmarked for 0.9; there could be when_file_exists and
when_file_not_exists conditions (or possibly better names like
creates) which would translate to:

   only_if: "True == os.path.isfile('/path/to/created/file')"

In related news, Ben, you can use the above only_if now.

* Michael DeHaan <michael.dehaan at gmail.com> [2012/10/04 08:04]:

Not really. The easy conditional stuff is probably going to compile
down to an only_if, and not result in network tests.

It will get as easy as this though:

when: changed('variable_name')

and maybe even automagic registering of previous commands via a stack, like

when: changed('previous')

or maybe even

when: changed('task_name')

Michael,

Try this. Though you’ll want 0.8 such that the boolean works as you
would expect.

  • action: get_url …
    register: get_url_result

  • action: command tar …
    only_if: “${get_url_result.changed}”

(Adding an easier alternative to the only_if syntax is on the top of
my list for 0.9)

Thanks for the heads up on this (and sorry about the very late reply to your post).

I don’t suppose that this construct would work with my current factoring:

  • name: Fetch Remote Packages
    action: get_url url=$s3/$item dest=/opt/$item
    with_items:
  • $tarball1
  • $tarball2
  • $tarball3
    tags:
  • download

Would I need to break this up to register the boolean, or could I put it into the loop somehow?

Cheers,

Ben

Ben Hood wrote:

Michael,

Try this. Though you'll want 0.8 such that the boolean works as you
would expect.

      - action: get_url ...
        register: get_url_result

      - action: command tar ...
        only_if: "${get_url_result.changed}"

(Adding an easier alternative to the only_if syntax is on the top of
my list for 0.9)

Thanks for the heads up on this (and sorry about the very late reply to
your post).

I don't suppose that this construct would work with my current factoring:

- name: Fetch Remote Packages
  action: get_url url=$s3/$item dest=/opt/$item
  with_items:
    - $tarball1
    - $tarball2
    - $tarball3
  tags:
    - download

Would I need to break this up to register the boolean, or could I put it
into the loop somehow?

Are you wanting to extract each tarball depending on whether or not that
particular one has changed, or do you want to run it if any of them change?

The former would likely be best implemented using a task include file
containing your get_url and unpacking command, and using with_items on the
include, e.g.
  tasks:
  - include: fetch-extract.yml url=$s3/$item name=$item
    with_items:
    - $tarball1
    - $tarball2
and in fetch-extract.yml:
  - name: fetch file
    action: get_url url=$url dest=/opt/$name
    register: fetch_result
  - name: extract file
    action: command tar -xzf /opt/$name
    when_boolean: ${fetch_result.changed}

The latter would be easier, just add register: foo and use ${foo.changed}.
It will be True if any of the items was changed, and False if they were all
unchanged.

Daniel

I do not see a register in your example but generally except for yum and apt combining with items and register would only store the last result

– Michael

Michael DeHaan wrote:

I do not see a register in your example but generally except for yum and
apt combining with items and register would only store the last result

No, register: on a with_items gets a dict with changed being the sum of all
changed, and results being a list of the results for each item.

Daniel

Are you wanting to extract each tarball depending on whether or not that
particular one has changed, or do you want to run it if any of them change?

The former.

The former would likely be best implemented using a task include file
containing your get_url and unpacking command, and using with_items on the
include, e.g.
tasks:
- include: fetch-extract.yml url=$s3/$item name=$item
   with_items:
   - $tarball1
   - $tarball2
and in fetch-extract.yml:
- name: fetch file
   action: get_url url=$url dest=/opt/$name
   register: fetch_result
- name: extract file
   action: command tar -xzf /opt/$name
   when_boolean: ${fetch_result.changed}

OK, I'll give that a go - thanks for the heads up.

BTW how does the register action populate the ${fetch_result.changed} flag? Does the get_url command save a checksum of the last downloaded file?

Cheers,

Ben

I can't remember how all the stuff I wrote works :slight_smile:

Good.

-- Michael