简体   繁体   中英

Executing task after being logged as root in ansible

I am trying to subsequently run a task after I am connected using ssh. I am connecting using this in my playbook

- name: connect using password # task 1; this task set/connect me as root
  expect:
    command: ssh -o "StrictHostKeyChecking=no" myuser@********
    responses:
      "password:": 
         -my password
         -my password
  delegate_to: localhost

That task is fine and I am able to see that I am connected. The problem now is that when I try to run subsequent tasks for example:

- name: copy folder # task 2 in the same playbook
  copy:
    src: "files/mylocalfile.txt"
    dest: "etc/temp"
    mode: "0777"
 

I have the following message:

 "msg: etc/temp not writable"

How do I do to continue executing the remaining task as root that got connected in task1 ?

I believe this might not be an ansible question, but a linux one. Is your user in /etc/wheel?

Ansible has the direective become , which will let you execute a task as root, if the user you are connecting with is allowed to escalate privileges. The task you want to run with privileges would be something like:

- name: copy folder # task 2 in the same playbook
  become: yes
  copy:
    src: "files/mylocalfile.txt"
    dest: "etc/temp"
mode: "0777"

you can use become_user if you need to specify the user you want to run the task as, and if you have a password for the privileged user, you can ask ansible to prompt for the password when running ansible-playbook, using --ask-become-password .

The following link offers documentation about privilege escalation in ansible:

https://docs.ansible.com/ansible/latest/user_guide/become.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM