trying using ec2 module from python but inventory is missing

73 views
Skip to first unread message

paweł kamiński

unread,
May 19, 2015, 1:58:44 PM5/19/15
to ansible...@googlegroups.com
hi,
I tried to find similar problem on this forum or through google but I was redirected to ansible cmd.

I am writing python tool that can modify ec2 resources. I started with sec groups. here is sample code I try to execute

properties = {}
runner
= ansible.Runner(
 module_name
="ec2_group",
 complex_args={
    "rules": properties["rules"],
    "rules_egress": properties["rules_egress"],
   
"vpc_id": properties["vpcId"],
   
"name": properties["name"],
   
"description": properties["descr"]
 },
 forks
=10,
 
inventory=Inventory("local_action")
)


return runner.run()

off course it fails with ansible.errors.AnsibleError: Unable to find an inventory file, specify one with -i which would probably make sense when run directly using ansible cli, but I have no idea how I should code it from python.
the documentation or examples are non-existence or it is very hard to find them

here is a role part from my yaml file

---
# This role will create security groups

- name: Create security group for elb
 local_action:
   module: ec2_group
   name: "{{ app_name }}-sec"
   description: "{{ app_name }} Sec Group"
   region: "{{ region }}"
   vpc_id: "{{ vpc_id }}"
   rules:
     - proto: tcp
       from_port: 80
       to_port: 80
       cidr_ip: 0.0.0.0/0
   rules_egress:
     - proto: all
       cidr_ip: 0.0.0.0/0
 register: app_sec
 tags:
   - sec
   
- other-tag

can someone point me in any direction? any tips will be much appropriated  
 

paweł kamiński

unread,
May 19, 2015, 5:51:47 PM5/19/15
to ansible...@googlegroups.com
I am really frustrated :)
running playbook from terminal would be something like

ansible-playbook -i stage defienition.yml --private-key=~/.ssh/key.pem

where stage is a directory containing hosts file containing

[local]
127.0.0.1

I keep trying change my previous code

runner = ansible.Runner(
 module_name="ec2_group",
 complex_args={
    "rules": properties["rules"],
    "rules_egress": properties["rules_egress"],
   
"vpc_id": properties["vpcId"],
   
"name": properties["name"],
   
"description": properties["descr"]
 },
forks=paralel,
 private_key_file
="~/.ssh/office.pem",
 inventory
=Inventory(["127.0.0.1"])
)
return runner.run()

which gives me (a bit better) error

{'dark': {'127.0.0.1': {'msg': 'SSH Error: Permission denied (publickey,password).\n    while connecting to 127.0.0.1:22\nIt is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.', 'failed': True}}, 'contacted': {}}
{'dark': {'127.0.0.1': {'msg': 'SSH Error: Permission denied (publickey,password).\n    while connecting to 127.0.0.1:22\nIt is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.', 'failed': True}}, 'contacted': {}}
{'dark': {'127.0.0.1': {'msg': 'SSH Error: Permission denied (publickey,password).\n    while connecting to 127.0.0.1:22\nIt is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.', 'failed': True}}, 'contacted': {}}

I guess I need to specify that module is run in local_action context

paweł kamiński

unread,
May 19, 2015, 6:15:28 PM5/19/15
to ansible...@googlegroups.com
OOOk I figure it out,

properties = {}
runner 
= ansible.Runner(
 module_name
="ec2_group",
 complex_args={
    "rules": properties["rules"],
    "rules_egress": properties["rules_egress"],
    "vpc_id": properties["vpcId"],
    "name": properties["name"],
    "description": properties["descr"]
 },
 forks
=10,

 
inventory=Inventory(["127.0.0.1"]),
 transport="local"
)

On Tuesday, 19 May 2015 19:58:44 UTC+2, paweł kamiński wrote:
Reply all
Reply to author
Forward
0 new messages