bash – Ansible – 高级shell命令执行格式
发布时间:2020-12-15 21:43:55 所属栏目:安全 来源:网络整理
导读:我有3个变量名为IPOctet,ServerIPRange和epcrange. 如果我在终端中执行以下操作,它可以完美地工作 IPOctet=$(echo "$ServerIPRange/$epcrange+$IPOctet" | bc) 如何在任务中的ansible中做类似的事情,例如 ---- hosts: localhost gather_facts: False vars_pr
我有3个变量名为IPOctet,ServerIPRange和epcrange.
如果我在终端中执行以下操作,它可以完美地工作 IPOctet=$(echo "$ServerIPRange/$epcrange+$IPOctet" | bc) 如何在任务中的ansible中做类似的事情,例如 --- - hosts: localhost gather_facts: False vars_prompt: - name: epcrange prompt: Enter the number of EPCs that you want to configure private: False default: "1" - name: serverrange prompt: Enter the number of Clients that you want to configure private: False default: "1" - name: ServerIPRange prompt: Enter the ServerIP range private: False default: '128' - name: LastIPOctet prompt: Enter The last Octet of the IP you just entered private: False default: '10' pre_tasks: - name: Set some facts set_fact: ServerIP1: "{{ServerIP}}" ServerIPRange1: "{{ServerIPRange}}" IPOctet: "{{LastIPOctet}}" - name: local action math local_action: shell {{IPOctet}}=$(echo "${{ServerIPRange}}/${{epcrange}}+${{IPOctet}}" | bc) # Proper Syntax? with_sequence: start=1 end=4 register: result ignore_errors: yes 这个命令的正确语法是什么?也许使用shell echo“…….”.我只需要将此命令的内容保存到IPOctet变量中,IPOctet将随每次循环迭代而改变,结果应存储在我的结果寄存器中 P.S:我如何单独访问阵列中的各个项目? 编辑:这样的事情是否可能,目前它只进行一次计算并将其存储在寄存器中4次… - name: bashless math set_fact: IPOctet: "{{ (ServerIPRange|int/epcrange|int)+IPOctet|int }}" register: IPOctet with_sequence: "start=1 end={{stop}} " register: my_ip_octet 解决方法
>您的终端表达式重新分配IPOctet shell变量,因此每次执行时都会给出不同的结果.这很好,但很难在Ansible中重现:
$IPOctet=10 ServerIPRange=128 epcrange=1 $IPOctet=$(echo "$ServerIPRange/$epcrange+$IPOctet" | bc); echo $IPOctet 138 $IPOctet=$(echo "$ServerIPRange/$epcrange+$IPOctet" | bc); echo $IPOctet 266 >语法:“shell {{IPOctet}} = $(echo …”不分配给Ansible变量. - name: local action math2 local_action: shell IPOctet={{IPOctet}}; for i in 1 2 3 4; do IPOctet=$(expr {{ServerIPRange}} / {{epcrange}} + $IPOctet); echo $IPOctet; done register: result 注意:我使用的是expr命令而不是bc,但结果是一样的. - name: iterate results local_action: debug msg={{item}} with_items: result.stdout_lines (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |