简体   繁体   English

在openstack上使用ansible的网络自动化

[英]Automation of networks using ansible on openstack

I've written an ansible script, to create a network based on a condition. 我写了一个ansible脚本,根据条件创建一个网络。 So that, even if I run ansible script again it will not create duplicate entries in my openstack environment. 因此,即使我再次运行ansible脚本,它也不会在我的openstack环境中创建重复的条目。

task: 任务:

name: "create network"

shell: neutron net-create (openstack details like project username,password, api) network_1

when: ("neutron net-list -c name| grep network_1| awk '{print$2}'" == "null")
  the above condition didn't work so, I tried another condition

when: ("neutron net-list -c name| grep network_1| awk '{print$2}'" == neutron net-list -c name| grep network_2 | awk '{print$2}')

I don't have either of the twonetworks in my project. 我的项目中没有任何两种网络。 My intention was, both the statments display null output and firsttime condition becomes true and it should execute and create a network. 我的意图是,两个语句都显示空输出,并且firsttime条件变为true,它应该执行并创建一个网络。 If I run the script for second time it not satify condition and condition check becomes false and network will not be created. 如果我第二次运行脚本,它不满足条件和条件检查变为false并且不会创建网络。

But both the conditions skipped and returned false saying condition check failed. 但是两个条件都被跳过并且返回错误说条件检查失败。

Your when condition needs to be something that Ansible will recognise and not just a shell command that you haven't told it how to execute. 你的条件需要是Ansible会识别的东西,而不仅仅是你没有告诉它如何执行的shell命令。

In this case you could do something like this: 在这种情况下,您可以执行以下操作:

- name: check for network_1    
  shell: "neutron net-list -c name| grep network_1| awk '{print$2}'"    
  register: network_1

- name: "create network"    
  shell: neutron net-create (openstack details like project username,password, api) network_1    
  when: network_1.stdout == "null"

That's presuming that when you run neutron net-list -c name| grep network_1| awk '{print$2}' 这是假设当你运行neutron net-list -c name| grep network_1| awk '{print$2}' neutron net-list -c name| grep network_1| awk '{print$2}' neutron net-list -c name| grep network_1| awk '{print$2}' when network_1 doesn't exist it returns null (I haven't used OpenStack much so not sure if this is in fact true). neutron net-list -c name| grep network_1| awk '{print$2}'当network_1不存在时返回null (我没有使用OpenStack,因此不确定这是否真的如此)。

In general though, with Ansible you should only be shelling out if you absolutely need to because then you need to do things like above where you need to check for existence of resources and manage idempotency which should be covered for you by any decent module. 一般来说,使用Ansible,如果你绝对需要,你应该只是在炮轰,因为那时你需要做上面的事情,你需要检查资源是否存在,并管理任何体面的模块应该为你提供的幂等性。 In this case you should be able to use os_network to create your network if it doesn't already exist: 在这种情况下,如果网络尚不存在,您应该能够使用os_network来创建网络:

- os_network:
    cloud: mycloud
    state: present
    name: network_1
    external: false

It will also happily pick up environment variables such as OS_USERNAME on the host running Ansible so you can avoid putting credentials in to your Ansible code. 它还将很乐意在运行Ansible的主机上获取OS_USERNAME等环境变量,这样您就可以避免将凭据放入Ansible代码中。

  • name: network list 名称:网络列表

    shell: neutron net-list -c name | shell:neutron net-list -c name | grep network | grep网络| awk '{print$2}' awk'{print $ 2}'

    register: res 注册:res

  • name: network create 名称:网络创建

    shell: neutron net-create network shell:中子网创建网络

    when: res.stdout != "network" when:res.stdout!=“网络”

Here in first statement am checking if the network name is present in the list. 这里在第一个语句中检查列表中是否存在网络名称。 Second section am saying if network name is not equal to network, create the network. 第二部分我说如果网络名称不等于网络,则创建网络。 If we run the playbook again this condition will be false as the first statement will be able to retrieve the name from table. 如果我们再次运行playbook,则这个条件将为false,因为第一个语句将能够从表中检索名称。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM