Ansible: Can I execute role from command line?

前端 未结 7 827
陌清茗
陌清茗 2020-12-07 14:22

Suppose I have a role called \"apache\"

Now I want to execute that role on host 192.168.0.10 from the command line from Ansible host

ansible-playbook         


        
相关标签:
7条回答
  • 2020-12-07 15:15

    Have you tried that? it's super cool. I'm using 'update-os' instead of 'apache' role to give a more meaningful example. I have a role called let's say ./roles/update-os/ in my ./ I add a file called ./role-update-os.yml which looks like:

    #!/usr/bin/ansible-playbook
    ---
    - hosts: all
      gather_facts: yes
      become: yes
      roles:
      - update-os
    

    Make this file executable (chmod +x role-update-os.yml). Now you can run and limit to whatever you have in your inventory ./update-os.yml -i inventory-dev --limit 192.168.0.10 the limit you can pass the group names as well.

    • --limit web,db > web and db is the group defined in your inventory
    • --limit 192.168.0.10,192.168.0.201
    $ cat inventory-dev
    [web]
    192.168.0.10
    
    [db]
    192.168.0.201
    

    Note that you can configure ssh-keys and sudoers policy to be able to execute without having to type password - ideal for automation, there are security implications with this. therefore you have to analyze your environment to see whether it's suitable.

    0 讨论(0)
提交回复
热议问题