Ansible 有很多的使用场景:

  1. 以命令行方式执行单一的task
  2. 通过ansible-playbook 来执行playbbook
  3. 使用发行版tower管理、运行playbook
  4. 以及使用开源版本的semphore 来替换tower
    以下命令,列举出了在使用ansible-playbook时的技巧
  1. Syntax checking

    $ ansible-playbook --syntax-check oraclejdk-destroy.yml
    ERROR! Syntax Error while loading YAML.

    The error appears to have been in '/home/liquidat/Gits/github/>ansible-demo-oraclejdk/oracle-windows-destroy.yml': line 10, column 11,
    but may be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:

    win_template: src=data/remove-program.j2 dest=C:\temp\remove-program.ps1

    • name: remove application
      ^ here
  2. Listing affected hosts

    $ ansible-playbook --list-hosts oraclejdk-destroy.yml

    playbook: oraclejdk-destroy.yml

    play #1 (windows): remove OracleJDK on Windows TAGS: []
    pattern: [u'windows']
    hosts (1):
    radon

    play #2 (rhel): remove OracleJDK on RHEL TAGS: []
    pattern: [u'rhel']
    hosts (2):
    neon
    helium
    ...

  3. List tasks

    $ ansible-playbook --list-tasks oraclejdk-destroy.yml

    playbook: oraclejdk-destroy.yml

    play #1 (windows): remove OracleJDK on Windows TAGS: []
    tasks:
    copy Java remove script to temp TAGS: []
    remove application TAGS: []
    remove temp dir in Windows TAGS: []

    play #2 (rhel): remove OracleJDK on RHEL TAGS: []
    tasks:
    remove java dir TAGS: []
    ...

  4. List all tags

    $ ansible-playbook --list-tags setup-control.yml

    playbook: setup-control.yml

    play #1 (tuzak): TAGS: []
    TASK TAGS: [base_setup, db, imap, ldap, mail, oc, smtp]
    ...

  5. Running in test mode

    ansible-playbook --check oraclejdk-setup.yml

    PLAY [set up OracleJDK on Windows]
    TASK [setup]
    ok: [radon]

    TASK [set up temp dir in Windows] **********************************************

    ....

  6. Starting playbooks in the middle

    nsible-playbook --start-at-task="run exe installer" oraclejdk-setup.yml

    PLAY [set up OracleJDK on Windows] *********************************************

    TASK [setup] *******************************************************************
    ok: [radon]

    TASK [run exe installer] *******************************************************
    ok: [radon]
    ...

  7. Showing diffs

    $ ansible-playbook --diff examples/template.yml

    PLAY [template example] ********************************************************

    TASK [setup] *******************************************************************
    ok: [helium]

    TASK [copy template] ***********************************************************
    changed: [helium]
    --- before: /tmp/template.conf
    +++ after: dynamically generated
    @@ -1,2 +1,3 @@
    hostname: ansible-demo-helium
    -bumble: bee
    +foo: bar
    +MX: 10 mx2.redhat.com.,5 mx1.redhat.com.

    PLAY RECAP *********************************************************************
    helium : ok=2 changed=1 unreachable=0 failed=0