如何在一个var匹配中测试ansible中的子串列表?

时间:2018-05-30 00:14:59

标签: ansible jinja2 ansible-2.x

我对ansible相当新,我试图确定如何测试传递给我的playbook匹配的子串列表的变量。

我尝试过类似下面的内容。循环遍历我的badcmd列表,然后测试它是否在传递的变量中。

vars:
    badcmds:
     - clear
     - no

  tasks:


  - name: validate input
    debug:
       msg: " {{ item }}"
    when: item in my_command
    with_items: "{{ badcmds }}"

我收到以下错误:

  "msg": "The conditional check 'item in my_command' failed. 
  The error was: Unexpected templating type error occurred on
 ({% if item in my_command %} True {% else %} False {% endif %}):  
 coercing to Unicode: need string or buffer, bool found

非常感谢。

1 个答案:

答案 0 :(得分:2)

您的剧本的一个问题是package com.valcirjr98.logindj; $public class ProfileActivity extends AppCompatActivity { private static final int CHOOSE_IMAGE = 101; TextView textView; ImageView imageView; EditText editText; Uri uriProfileImage; ProgressBar progressBar; String profileImageUrl; FirebaseAuth mAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_profile); mAuth = FirebaseAuth.getInstance(); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); editText = (EditText) findViewById(R.id.editTextDisplayName); imageView = (ImageView) findViewById(R.id.imageView); progressBar = (ProgressBar) findViewById(R.id.progressbar); textView = (TextView) findViewById(R.id.textViewVerified); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showImageChooser(); } }); loadUserInformation(); findViewById(R.id.buttonSave).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { saveUserInformation(); } }); } @Override protected void onStart() { super.onStart(); if (mAuth.getCurrentUser() == null) { finish(); startActivity(new Intent(this, MainActivity.class)); } } private void loadUserInformation() { final FirebaseUser user = mAuth.getCurrentUser(); if (user != null) { if (user.getPhotoUrl() != null) { Glide.with(this) .load(user.getPhotoUrl().toString()) .into(imageView); } if (user.getDisplayName() != null) { editText.setText(user.getDisplayName()); } if (user.isEmailVerified()) { textView.setText("Email Verified"); } else { textView.setText("Email Not Verified (Click to Verify)"); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { Toast.makeText(ProfileActivity.this, "Verification Email Sent", Toast.LENGTH_SHORT).show(); } }); } }); } } } private void saveUserInformation() { String displayName = editText.getText().toString(); if (displayName.isEmpty()) { editText.setError("Name required"); editText.requestFocus(); return; } FirebaseUser user = mAuth.getCurrentUser(); if (user != null && profileImageUrl != null) { UserProfileChangeRequest profile = new UserProfileChangeRequest.Builder() .setDisplayName(displayName) .setPhotoUri(Uri.parse(profileImageUrl)) .build(); user.updateProfile(profile) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Toast.makeText(ProfileActivity.this, "Profile Updated", Toast.LENGTH_SHORT).show(); } } }); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) { uriProfileImage = data.getData(); try { Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uriProfileImage); imageView.setImageBitmap(bitmap); uploadImageToFirebaseStorage(); } catch (IOException e) { e.printStackTrace(); } } } private void uploadImageToFirebaseStorage() { StorageReference profileImageRef = FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg"); if (uriProfileImage != null) { progressBar.setVisibility(View.VISIBLE); profileImageRef.putFile(uriProfileImage) .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { progressBar.setVisibility(View.GONE); profileImageUrl = taskSnapshot.getDownloadUrl().toString(); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { progressBar.setVisibility(View.GONE); Toast.makeText(ProfileActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); } }); } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuLogout: FirebaseAuth.getInstance().signOut(); finish(); startActivity(new Intent(this, MainActivity.class)); break; } return true; } private void showImageChooser() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Profile Image"), CHOOSE_IMAGE); } } 会自动转换为布尔- no。你应该使用&#34; no&#34;使Ansible将变量视为字符串。没有引号:

false

输出:

---
- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    badcmds:
     - clear
     - no
    my_command: clear

  tasks:
  - name: print variable
    debug:
      msg: "{{ item }}"
    with_items: 
      - "{{ badcmds }}"

我想你应该在引号中加上TASK [print variable] *********************************************************************************************************************************************************************************************** ok: [localhost] => (item=None) => { "msg": "clear" } ok: [localhost] => (item=None) => { "msg": false } ,因为这不是你的意图。

进行循环并检查变量是否与no列表中的任何项匹配,您可以使用:

badcmds

希望有所帮助