Run command on the Ansible host

后端 未结 8 1291
小鲜肉
小鲜肉 2020-12-22 14:45

Is it possible to run commands on the Ansible host?

My scenario is that I want to take a checkout from a git server that is hosted internally (and isn\'t accessible

8条回答
  •  既然无缘
    2020-12-22 15:15

    Yes, you can run commands on the Ansible host. You can specify that all tasks in a play run on the Ansible host, or you can mark individual tasks to run on the Ansible host.

    If you want to run an entire play on the Ansible host, then specify hosts: 127.0.0.1 and connection:local in the play, for example:

    - name: a play that runs entirely on the ansible host
      hosts: 127.0.0.1
      connection: local
      tasks:
      - name: check out a git repository
        git: repo=git://foosball.example.org/path/to/repo.git dest=/local/path
    

    See Local Playbooks in the Ansible documentation for more details.

    If you just want to run a single task on your Ansible host, you can use local_action to specify that a task should be run locally. For example:

    - name: an example playbook
      hosts: webservers
      tasks:
      - ...
    
      - name: check out a git repository
        local_action: git repo=git://foosball.example.org/path/to/repo.git dest=/local/path
    

    See Delegation in the Ansible documentation for more details.

    Edit: You can avoid having to type connection: local in your play by adding this to your inventory:

    localhost ansible_connection=local
    

    (Here you'd use "localhost" instead of "127.0.0.1" to refer to the play).

    Edit: In newer versions of ansible, you no longer need to add the above line to your inventory, ansible assumes it's already there.

提交回复
热议问题