Skip to content

Show correct changed output in check mode #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: updox-2.8.5
Choose a base branch
from

Conversation

estheruary
Copy link

@estheruary estheruary commented Jun 16, 2021

Show correct changed output in check mode

Right now the command module has silly behavior in check mode. If the command is skipped because of a when block or something it shows as skipped. But if the command would actually run it ALSO shows as skipped despite the fact that the module knows that the command would run.

I'm not the only one annoyed by this either. This fixes:

Don't tell me that command doesn't support check mode when supports_check_mode=True.

In addition there is another feature added that I simplifies annoying logic when trying to use check mode to run binaries that are created by the playbook itself -- mocks!

- apt:
    name: awscli

- name: Install credentials
  template:
    src: ...
    dest: ...

- name: Run some command
  command: aws ec2 create-vpc ...
  register: aws_vpc

- name: Do something with the result
  lineinfile:
    dest: /etc/app/app.conf
    line: "vpc={{ (aws_vpc.stdout | to_json).Vpc.VpcId }}"

If you run this in check mode it will fail because Run some command is skipped and so the variable will never be defined. And if you add check_mode: false to that task to fix it it will also fail because the aws command isn't found. So you actually need to have a separate task to check if the aws command exists and use that to skip both tasks but then you lose the information that in reality both of these tasks would be changed which defeats the entire point of check mode.

With these patches we can do

- name: Run some command
  command: aws ec2 create-vpc ...
  args:
    mock:
      stdout: |
        {
          "Vpc": {
            ...
          }
        }
  register: aws_alias

And everything will "just work." In the case of an information gathering command you can do one better but it requires that you check for the command existence.

- name: Check if the aws command exists
  stat:
    path: /bin/aws
  register: aws_cmd

- name: Run some command
  command: aws sts get-caller-name
  args:
    mock:
      stdout: |
        {
         "UserId": "AIDASAMPLEUSERID",
         "Account": "123456789012",
         "Arn": "arn:aws:iam::123456789012:user/DevAdmin"
        }
  register: aws_alias
  check_mode: "{{ not aws_cmd.stat.exists }}"
  changed_when: false

Which will use the mock if the aws command doesn't exist but actually run it if it does.

Right now the command module has silly behavior in check mode. If the
command is skipped because of a when block or something it shows as
skipped. But if the command would actually run it ALSO shows as skipped
despite the fact that the module knows that the command would run.

I'm not the only one annoyed by this either:
Fixes: ansible#14075
       ansible#9508

Don't tell me that command doesn't support check mode when
supports_check_mode=True.
@estheruary estheruary force-pushed the show-command-changed-in-check-mode branch from 812e6fd to 7a34b87 Compare June 16, 2021 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant