Search code examples
ansiblerhel7

Ansible control node - can it configure itself?


I'm just getting into using Ansible - please be patient if this question is misguided.

I have a handful of servers on a network, one of which I plan to setup as the control node (Rhel7.9). I would like to use ansible locally on that system to configure itself using a few playbooks of interest, and do this as a sort of test before rolling out to other systems. I'm in the process of setting up my ansible config and inventory - from everything I'm reading it makes it seem like ansible is designed to simply push configurations to managed nodes... rather than do a 1 time run for the machine you are on.

What if I would simply like to pull a playbook from ansible galaxy and run it locally on the control node?

Would this as simple as running:

ansible-playbook -i "localhost," -c local playbook.yml

Do I even need to setup inventory and config to do this for the machine I am on? Are there best practices I'm missing because of my noobity?

Thanks!


Solution

  • Q: "Pull a playbook from Ansible Galaxy and run it locally on the control node."

    A: Yes. For example the playbook

    shell> cat playbook.yml
    - hosts: all
      gather_facts: false
      tasks:
        - debug:
            var: inventory_hostname
    

    is as simple as running

    shell> ansible-playbook playbook.yml -i localhost,
    
    PLAY [all] **********************************************************************
    
    TASK [debug] ********************************************************************
    ok: [localhost] => 
      inventory_hostname: localhost
    ...
    

    Best practice

    • Review the playbook and make sure you understand what the playbook is going to configure.

    • Check the syntax first

    shell> ansible-playbook playbook.yml -i localhost, --syntax-check
    
    • Dry run the playbook and show the changes
    shell> ansible-playbook playbook.yml -i localhost, --check --diff
    
    • If you think all is right run the playbook
    shell> ansible-playbook playbook.yml -i localhost,
    

    Privilage escalation

    Run Ansible as a user and become root. See the link above on how to do it. For example, edit the playbook

    shell> cat playbook.yml
    - hosts: localhost
      gather_facts: false
      become: true
      tasks:
        - command: ls -la /root/.ssh
          register: result
        - debug:
            var: result.stdout_lines