错误是:'dict object'没有属性'ansible_facts

时间:2016-11-23 17:00:35

标签: ansible ansible-playbook

我正在迭代yml文件并过滤并将这些微服务元数据保存在传递的命令行参数列表中

ansible-playbook -i inventory/inventory sp-deployer.yml --ask-vault-pass --extra-vars '{"microservices_list":[iwan,csrservice]}'

最后我需要基于上面提到的标准从yml文件中获取这三个值。我已经为此创建了ansible sp-deployer.yml。我使用了set_facts来创建动态列表。第一个列表工作正常但是当下我创建第二个失败。 PLease帮我解决这个问题。对我来说,其他任何建议都没有问题。

name: "ms_service"
port: "830"
streams: "noti,jk-noti"

vars.yml

    version: 1
    name: user
    jobs:
      ns1:
        ip: "1.1.1.1"
        ns_version: "4.2"
        f_packs:
          - f-pack-1:
            name: "pack1"
            microservices:
            - microservice-1:
              name: "ms_service"
              port: "830"
              streams: "noti,jk-noti"
            - microservice-2:
              name: "ms_service1"
              port: "830"
              streams: "noti1,jk-noti1"
          - f-pack-2:
            name: "pack4"
            microservices:
            - microservice-1:
              name: "ms_service3"
              port: "830"
              streams: "noti,jk-noti3"
            - microservice-2:
              name: "ms_service4"
              port: "830"
              streams: "noti,jk-noti4"
            - microservice-3:
              name: "ms_service5"
              port: "830"
              streams: "noti,jk-noti5"

脚本:SP-deployer.yml

    ---
- hosts: localhost
  vars_files:
    - ./vars.yml
  sudo: yes
  tasks:

  - name: Reading vars.yml file and preparing list of microservice with its metadata
    set_fact: foo_item="{{ item.1 }}"
    register: result    
    with_subelements:
     - "{{ jobs.ns1.f_packs }}"   ###item.0
     - microservices       ###item.1

  - name: make first list
    set_fact: foo="{{ result.results | map(attribute='ansible_facts.foo_item') | list }}"

  - name: print register
    debug: msg="{{ item }}" verbosity=3
    with_items:    
      - "{{ foo }}"

  - name: Filtering micro service list with match found from command line input
    when: item[0].name == item[1]
    set_fact: foo_item1="{{ item.0 }}"
    register: result_final    
    with_nested:
      - "{{ foo }}"
      - "{{ microservices_list }}"
  - name: make a list
    set_fact: foo1="{{ result_final.results | map(attribute='ansible_facts.foo_item1') | list }}"

错误

TASK [make a list] *************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'ansible_facts'\n\nThe error appears to have been in '/home/user/ansible/sp-deployer1.yml': line 40, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: make a list\n    ^ here\n"}
    to retry, use: --limit @/home/user/ansible/sp-deployer1.retry

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=1   

1 个答案:

答案 0 :(得分:4)

友情提示:如果您遇到此类错误,请务必使用protected void DetailsView1_ItemUpdating1(object sender, DetailsViewUpdateEventArgs e) { var conString = ConfigurationManager.ConnectionStrings["CONSTRING"].ConnectionString; SqlConnection con = new SqlConnection(conString); int idd = Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text); TextBox gt = DetailsView1.Rows[1].Cells[1].Controls[0] as ?TextBox; TextBox ma = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox mo = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox eng = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox cn = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox vin = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox am = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox cond = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox var = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox col = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox carnum = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; SqlDataAdapter SQLAdapter = new SqlDataAdapter("update cars set geartype = '"+gt.Text+ "', make ='" +ma.Text + "', model ='" + mo.Text + "', engnum ='" + eng.Text + "', chasisnum ='" + cn.Text + "', vin ='" + vin.Text + "', amount ='" + am.Text + "', Condition ='" + cond.Text + "', variant ='" + var.Text + "', color ='" + col.Text + "', carnum ='" + carnum.Text + "' where carid="+idd+"", con); DataTable DT = new DataTable(); SQLAdapter.Fill(DT); DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); DetailsView1.DataBind(); } 检查已注册的变量。

出现错误的原因是debug如果跳过循环迭代,则不会产生set_fact dict。
我看到你的循环中有ansible_facts语句。

要克服此错误,您应该只选择那些已定义when dict的循环迭代:

ansible_facts