modifying am IAM policy

95 views
Skip to first unread message

Tony Wong

unread,
Feb 9, 2023, 2:46:09 PM2/9/23
to Ansible Project
I am trying to add or modify an iam policy with below. it ran but did not modify anything

any idea?

---
- name: test
hosts: localhost
tasks:
- name: Create IAM Managed Policy
community.aws.iam_managed_policy:
policy_name: "PrismaCloud-IAM-ReadOnly-Policy"
policy:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
"Action": "appstream:DescribeStacks"
Resource: "*"
make_default: false
state: present


Rowe, Walter P. (Fed)

unread,
Feb 9, 2023, 2:47:56 PM2/9/23
to ansible...@googlegroups.com
Does your AWS user ID used by the task have rights to modify IAM policies?

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/06b09dc9-215a-44a9-b9f0-ec4f7732f775n%40googlegroups.com.

Tony Wong

unread,
Feb 9, 2023, 2:50:02 PM2/9/23
to ansible...@googlegroups.com
yes it does

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/WZzXL_z_teA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0A40E414-A094-499F-A48F-750F8F8072C5%40nist.gov.

Tony Wong

unread,
Feb 9, 2023, 4:30:20 PM2/9/23
to ansible...@googlegroups.com
ok I tried doing it this way and it worked but wiped out my existing policy. any idea how to append instead of replace?

---
- name: test
hosts: localhost
tasks:
- name: Create IAM Managed Policy
amazon.aws.iam_policy:
iam_type: role
iam_name: "aws_test_role"
policy_name: "PrismaCloud-IAM-ReadOnly-Policy"
policy_json:
Version: "2012-10-17"
Statement:
- Action: ["appstream:DescribeStacks"]
Effect: "Allow"
Resource: "*"
state: present

Tony Wong

unread,
Feb 9, 2023, 5:02:45 PM2/9/23
to ansible...@googlegroups.com
trying my loop but its only putting in one value . any idea?

---
- name: test
hosts: localhost
tasks:
- name: Create IAM Managed Policy
amazon.aws.iam_policy:
iam_type: role
iam_name: "aws_test_role"
policy_name: "PrismaCloud-IAM-ReadOnly-Policy"
policy_json:
Version: "2012-10-17"
Statement:
- Action: "{{ item }}"
Effect: "Allow"
Resource: "*"
state: present
loop:
- acm-pca:ListTags
- acm-pca:GetPolicy
- acm-pca:GetPolicy

Todd Lewis

unread,
Feb 9, 2023, 5:52:37 PM2/9/23
to Ansible Project
Here's an idea: Register the result, and show us the output from `ansible-playbook -vv`.
Based on what you said before about it replacing rather than adding to, I'm going to guess you're only getting the last value. (?)

Tony Wong

unread,
Feb 9, 2023, 6:34:36 PM2/9/23
to ansible...@googlegroups.com
Yep only last value 

Dick Visser

unread,
Feb 10, 2023, 7:41:33 AM2/10/23
to ansible...@googlegroups.com
You could use an inline template to loop over the list of actions, for example:

---

- name: test

  hosts: localhost

  tasks:

  - name: Create IAM Managed Policy

    amazon.aws.iam_policy:

      iam_type: role

      iam_name: "aws_test_role"

      policy_name: "PrismaCloud-IAM-ReadOnly-Policy"

      policy_json: |

        Version: "2012-10-17"

        Statement:

        {% for action in actions %}

          - Action: {{ action }}

            Effect: Allow

            Resource: "*"

        {% endfor %}

      state: present

    vars:

      actions:

        - acm-pca:ListTags

        - acm-pca:GetPolicy

        - acm-pca:GetPolicy



Rowe, Walter P. (Fed)

unread,
Feb 10, 2023, 7:56:24 AM2/10/23
to ansible...@googlegroups.com
Has this discussion gotten away from ansible and drifted into an AWS question?


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Rowe, Walter P. (Fed)

unread,
Feb 10, 2023, 7:56:57 AM2/10/23
to ansible...@googlegroups.com
Perhaps you need to query the current state of the IAM role, modify it, and re-apply it so you are adding to the existing policy?


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Tony Wong

unread,
Feb 10, 2023, 3:18:42 PM2/10/23
to ansible...@googlegroups.com
hi

I got 

 "msg": "Failed to decode the policy as valid JSON: Expecting value: line 1 column 1 (char 0)"



Tony Wong

unread,
Feb 13, 2023, 9:55:47 AM2/13/23
to ansible...@googlegroups.com
any idea on this?

fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "access_key": null,
            "aws_ca_bundle": null,
            "aws_config": null,
            "debug_botocore_endpoint_logs": false,
            "endpoint_url": null,
            "iam_name": "aws_test_role",
            "iam_type": "role",
            "policy_json": "Version: \"2012-10-17\"\nStatement:\n  - Action: acm-pca:ListTags\n    Effect: Allow\n    Resource: \"*\"\n  - Action: acm-pca:GetPolicy\n    Effect: Allow\n    Resource: \"*\"\n  - Action: acm-pca:GetPolicy\n    Effect: Allow\n    Resource: \"*\"",
            "policy_name": "PrismaCloud-IAM-ReadOnly-Policy",
            "profile": null,
            "region": null,
            "secret_key": null,
            "session_token": null,
            "skip_duplicates": false,
            "state": "present",
            "validate_certs": true
        }
    },

    "msg": "Failed to decode the policy as valid JSON: Expecting value: line 1 column 1 (char 0)"
}

Rowe, Walter P. (Fed)

unread,
Feb 13, 2023, 10:16:58 AM2/13/23
to ansible...@googlegroups.com
Your policy_json doesn't look like JSON.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Dick Visser

unread,
Feb 13, 2023, 11:02:17 AM2/13/23
to ansible...@googlegroups.com
On Mon, 13 Feb 2023 at 15:55, Tony Wong <tdub...@gmail.com> wrote:

> "msg": "Failed to decode the policy as valid JSON: Expecting value: line 1 column 1 (char 0)"

So, you will need to use proper JSON.
Give this a try:

---
- name: test
hosts: localhost
tasks:
- name: Create IAM Managed Policy
amazon.aws.iam_policy:
iam_type: role
iam_name: "aws_test_role"
policy_name: "PrismaCloud-IAM-ReadOnly-Policy"
policy_json: "{{ policy | to_json }}"
state: present
vars:
actions:
- acm-pca:ListTags
- acm-pca:GetPolicy
- acm-pca:GetPolicy
policy: |

Tony Wong

unread,
Feb 13, 2023, 1:47:39 PM2/13/23
to ansible...@googlegroups.com
tried but it failed

fatal: [localhost]: FAILED! => {
    "boto3_version": "1.24.27",
    "botocore_version": "1.27.27",
    "changed": false,
    "error": {
        "code": "MalformedPolicyDocument",
        "message": "Syntax errors in policy.",
        "type": "Sender"

    },
    "invocation": {
        "module_args": {
            "access_key": null,
            "aws_ca_bundle": null,
            "aws_config": null,
            "debug_botocore_endpoint_logs": false,
            "endpoint_url": null,
            "iam_name": "aws_test_role",
            "iam_type": "role",
            "policy_json": "\"Version: \\\"2012-10-17\\\"\\nStatement:\\n  - Action: acm-pca:ListTags\\n    Effect: Allow\\n    Resource: \\\"*\\\"\\n  - Action: acm-pca:GetPolicy\\n    Effect: Allow\\n    Resource: \\\"*\\\"\\n  - Action: acm-pca:GetPolicy\\n    Effect: Allow\\n    Resource: \\\"*\\\"\\n\"",

            "policy_name": "PrismaCloud-IAM-ReadOnly-Policy",
            "profile": null,
            "region": null,
            "secret_key": null,
            "session_token": null,
            "skip_duplicates": false,
            "state": "present",
            "validate_certs": true
        }
    },
    "msg": "An error occurred (MalformedPolicyDocument) when calling the PutRolePolicy operation: Syntax errors in policy.",
    "response_metadata": {
        "http_headers": {
            "connection": "close",
            "content-length": "279",
            "content-type": "text/xml",
            "date": "Mon, 13 Feb 2023 16:10:28 GMT",
            "x-amzn-requestid": "8ab06377-a416-45ea-a132-328cd03d329f"
        },
        "http_status_code": 400,
        "request_id": "8ab06377-a416-45ea-a132-328cd03d329f",
        "retry_attempts": 0
    }
}

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/WZzXL_z_teA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Feb 14, 2023, 8:05:57 AM2/14/23
to ansible...@googlegroups.com
This is not an ansible problem. You need to ready the AWS docs on specifying IAM policies and make sure your policy adheres to their format and only includes the key:value pairs they accept.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALmkhkqvQmg4x-M3nQUNigO4PQ_Et765EP4tOHkJiUYvf4ftZg%40mail.gmail.com.

Tony Wong

unread,
Feb 16, 2023, 11:11:59 AM2/16/23
to ansible...@googlegroups.com
ok this is more ansible problem. 

I like to put my policy changes in a vars file

so I got a policy.yaml file like this


policy.yaml

acm-pca:ListTags
acm-pca:GetPolicy
acm-pca:GetPolicy

---
- name: test
hosts: localhost
vars_files:
- policy.yml
tasks:
- name: Create IAM Managed Policy
amazon.aws.iam_policy:
iam_type: role
iam_name: "aws_test_role"
policy_name: "PrismaCloud-IAM-ReadOnly-Policy"
policy_json: "{{ policy | to_json }}"
state: present
policy: |
Version: "2012-10-17"
Statement:
{% for action in actions %}
- Action: {{ action }}
Effect: Allow
Resource: "*"
{% endfor %}


but when i run the pb it says

ERROR! variable files must contain either a dictionary of variables, or a list of dictionaries. Got: acm-pca:ListTags acm-pca:GetPolicy acm-pca:GetPolicy (<class 'ansible.parsing.yaml.objects.AnsibleUnicode'>)


Tony Wong

unread,
Feb 16, 2023, 12:13:33 PM2/16/23
to ansible...@googlegroups.com
ended up using the policy lookup method

---
- name: test
hosts: localhost
tasks:
- name: Create IAM Managed Policy
amazon.aws.iam_policy:
iam_type: role
iam_name: "aws_test_role"
policy_name: "PrismaCloud-IAM-ReadOnly-Policy"
policy_json: "{{ lookup('template','policy.json.j2') }}"

works but I guess the whole policy needs to be replaced instead of appended

Dick Visser

unread,
Feb 16, 2023, 2:05:39 PM2/16/23
to ansible...@googlegroups.com
This is correct. 
Got: acm-pca:ListTags acm-pca:GetPolicy acm-pca:GetPolicy (<class 'ansible.parsing.yaml.objects.AnsibleUnicode'>)

Your policy yaml file should read something like

policy:
  acm-pca:ListTags
  acm-pca:GetPolicy
  acm-pca:GetPolicy

--
Sent from Gmail Mobile

Tony Wong

unread,
Feb 17, 2023, 10:16:16 AM2/17/23
to ansible...@googlegroups.com
now getting this

fatal: [localhost]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'actions' is undefined\n\nThe error appears to be in '/Users/t/virtualenv/ansible/update_iam_policy/update_iam3.yaml': line 7, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n  - name: Create IAM Managed Policy\n    ^ here\n"
}

Todd Lewis

unread,
Feb 17, 2023, 12:26:09 PM2/17/23
to Ansible Project
If you're going to post an error indicating an undefined variable problem on line 7 column 5 of a file we don't have, and you aren't going to give us that portion of the file, or show us why you think that variable should be defined at that point, what then do you expect us to do? We're trying to help you after all. Please give us the information necessary to do that.

Rowe, Walter P. (Fed)

unread,
Feb 17, 2023, 12:34:21 PM2/17/23
to ansible...@googlegroups.com
I think they did provide it .. just not with line numbers.

---
- name: test
  hosts: localhost
  vars_files:
    - policy.yml
  tasks:
    - name: Create IAM Managed Policy
      amazon.aws.iam_policy:
        iam_type: role
        iam_name: "aws_test_role"
        policy_name: "PrismaCloud-IAM-ReadOnly-Policy"
        policy_json: "{{ policy | to_json }}"
        state: present
        policy: |
          Version: "2012-10-17"
          Statement:
            {% for action in actions %}
              - Action: {{ action }}
                Effect: Allow
                Resource: "*"
            {% endfor %}

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Reply all
Reply to author
Forward
0 new messages