Ansible: Running Commands in Dry-Run Mode in Check Mode

Say you have an Ansible playbook that invokes a command. Normally, that command executes when you run ansible normally, and doesn’t execute at all when you run ansible in check mode.

But a lot of commands, like rsync have a -n or --dry-run argument that shows what would be done, without actually making any changes. So it would be nice to combine the two.

Let’s start with a simple playbook that copies some files with rsync:

- name: Copy files
  tasks:
    - name: rsync the files
      command: >-
        rsync
        -avi
        /tmp/source/
        /tmp/destination/
  hosts: localhost
  become: no
  gather_facts: no

When you execute this playboook with ansible-playbook foo.yml rsync runs, and when you run in check mode, with ansible-playbook -C foo.yml, rsync doesn’t run.

This is inconvenient, because we’d like to see what rsync would have done before we commit to doing it. So let’s force it to run even in check mode, with check_mode: no, but also run rsync in dry-run mode, so we don’t make changes while we’re still debugging the playbook:

- name: Copy files
  tasks:
    - name: rsync the files
      command: >-
        rsync
        --dry-run
        -avi
        /tmp/source/
        /tmp/destination/
      check_mode: no
  hosts: localhost
  become: no
  gather_facts: no

Now we just need to remember to remove the --dry-run argument when we’re ready to run it for real. And turn it back on again when we need to debug the playbook.

Or we could do the smart thing, and try to add that argument only when we’re running Ansible in check mode. Thankfully, there’s a variable for that: ansible_check_mode, so we can set the argument dynamically:

- name: Copy files
  tasks:
    - name: rsync the files
      command: >-
        rsync
        {{ '--dry-run' if ansible_check_mode else '' }}
        -avi
        /tmp/source/
        /tmp/destination/
      check_mode: no
  hosts: localhost
  become: no
  gather_facts: no

You can check that this works with ansible-playbook -v -C foo.yml and ansible-playbook -v foo.yml.

Advertisements
Posted in Geek, Things I've Learned | Tagged , | Leave a comment

Pseudo-Numeric Identifiers

Let’s say you’re a programmer, and your application uses Library of Congress Control Numbers for books, e.g., 2001012345, or ZIP codes, like 90210. What data types would you use to represent them? Or maybe something like the Dewey Decimal System, which uses 320 to classify a book as Political Science, 320.5 for Political Theory, and 320.973 for “Political institutions and public administration (United States)”?

If you said “integer”, “floating point”, or any kind of numeric type, then clearly you weren’t paying attention during the title.

The correct answer was “string” (or some kind of array of tokens), because although these entities consist of digits, they’re not numbers: they’re identifiers, same as “root” or “Jane Smith”. You can assign them, sort them, group them by common features, but you can’t meaningfully add or multiply them together. If you’re old enough, you may remember the TV series The Prisoner or Get Smart, where characters, most of them secret agents, refer to each other by their code numbers all the time; when agents 86 and 99 team up, they don’t become agent 185 all of a sudden.

If you keep in mind this distinction between numbers, which represent quantities, and strings that merely look like numbers because they happen to consist entirely of integers, you can save yourself a lot of grief. For instance, when your manager decides to store the phone number 18003569377 as “1-800-FLOWERS”, dashes and all. Or when you need to store a foreign phone number and have to put a plus sign in front of the country code.

Posted in Hacking | Tagged | 2 Comments

Removing Magic

So this was one of those real-life mysteries.

I like crossword puzzles. And in particular, I like indie crossword puzzles, because they tend to be more inventive and less censored than ones that run in newspapers. So I follow several crossword designers on Twitter.

Yesterday, one of them mentioned that people were having a problem with his latest puzzle. I tried downloading it on my iPad, and yeah, it wouldn’t open in Across Lite. Other people were saying that their computers thought the file was in PostScript format. I dumped the HTTP header with

lynx -head -dump http://url.to/crossword.puz

and found the header

Content-type: application/postscript

which was definitely wrong for a .puz file. What’s more, other .puz files in the same directory were showing up as

Content-type: application/octet-stream

as they should.

I mentioned all this to the designer, which led to us chatting back and forth to see what the problem was. And eventually I had the proverbial aha moment.

.puz files begin with a two-byte checksum. In this particular case, they turned out to be 0x25 and 0x21. Or, in ASCII, “%!“. And as it turns out, PostScript files begin with “%!“, according to Unix’s magic file.

So evidently what happened was: the hosting server didn’t have a default type for files ending in .puz. Not terribly surprising, since that’s not really a widely-used format. So since it didn’t recognize the filename extension, it did the next-best thing and looked at the first few bytes of the file (probably with file or something equivalent) to see if it could make an educated guess. It saw the checksum as “%!” and decided it was a PostScript file.

The obvious fix was to change something about the file: rewrite a clue, add a note, change the copyright statement, anything to change the contents of the file, and thus the checksum.

The more permanent solution was to add a .htaccess file to the puzzle file directory, with

AddType application/octet-stream .puz

assuming that the hosting provider used Apache or something compatible.

This didn’t take immediately; I think the provider cached this metadata for a few hours. But eventually things cleared up.

I’m not sure what the lesson is, here. “Don’t use two-byte checksums at offset 0”, maybe?

Posted in Geek | Tagged , , , , | 4 Comments

Do You Even Science, Frater?

The other day, I went to a Thomistic Society talk about Aquinas’s views on the Problem of Evil and other topics. At one point, the presenter casually mentioned that humans engage in self-destructive behavior, like alcoholism, self-mutilation, drug addiction, etc., while non-human animals don’t.

That made my [citation needed] sense tingle, so I looked around. Among other things, I found Animal models of self-destructive behavior and suicide:

Research on nonhuman primates has demonstrated that self-mutilation is a common reaction to extreme disruptions of parental caretaking in other mammalian species as well. For example, isolated young rhesus monkeys engage in self-biting and head slapping and banging (21). Analgesia is also common in self-destructive animals.

Or this non-scholarly page about the effects of drugs, including addiction, in animals such as horses, goats, and even bees.

So apparently this speaker simply wasn’t aware of self-destructive behavior in non-human animals. I don’t remember what her point was, so it might have been a minor thing, but still, it wasn’t true.


But this brought to mind the previous Thomistic Institute talk I went to: there, the presenter casually mentioned that humans engage in abstract reasoning, while animals don’t.

Again, this didn’t seem quite right. This study from 2007 involved teaching dogs to push a button when shown a set of pictures of dogs, and another button when shown a set of pictures of landscapes.

Interestingly, presentation of pictures providing contradictive information (novel dog pictures mounted on familiar landscape pictures) did not disrupt performance, which suggests that the dogs made use of a category-based response rule with classification being coupled to category-relevant features (of the dog) rather than to item-specific features (of the background).

Or this paper, entitled simply Concept Learning in Animals, whose abstract says:

We suggest that several of the major varieties of conceptual classes claimed to be uniquely human are also exhibited by nonhuman animals. We present evidence for the formation of several sorts of conceptual stimulus classes by nonhuman animals: perceptual classes involving classification according to the shared attributes of objects, associative classes or functional equivalences in which stimuli form a class based on common associations, relational classes, in which the conceptual relationship between or among stimuli defines the class, and relations between relations, in which the conceptual (analogical) relationship is defined by the relation between classes of stimuli. We conclude that not only are nonhuman animals capable of acquiring a wide variety of concepts, but that the underlying processes that determine concept learning are also likely to be quite similar.

No one will deny that humans can perform mental feats that non-human animals can’t, as far as we can tell. Other animals can’t play chess, prove mathematical theorems, or form complex sentences, as far as I know. But at the same time, the issue isn’t a black-and-white “humans can reason abstractly and animals can’t.”


Lastly, I’ve written at length about Thomist Edward Feser, and his ignorance of science from Newton on up.

Individually, each of these mistakes are just that: mistakes. Or ignorance: philosophers can’t be expected to be masters of nuclear physics or animal cognition. Or simplifications that gloss over a complex idea in order to make a broader point.

But collectively, I do see a pattern of Thomists being wrong on matters of science in a thousand small ways. That suggests that either they don’t bother checking whether their beliefs are true, where possible, and correct their errors, or else they have other beliefs that lead them to erroneous conclusions. And either way, if I can’t trust them on the small stuff, why should I believe them on the big stuff?

 

Posted in Religion | Tagged , , , , , , | 2 Comments

Democracy Is Not Football

One of the many things that annoyed me in the wake of Trump’s upset victory was that for so many people on the winning side, this boiled down to “delicious librul tears“. And even a lot of Trump’s executive actions have been less about advancing his own plan, as about tearing down Obama’s legacy. We’ve also seen this with the ACA, where the GOP has been quite intent on repealing Obamacare, but has no idea what to replace it with, or how to get health care to everyone.

On the other side, I see people like the organizers of the March for Our Lives, and before them a thousand newly-hatched activists, whom Trump and the GOP awakened to the fact that government isn’t Them; it’s Us. You don’t have to choose among the two candidates that the major parties provide; you can run for office yourself. And if you can’t run and can’t vote, you can still write your representatives, and raise a fuss and bloody well make people care about the issues that you care about.

This is of a piece with the rise of prayer-shaming: a lot of the response to shootings like Parkland has been that offering “thoughts and prayers” is the same as not doing anything at all, and by now you are expected to know this. The young generation is the least-religious one yet in recent American history, and they know that praying doesn’t fix problems. Sitting down and fixing problems fixes problems.

This may turn out to be the silver lining of the Trump era: that so much stuff broke, that it woke people up to the fact that democracy isn’t a spectator sport.

(Originally started as a comment at chez Hemant.)

Posted in Politics | Tagged , , | Leave a comment

Hell Is a School, Apparently

By now, you’ve all seen this T-shirt, which began circulating approximately several years before 17 people were killed at Stoneman Douglas High School, in Florida:

T-shirt: "Dear God, why do you allow violence in schools?" "I'm not allowed in schools. -- God"

As many people have pointed out, the implication is that, against all theology, God—or at least the God of sanctimonious T-shirt wearers—is not omnipresent. That a simple legislative measure is sufficient to banish God from a place.

But if you point this out, or indeed dare to make fun of a religious idea, institution, or person in a public forum, you’ll see veiled threats of hell:

godly comments

I’ve found that Christians far prefer veiled threats over overt ones. I think they’re uncomfortable with their own beliefs, and prefer to skate around them. Or maybe what they believe deep down isn’t what they believe in public. At any rate, the usual response I get is that Hell, in the afterlife, is simply an absence of God.

So, it’s like a school, I guess.

I’ve attended public school, and I still go there, for special events. I’ve never seen a pitchfork or smelled a lake of molten sulfur (and if there were, I’m sure there’d be railings so you didn’t fall in accidentally).

But really, if The Bad Afterlife is like being in an American public school, then sure, I’ll take that. It sounds an awful lot like ordinary life right now.

Posted in :-), Politics, Religion, Uncategorized | Tagged , , | 1 Comment

City Mileage

I’ve known forever that city mileage for cars is worse than highway mileage, but I never knew why. But it’s a bit like riding a bike.

When you ride a bike, you have to put in a lot of work at the beginning, getting up to speed. And after that, you can mostly coast. Assuming you’re on flat ground, you have to pedal a bit because friction is slowing you down, but it’s nowhere near how hard you had to work getting up to speed.

And then you stop at a red light, and you throw away all the energy you had, so that when the light turns green, you have to put in another burst of work getting up to speed. And, of course, in the city you’re stopping like this all the time. Every few blocks, you throw away all your accumulated energy, and have to start over. This applies to cars the same way as to bikes, except that on your bike, you immediately sense this in your legs, not at the end of the week when the tank is empty.

So it’s not that highways somehow enhance your mileage. Rather, it’s cities that make for wasteful driving. It’s a bit embarrassing that it took me this long to figure it out, but better late than never.

Posted in Science, Things I've Learned | Tagged , , , | 4 Comments