WASHINGTON — President Obama said Thursday that he would deploy up to 300 military advisers to Iraq to help its struggling security forces fend off a wave of Sunni militants who have overrun large parts of the country, edging the United States back into a conflict that Mr. Obama once thought he had left behind.

Warning that the militants pose a threat not just to Iraq but also to the United States, Mr. Obama said he was prepared to take “targeted and precise military action,” a campaign of airstrikes that a senior administration official said could be extended into neighboring Syria.

Mr. Obama’s calibrated military moves — coupled with his pointed warning to Iraq’s Shiite prime minister, Nuri Kamal al-Maliki, to quell his country’s sectarian fires, and his announcement that Secretary of State John Kerry would embark on a diplomatic campaign — opened a risky new chapter in the president’s reluctant engagement with Iraq.

Having captured the presidency in part because of his opposition to the Iraq war and his promise to wind it down, Mr. Obama is now returning American soldiers to an unresolved conflict. After struggling to steer clear of the sectarian fault lines that divide Iraq, he is now plunging into yet another effort to unite a fractured country.