Run command on the Ansible host

后端 未结 8 1283
小鲜肉
小鲜肉 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:35

    I'd like to share that Ansible can be run on localhost via shell:

    ansible all -i "localhost," -c local -m shell -a 'echo hello world'

    This could be helpful for simple tasks or for some hands-on learning of Ansible.

    The example of code is taken from this good article:

    Running ansible playbook in localhost

    0 讨论(0)
  • 2020-12-22 15:38

    You can use delegate_to to run commands on your Ansible host (admin host), from where you are running your Ansible play. For example:

    Delete a file if it already exists on Ansible host:

     - name: Remove file if already exists
       file:
        path: /tmp/logfile.log
        state: absent
        mode: "u+rw,g-wx,o-rwx"
       delegate_to: 127.0.0.1
    

    Create a new file on Ansible host :

     - name: Create log file
       file:
        path: /tmp/logfile.log
        state: touch
        mode: "u+rw,g-wx,o-rwx"
       delegate_to: 127.0.0.1
    
    0 讨论(0)
提交回复
热议问题