Skip to content
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

Fix aws_ssm fails when gathering facts #583

Closed
wants to merge 1 commit into from
Closed

Fix aws_ssm fails when gathering facts #583

wants to merge 1 commit into from

Conversation

richardsonky
Copy link

@richardsonky richardsonky commented May 26, 2021

Depends-On: #621

SUMMARY

Fixes #113

aws_ssm fails when gathering facts. This fix incorporates some code submitted by others, but also works on windows which others haven't done yet.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

aws_ssm

ADDITIONAL INFORMATION

Fixes aws_ssm connection plugin when gathering facts. Uses code a few others submitted but also fixes windows.
Changed logic in the exec_command, wrap_command, and post_process functions that help to unify the output from windows and linux to make it easier to locate return values of the commands.

We have been running this in our environment for over a month and it is working without issue against win2016, win2019, centos7, suse15, and rhel7 ec2 instances.

@ansibullbot ansibullbot added bug This issue/PR relates to a bug community_review connection connection plugin needs_triage new_contributor Help guide this first time contributor plugins plugin (any type) labels May 26, 2021
@tremble
Copy link
Contributor

tremble commented May 27, 2021

Thanks for taking the time to submit this PR.

I've not got the time to review it right now, however the sanity tests are throwing some errors that should be fixed:

https://dashboard.zuul.ansible.com/t/ansible/build/c060c97822fe445fabfd681f5dfd6ca3/console

ERROR: Found 2 pep8 issue(s) which need to be resolved:
ERROR: plugins/connection/aws_ssm.py:447:14: E121: continuation line under-indented for hanging indent
ERROR: plugins/connection/aws_ssm.py:457:70: E261: at least two spaces before inline comment

@richardsonky
Copy link
Author

I had to revert the linux cmd f(printf ...) statements that were suggested by others, it wasn't going to pass the python2.6 checks. I went back to the old cmd method and modified it with ';'s so it works as expected and passes the python2.6 tests.

I also modified a few of the logging lines while i was in here to have it log in vvvv so that it doesn't show up in debug logs, only in connection. I've tested in each OS we support and it is working as expected.

@KFoxder
Copy link

KFoxder commented Jun 8, 2021

@tremble Should this be assigned to someone for review?

@gravesm gravesm requested review from jillr and goneri June 29, 2021 20:04
@goneri
Copy link
Member

goneri commented Jun 29, 2021

I had to revert the linux cmd f(printf ...) statements that were suggested by others, it wasn't going to pass the python2.6 checks. I went back to the old cmd method and modified it with ';'s so it works as expected and passes the python2.6 tests.

I also modified a few of the logging lines while i was in here to have it log in vvvv so that it doesn't show up in debug logs, only in connection. I've tested in each OS we support and it is working as expected.

For the record, we now only support Py38+. It's a pretty recent change thus.

@goneri
Copy link
Member

goneri commented Jun 29, 2021

Can you quickly explain how you do you tests? Maybe you've got a test playbook?

@richardsonky
Copy link
Author

richardsonky commented Jun 29, 2021

Our tests are not really official but seem to work ok. I have a CF template to build an ec2 instance for each OS we support at our company. We have a system in place to kick off our playbooks using cloudwatch events, step functions, and fargate. It will run through our standard playbook to configure the ec2. We visually look at ansible logs to see if the playbooks ran correctly. I've found a few bugs using this test so it seems work, though not very automated and probably doesn't test every possibility.

@wilinger
Copy link

wilinger commented Jul 1, 2021

Did you also test on Ubuntu 20.04? Works on Amazon Linux but not Ubuntu for me

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ValueError: invalid literal for int() with base 10: "echo $'\\n'$?" fatal: [test_ubuntu1]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}

@richardsonky
Copy link
Author

I did not test ubuntu, it's not one of the OSs we support, however i think i found the problem. There were two issues. The first is it did not detect the proper command output for ubuntu, so i modified the check to ensure that it finds the right output. Once that worked it then did not like the old method of wrapping the command, but it does work with the new printf statements. I put the printf statements back and ubuntu is working. I don't have an extended playbook for ubuntu, i pretty much just did a quick gather facts and it works. I also tested ubuntu 18.04 and it works now as well.

I redid my tests for each OS we support and they work still. So if the automated testing doesn't check old python versions anymore then it should pass those checks.

Let me know if that works ok for you now.

@richardsonky
Copy link
Author

Looks like it's still failing on those older versions of python that do not support f strings. Do we need to find a solution that works with all those older versions still?

@wilinger
Copy link

wilinger commented Jul 1, 2021

I did not test ubuntu, it's not one of the OSs we support, however i think i found the problem. There were two issues. The first is it did not detect the proper command output for ubuntu, so i modified the check to ensure that it finds the right output. Once that worked it then did not like the old method of wrapping the command, but it does work with the new printf statements. I put the printf statements back and ubuntu is working. I don't have an extended playbook for ubuntu, i pretty much just did a quick gather facts and it works. I also tested ubuntu 18.04 and it works now as well.

I redid my tests for each OS we support and they work still. So if the automated testing doesn't check old python versions anymore then it should pass those checks.

Let me know if that works ok for you now.

The change seemed to have broke something because it's not working for both Ubuntu and Amazon Linux now. Counts down 'EXEC remaining' and loops. What seemed to work for me for both Amazon Linux and Ubuntu was the patched code posted on #113

<i-01a11b1adbdd11111> EXEC stdout line: 
<i-01a11b1adbdd11111> EXEC stdout line: Starting session with SessionId: botocore-session-11111111111-0f1db111f64ee111c
<i-01a11b1adbdd11111> EXEC remaining: 60
<i-01a11b1adbdd11111> EXEC remaining: 59
<i-01a11b1adbdd11111> EXEC stdout line: $ stty -echo
<i-01a11b1adbdd11111> EXEC stdout line: PS1=''
<i-01a11b1adbdd11111> EXEC stdout line: printf '%s\n' 'VKbBEZmnukenWpSHnhjpeLsytB';
<i-01a11b1adbdd11111> EXEC stdout line: echo | ( umask 77 && mkdir -p "` echo /tmp `"&& mkdir "` echo /tmp/ansible-tmp-1625158982.2448153-792605-17192929165503 `" && echo ansible-tmp-1625158982.2448153-792605-17192929165111="` echo /tmp/ansible-tmp-1625158982.2448153-792605-17192929165503 `" );
<i-01a11b1adbdd11111> EXEC stdout line: printf '\n%s\n%s\n' "$?" 'iWzKicCLGXsTVUgEgQFTjzkFtq';
<i-01a11b1adbdd11111> EXEC stdout line: $ VKbBEZmnukenWpSHnhjpeLsysa
<i-01a11b1adbdd11111> EXEC stdout line: ansible-tmp-1625158982.2448153-792605-17192929165111=/tmp/ansible-tmp-1625158982.2448153-792605-17192929165111
<i-01a11b1adbdd11111> EXEC stdout line: 
<i-01a11b1adbdd11111> EXEC stdout line: 0
<i-01a11b1adbdd11111> EXEC stdout line: iWzKicCLGXsTVUgEgQFTjzkFtq
<i-01a11b1adbdd11111> EXEC remaining: 58
<i-01a11b1adbdd11111> EXEC remaining: 57
<i-01a11b1adbdd11111> EXEC remaining: 56
<i-01a11b1adbdd11111> EXEC remaining: 55
<i-01a11b1adbdd11111> EXEC remaining: 54

@richardsonky
Copy link
Author

Sorry about that, i had a typo when i transferred the edits to this repo. Please test again and let me know if that worked. I've tested on ansible linux and ubuntu and it appears to work ok for me.

@wilinger
Copy link

wilinger commented Jul 2, 2021

@richardsonky It's working now on both Ubuntu and Amazon Linux now. Thanks for the fix!

@richardsonky
Copy link
Author

I had to revert the linux cmd f(printf ...) statements that were suggested by others, it wasn't going to pass the python2.6 checks. I went back to the old cmd method and modified it with ';'s so it works as expected and passes the python2.6 tests.
I also modified a few of the logging lines while i was in here to have it log in vvvv so that it doesn't show up in debug logs, only in connection. I've tested in each OS we support and it is working as expected.

For the record, we now only support Py38+. It's a pretty recent change thus.

@goneri
Looks like the checks for older python version is still a problem since they don't support f strings. Do we need to fix the code to work with those older versions?

@goneri
Copy link
Member

goneri commented Jul 2, 2021

I had to revert the linux cmd f(printf ...) statements that were suggested by others, it wasn't going to pass the python2.6 checks. I went back to the old cmd method and modified it with ';'s so it works as expected and passes the python2.6 tests.
I also modified a few of the logging lines while i was in here to have it log in vvvv so that it doesn't show up in debug logs, only in connection. I've tested in each OS we support and it is working as expected.

For the record, we now only support Py38+. It's a pretty recent change thus.

@goneri
Looks like the checks for older python version is still a problem since they don't support f strings. Do we need to fix the code to work with those older versions?

I mean, we support py38+ on the controller side. We continue to support py2 on the remote nodes.

@goneri
Copy link
Member

goneri commented Jul 2, 2021

Ansible-test still checks with Python 2.6 to 3.7. To keep it quiet, we use ignore files to disable some checks: https://github.com/ansible-collections/community.aws/tree/main/tests/sanity

@goneri
Copy link
Member

goneri commented Jul 2, 2021

recheck

@goneri
Copy link
Member

goneri commented Jul 2, 2021

I've just pushed #621 to extend these filters.

@jillr
Copy link
Collaborator

jillr commented Jul 7, 2021

For the record, we now only support Py38+. It's a pretty recent change thus.

We support 3.6+ for the AWS collections, but we definitely don't need to be testing on 2.x any longer.

@damoun
Copy link

damoun commented Sep 30, 2021

Is there any news about this PR ? Looks like #621 is not needed anymore.

@vranyes
Copy link
Contributor

vranyes commented Dec 13, 2021

This PR fixes this issue for me as well - any chance this can be merged?

@kloveday2
Copy link

This PR also fixes the issue for me. What left to be done to get it merged?

@softwarefactory-project-zuul
Copy link
Contributor

Merge Failed.

This change or one of its cross-repo dependencies was unable to be automatically merged with the current state of its repository. Please rebase the change and upload a new patchset.

2 similar comments
@softwarefactory-project-zuul
Copy link
Contributor

Merge Failed.

This change or one of its cross-repo dependencies was unable to be automatically merged with the current state of its repository. Please rebase the change and upload a new patchset.

@softwarefactory-project-zuul
Copy link
Contributor

Merge Failed.

This change or one of its cross-repo dependencies was unable to be automatically merged with the current state of its repository. Please rebase the change and upload a new patchset.

remove unnecessary changes

add fragment

fix changelog

fix pep8 issues

fix indent

modification to pass python2.6 tests

update to fix ubuntu. need to use new printf method

fix typo in condition
@ansibullbot
Copy link

@richardsonky This PR was evaluated as a potentially problematic PR for the following reasons:

  • More than 50 changed files.

Such PR can only be merged by human. Contact a Core team member to review this PR on IRC: #ansible-devel on irc.freenode.net

click here for bot help

@softwarefactory-project-zuul
Copy link
Contributor

Merge Failed.

This change or one of its cross-repo dependencies was unable to be automatically merged with the current state of its repository. Please rebase the change and upload a new patchset.

@ansibullbot ansibullbot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Feb 23, 2022
@richardsonky
Copy link
Author

not sure what i'm doing, so closing and will reopen a new one

@Leon-Africa
Copy link

Reference: #948 (comment)

abikouo pushed a commit to abikouo/community.aws that referenced this pull request Oct 24, 2023
aws_ec2 fix hostnames

SUMMARY

aws_ec2- fix hostnames bugs
Should fix ansible-collections#582 and ansible-collections#583

Fix returned hosts when

hostnames:
- tag:Tag1,Tag2


Fix returned hosts when

hostnames:
- tag:Tag1
- tag:Tag2


Fix returned hosts when

hostnames:
- tag:Tag1=Test1,Tag2=Test2

Given and EC2 instance with the following tags
tags:
  Tag1: Test1
  Tag2: Test2

Instead of only returning
"aws_ec2": {
    "hosts": [
        "Test1"
    ]
},
"tag_Name_instance_02": {
    "hosts": [
        "Test1"
    ]
},
"tag_Tag1_Test1": {
    "hosts": [
        "Test1"
    ]
},
"tag_Tag2_Test2": {
    "hosts": [
        "Test1"
    ]
}

It returns now
{
"_meta": {
   "hostvars": {
         "Test1": {
                "ami_launch_index": 0,
                "ansible_host": "10.210.0.101",
                   ...},
         "Test2": {
                 "ami_launch_index": 0,
                 "ansible_host": "10.210.0.101",
                   ...},
    },
    "all": {
        "children": [
            "aws_ec2",
            "tag_Name_instance_02",
            "tag_Tag1_Test1",
            "tag_Tag2_Test2",
            "ungrouped"
        ]
    },
    "aws_ec2": {
        "hosts": [
            "Test1",
            "Test2"
        ]
    },
    "tag_Name_instance_02": {
        "hosts": [
            "Test1",
            "Test2"
        ]
    },
    "tag_Tag1_Test1": {
        "hosts": [
            "Test1",
            "Test2"
        ]
    },
    "tag_Tag2_Test2": {
        "hosts": [
            "Test1",
            "Test2"
        ]
    }
}




 
ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

aws_ec2
ADDITIONAL INFORMATION

Reviewed-by: Mark Chappell <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: Markus Bergholz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug community_review connection connection plugin has_issue needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR new_contributor Help guide this first time contributor plugins plugin (any type)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ssm connection plugin fails at gathering facts